Add AOT Repositories example.

Closes #695
This commit is contained in:
Christoph Strobl
2025-01-09 13:21:54 +01:00
committed by Mark Paluch
parent e14503721a
commit 5a234bcef3
20 changed files with 1014 additions and 1 deletions

View File

@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jpa-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<groupId>org.example</groupId>
<artifactId>aot-generation</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>7.0.0.Beta5</hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>5.1.0</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>5.1.0</version>
<classifier>jakarta</classifier>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>5.1.0</version>
<classifier>jakarta</classifier>
</annotationProcessorPath>
<annotationProcessorPath>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>5.1.0</version>
<classifier>jakarta</classifier>
</annotationProcessorPath>
<annotationProcessorPath>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.2.0</version>
</annotationProcessorPath>
</annotationProcessorPaths>
<!-- Recommended: Some IDE's might require this configuration to include generated sources for IDE usage -->
<generatedTestSourcesDirectory>target/generated-test-sources
</generatedTestSourcesDirectory>
<generatedSourcesDirectory>target/generated-sources
</generatedSourcesDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>process-aot</id>
<goals>
<goal>process-aot</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.aot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author Christoph Strobl
*/
@SpringBootApplication
public class AotJpaApp {
public static void main(String[] args) {
SpringApplication.run(AotJpaApp.class, args);
}
}

View File

