Skip to content

Commit 5a234bc

Browse files
christophstroblmp911de
authored andcommitted
Add AOT Repositories example.
Closes #695
1 parent e145037 commit 5a234bc

File tree

20 files changed

+1014
-1
lines changed

20 files changed

+1014
-1
lines changed

jpa/aot-generation/pom.xml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.data.examples</groupId>
8+
<artifactId>spring-data-jpa-examples</artifactId>
9+
<version>2.0.0.BUILD-SNAPSHOT</version>
10+
</parent>
11+
12+
<groupId>org.example</groupId>
13+
<artifactId>aot-generation</artifactId>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<hibernate.version>7.0.0.Beta5</hibernate.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.jspecify</groupId>
23+
<artifactId>jspecify</artifactId>
24+
<version>1.0.0</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>com.querydsl</groupId>
29+
<artifactId>querydsl-jpa</artifactId>
30+
<version>5.1.0</version>
31+
<classifier>jakarta</classifier>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.querydsl</groupId>
35+
<artifactId>querydsl-apt</artifactId>
36+
<version>5.1.0</version>
37+
<classifier>jakarta</classifier>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.apache.maven.plugins</groupId>
45+
<artifactId>maven-compiler-plugin</artifactId>
46+
<configuration>
47+
<annotationProcessorPaths>
48+
<annotationProcessorPath>
49+
<groupId>com.querydsl</groupId>
50+
<artifactId>querydsl-jpa</artifactId>
51+
<version>5.1.0</version>
52+
<classifier>jakarta</classifier>
53+
</annotationProcessorPath>
54+
<annotationProcessorPath>
55+
<groupId>com.querydsl</groupId>
56+
<artifactId>querydsl-apt</artifactId>
57+
<version>5.1.0</version>
58+
<classifier>jakarta</classifier>
59+
</annotationProcessorPath>
60+
<annotationProcessorPath>
61+
<groupId>jakarta.persistence</groupId>
62+
<artifactId>jakarta.persistence-api</artifactId>
63+
<version>3.2.0</version>
64+
</annotationProcessorPath>
65+
</annotationProcessorPaths>
66+
67+
<!-- Recommended: Some IDE's might require this configuration to include generated sources for IDE usage -->
68+
<generatedTestSourcesDirectory>target/generated-test-sources
69+
</generatedTestSourcesDirectory>
70+
<generatedSourcesDirectory>target/generated-sources
71+
</generatedSourcesDirectory>
72+
</configuration>
73+
</plugin>
74+
<plugin>
75+
<groupId>org.springframework.boot</groupId>
76+
<artifactId>spring-boot-maven-plugin</artifactId>
77+
<executions>
78+
<execution>
79+
<id>process-aot</id>
80+
<goals>
81+
<goal>process-aot</goal>
82+
</goals>
83+
</execution>
84+
</executions>
85+
</plugin>
86+
</plugins>
87+
</build>
88+
89+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.aot;
17+
18+
import org.springframework.boot.SpringApplication;
19+
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
21+
/**
22+
* @author Christoph Strobl
23+
*/
24+
@SpringBootApplication
25+
public class AotJpaApp {
26+
27+
public static void main(String[] args) {
28+
SpringApplication.run(AotJpaApp.class, args);
29+
}
30+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.aot;
17+
18+
import java.util.List;
19+
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.boot.CommandLineRunner;
22+
import org.springframework.data.domain.Page;
23+
import org.springframework.data.domain.PageRequest;
24+
import org.springframework.data.domain.Slice;
25+
import org.springframework.stereotype.Component;
26+
27+
/**
28+
* @author Christoph Strobl
29+
* @since 2025/01
30+
*/
31+
@Component
32+
public class CLR implements CommandLineRunner {
33+
34+
@Autowired UserRepository repository;
35+
36+
@Override
37+
public void run(String... args) throws Exception {
38+
39+
User luke = new User("id-1", "luke");
40+
luke.setFirstname("Luke");
41+
luke.setLastname("Skywalker");
42+
// Post lukeP1 = new Post("I have a bad feeling about this.");
43+
// em.persist(lukeP1);
44+
// luke.setPosts(List.of(lukeP1));
45+
46+
User leia = new User("id-2", "leia");
47+
leia.setFirstname("Leia");
48+
leia.setLastname("Organa");
49+
50+
User han = new User("id-3", "han");
51+
han.setFirstname("Han");
52+
han.setLastname("Solo");
53+
// Post hanP1 = new Post("It's the ship that made the Kessel Run in less than 12 Parsecs.");
54+
// em.persist(hanP1);
55+
// han.setPosts(List.of(hanP1));
56+
57+
User chewbacca = new User("id-4", "chewbacca");
58+
User yoda = new User("id-5", "yoda");
59+
Post yodaP1 = new Post("Do. Or do not. There is no try.");
60+
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.");
61+
// em.persist(yodaP1);
62+
// em.persist(yodaP2);
63+
// yoda.setPosts(List.of(yodaP1, yodaP2));
64+
65+
User vader = new User("id-6", "vader");
66+
vader.setFirstname("Anakin");
67+
vader.setLastname("Skywalker");
68+
// Post vaderP1 = new Post("I am your father");
69+
// em.persist(vaderP1);
70+
// vader.setPosts(List.of(vaderP1));
71+
72+
User kylo = new User("id-7", "kylo");
73+
kylo.setFirstname("Ben");
74+
kylo.setLastname("Solo");
75+
76+
repository.saveAll(List.of(luke, leia, han, chewbacca, yoda, vader, kylo));
77+
78+
System.out.println("------- annotated multi -------");
79+
System.out.println(repository.usersWithUsernamesStartingWith("l"));
80+
81+
System.out.println("------- derived single -------");
82+
System.out.println(repository.findUserByUsername("yoda"));
83+
84+
// System.out.println("------- derived nested.path -------");
85+
// System.out.println(repository.findUserByPostsMessageLike("father"));
86+
87+
System.out.println("------- derived optional -------");
88+
System.out.println(repository.findOptionalUserByUsername("yoda"));
89+
90+
System.out.println("------- derived count -------");
91+
Long count = repository.countUsersByLastnameLike("Sky");
92+
System.out.println("user count " + count);
93+
94+
System.out.println("------- derived exists -------");
95+
Boolean exists = repository.existsByUsername("vader");
96+
System.out.println("user exists " + exists);
97+
98+
System.out.println("------- derived multi -------");
99+
System.out.println(repository.findUserByLastnameStartingWith("Sky"));
100+
101+
System.out.println("------- derived sorted -------");
102+
System.out.println(repository.findUserByLastnameStartingWithOrderByFirstname("Sky"));
103+
104+
System.out.println("------- derived page -------");
105+
Page<User> page0 = repository.findUserByLastnameStartingWith("S", PageRequest.of(0, 2));
106+
System.out.println("page0: " + page0);
107+
System.out.println("page0.content: " + page0.getContent());
108+
109+
Page<User> page1 = repository.findUserByLastnameStartingWith("S", PageRequest.of(1, 2));
110+
System.out.println("page1: " + page1);
111+
System.out.println("page1.content: " + page1.getContent());
112+
113+
System.out.println("------- derived slice -------");
114+
Slice<User> slice0 = repository.findUserByUsernameAfter("luke", PageRequest.of(0, 2));
115+
System.out.println("slice0: " + slice0);
116+
System.out.println("slice0.content: " + slice0.getContent());
117+
118+
System.out.println("------- derived top -------");
119+
System.out.println(repository.findTop2UsersByLastnameStartingWith("S"));
120+
121+
// System.out.println("------- derived with fields -------");
122+
// System.out.println(repository.findJustUsernameBy());
123+
}
124+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.aot;
17+
18+
import java.time.Instant;
19+
import java.time.temporal.ChronoUnit;
20+
import java.util.Random;
21+
22+
import jakarta.persistence.Entity;
23+
import jakarta.persistence.GeneratedValue;
24+
import jakarta.persistence.Id;
25+
26+
/**
27+
* @author Christoph Strobl
28+
* @since 2025/01
29+
*/
30+
@Entity
31+
public class Post {
32+
33+
@Id
34+
@GeneratedValue
35+
private Long id;
36+
37+
private String message;
38+
private Instant date;
39+
40+
public Post() {
41+
}
42+
43+
public Post(String message) {
44+
this.message = message;
45+
this.date = Instant.now().minus(new Random().nextLong(100), ChronoUnit.MINUTES);
46+
}
47+
48+
public String getMessage() {
49+
return message;
50+
}
51+
52+
public void setMessage(String message) {
53+
this.message = message;
54+
}
55+
56+
public Instant getDate() {
57+
return date;
58+
}
59+
60+
public void setDate(Instant date) {
61+
this.date = date;
62+
}
63+
64+
@Override
65+
public String toString() {
66+
return message;
67+
}
68+
}

0 commit comments

Comments
 (0)