@@ -0,0 +1,124 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.aot;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Component;
/**
* @author Christoph Strobl
* @since 2025/01
*/
@Component
public class CLR implements CommandLineRunner {
@Autowired UserRepository repository;
@Override
public void run(String... args) throws Exception {
User luke = new User("id-1", "luke");
luke.setFirstname("Luke");
luke.setLastname("Skywalker");
// Post lukeP1 = new Post("I have a bad feeling about this.");
// em.persist(lukeP1);
// luke.setPosts(List.of(lukeP1));
User leia = new User("id-2", "leia");
leia.setFirstname("Leia");
leia.setLastname("Organa");
User han = new User("id-3", "han");
han.setFirstname("Han");
han.setLastname("Solo");
// Post hanP1 = new Post("It's the ship that made the Kessel Run in less than 12 Parsecs.");
// em.persist(hanP1);
// han.setPosts(List.of(hanP1));
User chewbacca = new User("id-4", "chewbacca");
User yoda = new User("id-5", "yoda");
Post yodaP1 = new Post("Do. Or do not. There is no try.");
Post yodaP2 = new Post("Decide you must, how to serve them best. If you leave now, help them you could; but you would destroy all for which they have fought, and suffered.");
// em.persist(yodaP1);
// em.persist(yodaP2);
// yoda.setPosts(List.of(yodaP1, yodaP2));
User vader = new User("id-6", "vader");
vader.setFirstname("Anakin");
vader.setLastname("Skywalker");
// Post vaderP1 = new Post("I am your father");
// em.persist(vaderP1);
// vader.setPosts(List.of(vaderP1));
User kylo = new User("id-7", "kylo");
kylo.setFirstname("Ben");
kylo.setLastname("Solo");
repository.saveAll(List.of(luke, leia, han, chewbacca, yoda, vader, kylo));
System.out.println("------- annotated multi -------");
System.out.println(repository.usersWithUsernamesStartingWith("l"));
System.out.println("------- derived single -------");
System.out.println(repository.findUserByUsername("yoda"));
// System.out.println("------- derived nested.path -------");
// System.out.println(repository.findUserByPostsMessageLike("father"));
System.out.println("------- derived optional -------");
System.out.println(repository.findOptionalUserByUsername("yoda"));
System.out.println("------- derived count -------");
Long count = repository.countUsersByLastnameLike("Sky");
System.out.println("user count " + count);
System.out.println("------- derived exists -------");
Boolean exists = repository.existsByUsername("vader");
System.out.println("user exists " + exists);
System.out.println("------- derived multi -------");
System.out.println(repository.findUserByLastnameStartingWith("Sky"));
System.out.println("------- derived sorted -------");
System.out.println(repository.findUserByLastnameStartingWithOrderByFirstname("Sky"));
System.out.println("------- derived page -------");
Page<User> page0 = repository.findUserByLastnameStartingWith("S", PageRequest.of(0, 2));
System.out.println("page0: " + page0);
System.out.println("page0.content: " + page0.getContent());
Page<User> page1 = repository.findUserByLastnameStartingWith("S", PageRequest.of(1, 2));
System.out.println("page1: " + page1);
System.out.println("page1.content: " + page1.getContent());
System.out.println("------- derived slice -------");
Slice<User> slice0 = repository.findUserByUsernameAfter("luke", PageRequest.of(0, 2));
System.out.println("slice0: " + slice0);
System.out.println("slice0.content: " + slice0.getContent());
System.out.println("------- derived top -------");
System.out.println(repository.findTop2UsersByLastnameStartingWith("S"));
// System.out.println("------- derived with fields -------");
// System.out.println(repository.findJustUsernameBy());
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.aot;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Random;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
/**
* @author Christoph Strobl
* @since 2025/01
*/
@Entity
public class Post {
@Id
@GeneratedValue
private Long id;
private String message;
private Instant date;
public Post() {
}
public Post(String message) {
this.message = message;
this.date = Instant.now().minus(new Random().nextLong(100), ChronoUnit.MINUTES);
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Instant getDate() {
return date;
}
public void setDate(Instant date) {
this.date = date;
}
@Override
public String toString() {
return message;
}
}

View File

@@ -0,0 +1,114 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.aot;
import java.time.Instant;
import java.util.List;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
/**
* @author Christoph Strobl
* @since 2025/01
*/
@Entity(name = "users")
public class User {
@Id
private String id;
private String username;
@Column(name = "first_name") String firstname;
@Column(name = "last_name") String lastname;
// @OneToMany
// private List<Post> posts;
Instant registrationDate;
Instant lastSeen;
public User() {
}
public User(String id, String username) {
this.id = id;
this.username = username;
}
public String getId() {
return id;
}
public String getUsername() {
return username;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Instant getRegistrationDate() {
return registrationDate;
}
public void setRegistrationDate(Instant registrationDate) {
this.registrationDate = registrationDate;
}
public Instant getLastSeen() {
return lastSeen;
}
public void setLastSeen(Instant lastSeen) {
this.lastSeen = lastSeen;
}
// public List<Post> getPosts() {
// return posts;
// }
//
// public void setPosts(List<Post> posts) {
// this.posts = posts;
// }
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", username='" + username + '\'' +
", firstname='" + firstname + '\'' +
", lastname='" + lastname + '\'' +
", registrationDate=" + registrationDate +
", lastSeen=" + lastSeen +
// ", posts=" + posts +
'}';
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.aot;
import java.time.Instant;
/**
* @author Christoph Strobl
* @since 2025/01
*/
public record UserProjection(String username, Instant registrationDate) {
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.aot;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.CrudRepository;
/**
* @author Christoph Strobl
* @since 2025/01
*/
public interface UserRepository extends CrudRepository<User, String>, QuerydslPredicateExecutor<User> {
User findUserByUsername(String username);
Optional<User> findOptionalUserByUsername(String username);
Long countUsersByLastnameLike(String lastname);
Boolean existsByUsername(String username);
List<User> findUserByLastnameLike(String lastname);
List<User> findUserByLastnameStartingWithOrderByFirstname(String lastname);
List<User> findTop2UsersByLastnameStartingWith(String lastname);
Slice<User> findUserByUsernameAfter(String username, Pageable pageable);
List<User> findUserByLastnameStartingWith(String lastname);
Page<User> findUserByLastnameStartingWith(String lastname, Pageable page);
@Query("SELECT u FROM example.springdata.aot.User u WHERE u.username LIKE ?1%")
List<User> usersWithUsernamesStartingWith(String username);
}

View File

@@ -0,0 +1,5 @@
spring.jpa.defer-datasource-initialization=true
spring.jpa.properties.hibernate.boot.allow_jdbc_metadata_access=false
spring.aot.repositories.enabled=true
#logging.level.org.springframework.data.repository.aot.generate.RepositoryContributor=trace

View File

@@ -29,6 +29,7 @@
<module>vavr</module>
<module>multitenant</module>
<module>graalvm-native</module>
<module>aot-generation</module>
</modules>
<dependencies>
@@ -38,6 +39,24 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>4.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>4.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>

View File

@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-mongodb-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<groupId>org.example</groupId>
<artifactId>aot-generation</artifactId>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>4.0.x-GENERATED-REPOSITORIES-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>5.0.0-GENERATED-REPOSITORIES-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>process-aot</id>
<goals>
<goal>process-aot</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.aot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author Christoph Strobl
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

View File

@@ -0,0 +1,114 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.aot;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Component;
/**
* @author Christoph Strobl
* @since 2025/01
*/
@Component
public class CLR implements CommandLineRunner {
@Autowired UserRepository repository;
@Override
public void run(String... args) throws Exception {
User luke = new User("id-1", "luke");
luke.setFirstname("Luke");
luke.setLastname("Skywalker");
luke.setPosts(List.of(new Post("I have a bad feeling about this.")));
User leia = new User("id-2", "leia");
leia.setFirstname("Leia");
leia.setLastname("Organa");
User han = new User("id-3", "han");
han.setFirstname("Han");
han.setLastname("Solo");
han.setPosts(List.of(new Post("It's the ship that made the Kessel Run in less than 12 Parsecs.")));
User chewbacca = new User("id-4", "chewbacca");
User yoda = new User("id-5", "yoda");
yoda.setPosts(List.of(new Post("Do. Or do not. There is no try."), new Post("Decide you must, how to serve them best. If you leave now, help them you could; but you would destroy all for which they have fought, and suffered.")));
User vader = new User("id-6", "vader");
vader.setFirstname("Anakin");
vader.setLastname("Skywalker");
vader.setPosts(List.of(new Post("I am your father")));
User kylo = new User("id-7", "kylo");
kylo.setFirstname("Ben");
kylo.setLastname("Solo");
repository.saveAll(List.of(luke, leia, han, chewbacca, yoda, vader, kylo));
System.out.println("------- annotated multi -------");
System.out.println(repository.usersWithUsernamesStartingWith("l"));
System.out.println("------- derived single -------");
System.out.println(repository.findUserByUsername("yoda"));
System.out.println("------- derived nested.path -------");
System.out.println(repository.findUserByPostsMessageLike("father"));
System.out.println("------- derived optional -------");
System.out.println(repository.findOptionalUserByUsername("yoda"));
System.out.println("------- derived count -------");
Long count = repository.countUsersByLastnameLike("Sky");
System.out.println("user count " + count);
System.out.println("------- derived exists -------");
Boolean exists = repository.existsByUsername("vader");
System.out.println("user exists " + exists);
System.out.println("------- derived multi -------");
System.out.println(repository.findUserByLastnameLike("Sky"));
System.out.println("------- derived sorted -------");
System.out.println(repository.findUserByLastnameLikeOrderByFirstname("Sky"));
System.out.println("------- derived page -------");
Page<UserProjection> page0 = repository.findUserByLastnameStartingWith("S", PageRequest.of(0, 2));
System.out.println("page0: " + page0);
System.out.println("page0.content: " + page0.getContent());
Page<UserProjection> page1 = repository.findUserByLastnameStartingWith("S", PageRequest.of(1, 2));
System.out.println("page1: " + page1);
System.out.println("page1.content: " + page1.getContent());
System.out.println("------- derived slice -------");
Slice<User> slice0 = repository.findUserByUsernameAfter("luke", PageRequest.of(0, 2));
System.out.println("slice0: " + slice0);
System.out.println("slice0.content: " + slice0.getContent());
System.out.println("------- derived top -------");
System.out.println(repository.findTop2UsersByLastnameLike("S"));
System.out.println("------- derived with fields -------");
System.out.println(repository.findJustUsernameBy());
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.aot;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Random;
/**
* @author Christoph Strobl
* @since 2025/01
*/
public class Post {
private String message;
private Instant date;
public Post(String message) {
this.message = message;
this.date = Instant.now().minus(new Random().nextLong(100), ChronoUnit.MINUTES);
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Instant getDate() {
return date;
}
public void setDate(Instant date) {
this.date = date;
}
@Override
public String toString() {
return message;
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.aot;
import java.time.Instant;
import java.util.List;
import org.springframework.data.mongodb.core.mapping.Field;
/**
* @author Christoph Strobl
* @since 2025/01
*/
public class User {
private final String id;
private final String username;
@Field("first_name") String firstname;
@Field("last_name") String lastname;
private List<Post> posts;
Instant registrationDate;
Instant lastSeen;
public User(String id, String username) {
this.id = id;
this.username = username;
}
public String getId() {
return id;
}
public String getUsername() {
return username;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Instant getRegistrationDate() {
return registrationDate;
}
public void setRegistrationDate(Instant registrationDate) {
this.registrationDate = registrationDate;
}
public Instant getLastSeen() {
return lastSeen;
}
public void setLastSeen(Instant lastSeen) {
this.lastSeen = lastSeen;
}
public List<Post> getPosts() {
return posts;
}
public void setPosts(List<Post> posts) {
this.posts = posts;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", username='" + username + '\'' +
", firstname='" + firstname + '\'' +
", lastname='" + lastname + '\'' +
", registrationDate=" + registrationDate +
", lastSeen=" + lastSeen +
", posts=" + posts +
'}';
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.aot;
import java.time.Instant;
/**
* @author Christoph Strobl
* @since 2025/01
*/
public interface UserProjection {
String getUsername();
Instant getRegistrationDate();
}

View File

@@ -0,0 +1,44 @@
package example.springdata.aot;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.CrudRepository;
/**
* @author Christoph Strobl
* @since 2025/01
*/
public interface UserRepository extends CrudRepository<User, String> {
User findUserByUsername(String username);
Optional<User> findOptionalUserByUsername(String username);
Long countUsersByLastnameLike(String lastname);
Boolean existsByUsername(String username);
List<User> findUserByLastnameLike(String lastname);
List<User> findUserByPostsMessageLike(String part);
List<User> findUserByLastnameLikeOrderByFirstname(String lastname);
List<User> findTop2UsersByLastnameLike(String lastname);
Slice<User> findUserByUsernameAfter(String username, Pageable pageable);
Page<UserProjection> findUserByLastnameStartingWith(String lastname, Pageable page);
@Query("{ 'username' : { $regex: '?0.*', $options: 'i' } }")
List<UserProjection> usersWithUsernamesStartingWith(String username);
@Query(fields = "{ 'username' : 1 }", sort = "{ 'username' : -1 }")
List<User> findJustUsernameBy();
}

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
</encoder>
</appender>
<appender name="no-op" class="ch.qos.logback.core.helpers.NOPAppender" />
<!--
<logger name="org.springframework" level="debug" />
-->
<logger name="org.springframework.data.mongodb.core" level="error"/>
<logger name="org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener" level="debug" additivity="false">
<appender-ref ref="no-op" />
</logger>
<logger name="org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLogger" level="warn" />
<logger name="org.springframework.data.mongodb.test.util" level="info"/>
<logger name="org.springframework.data.repository.aot.generate.RepositoryContributor" level="trace" />
<logger name="example.springdata.aot" level="debug" />
<root level="warn">
<appender-ref ref="console" />
</root>
</configuration>

View File

@@ -0,0 +1 @@
spring.aot.repositories.enabled=true

View File

@@ -36,7 +36,8 @@
<module>linking</module>
<module>util</module>
<module>fragment-spi</module>
</modules>
<module>aot-generation</module>
</modules>
<dependencyManagement>
<dependencies>

View File

@@ -37,7 +37,10 @@
<properties>
<apt.version>1.1.3</apt.version>
<jvm.enable-preview></jvm.enable-preview>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-data-bom.version>2025.1.0-SNAPSHOT</spring-data-bom.version>
</properties>
<profiles>