Skip to content

Commit 78098a4

Browse files
committed
Add AOT Examples for JDBC and Cassandra.
Closes #701
1 parent f58cc5a commit 78098a4

File tree

18 files changed

+711
-4
lines changed

18 files changed

+711
-4
lines changed

cassandra/aot-optimization/pom.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<artifactId>spring-data-cassandra-aot-optimization-example</artifactId>
7+
8+
<parent>
9+
<groupId>org.springframework.data.examples</groupId>
10+
<artifactId>spring-data-cassandra-examples</artifactId>
11+
<version>2.0.0.BUILD-SNAPSHOT</version>
12+
<relativePath>../pom.xml</relativePath>
13+
</parent>
14+
15+
<name>Spring Data Cassandra - AOT Optimization Example</name>
16+
<description>Sample project showing the usage of Spring Data Cassandra AOT
17+
Repositories.
18+
</description>
19+
20+
<properties>
21+
<spring-data-bom.version>2025.1.0-SNAPSHOT</spring-data-bom.version>
22+
<spring-framework.version>7.0.0-M9</spring-framework.version>
23+
</properties>
24+
25+
<dependencies>
26+
27+
<dependency>
28+
<groupId>org.springframework.data</groupId>
29+
<artifactId>spring-data-cassandra</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>${project.groupId}</groupId>
34+
<artifactId>spring-data-cassandra-example-utils</artifactId>
35+
<version>${project.version}</version>
36+
<scope>test</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>com.fasterxml.jackson.core</groupId>
41+
<artifactId>jackson-databind</artifactId>
42+
</dependency>
43+
44+
</dependencies>
45+
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-maven-plugin</artifactId>
52+
<executions>
53+
<execution>
54+
<id>process-aot</id>
55+
<goals>
56+
<goal>process-aot</goal>
57+
</goals>
58+
</execution>
59+
</executions>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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.cassandra;
17+
18+
import org.springframework.data.cassandra.core.mapping.UserDefinedType;
19+
20+
/**
21+
* @author Mark Paluch
22+
*/
23+
@UserDefinedType
24+
public record Address(String street, String zip, String city) {
25+
26+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.cassandra;
17+
18+
import java.util.Collections;
19+
20+
import org.springframework.boot.CommandLineRunner;
21+
import org.springframework.boot.SpringApplication;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.data.cassandra.core.CassandraOperations;
25+
import org.springframework.data.domain.Limit;
26+
27+
/**
28+
* Basic Spring Boot application.
29+
*
30+
* @author Mark Paluch
31+
*/
32+
@SpringBootApplication
33+
public class AotApplication {
34+
35+
public static void main(String[] args) {
36+
SpringApplication.run(AotApplication.class, args);
37+
}
38+
39+
@Bean
40+
CommandLineRunner commandLineRunner(UserRepository repository, CassandraOperations operations)
41+
throws InterruptedException {
42+
43+
operations.getCqlOperations().execute("CREATE INDEX IF NOT EXISTS user_username ON users (uname);");
44+
operations.getCqlOperations().execute(
45+
"CREATE CUSTOM INDEX IF NOT EXISTS users_lname_idx_1 ON users (lname) USING 'org.apache.cassandra.index.sasi.SASIIndex';");
46+
47+
/*
48+
Cassandra secondary indexes are created in the background without the possibility to check
49+
whether they are available or not. So we are forced to just wait. *sigh*
50+
*/
51+
Thread.sleep(1000);
52+
53+
return args -> {
54+
55+
User user = new User();
56+
user.setId(42L);
57+
user.setUsername("heisenberg");
58+
user.setFirstname("Walter");
59+
user.setLastname("White");
60+
61+
user.setCurrent(new Address("308 Negra Arroyo Lane", "87104", "Albuquerque"));
62+
user.setPrevious(Collections.singletonList(new Address("12000 – 12100 Coors Rd SW", "87045", "Albuquerque")));
63+
64+
repository.save(user);
65+
66+
System.out.println("------- Annotated Single -------");
67+
System.out.println(repository.findUserByIdIn(1000));
68+
System.out.println(repository.findUserByIdIn(42));
69+
70+
System.out.println("------- Derived Single -------");
71+
System.out.println(repository.findUserByUsername(user.getUsername()));
72+
73+
System.out.println("------- Derived SASI -------");
74+
System.out.println(repository.findUsersByLastnameStartsWith("White", Limit.of(1)));
75+
};
76+
77+
}
78+
79+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.cassandra;
17+
18+
import lombok.Data;
19+
import lombok.NoArgsConstructor;
20+
21+
import java.util.List;
22+
23+
import org.springframework.data.cassandra.core.mapping.CassandraType;
24+
import org.springframework.data.cassandra.core.mapping.Column;
25+
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
26+
import org.springframework.data.cassandra.core.mapping.Table;
27+
import org.springframework.data.geo.Point;
28+
29+
import com.datastax.oss.driver.api.core.data.UdtValue;
30+
31+
/**
32+
* Sample user class.
33+
*
34+
* @author Oliver Gierke
35+
* @author Thomas Darimont
36+
* @author Mark Paluch
37+
*/
38+
@Data
39+
@NoArgsConstructor
40+
@Table(value = "users")
41+
public class User {
42+
43+
@PrimaryKey("user_id") private Long id;
44+
45+
@Column("uname") private String username;
46+
@Column("fname") private String firstname;
47+
@Column("lname") private String lastname;
48+
49+
Address current;
50+
List<Address> previous;
51+
Point point;
52+
53+
@CassandraType(type = CassandraType.Name.UDT, userTypeName = "address") UdtValue alternative;
54+
55+
public User(Long id) {
56+
this.setId(id);
57+
}
58+
59+
public User(Long id, String firstname, String lastname) {
60+
this.id = id;
61+
this.firstname = firstname;
62+
this.lastname = lastname;
63+
}
64+
}
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+
* 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.cassandra;
17+
18+
import java.util.List;
19+
20+
import org.springframework.data.cassandra.repository.Query;
21+
import org.springframework.data.domain.Limit;
22+
import org.springframework.data.repository.CrudRepository;
23+
24+
/**
25+
* Simple repository interface for {@link User} instances. The interface is used to declare the so-called query methods,
26+
* i.e. methods to retrieve single entities or collections of them.
27+
*
28+
* @author Thomas Darimont
29+
*/
30+
public interface UserRepository extends CrudRepository<User, Long> {
31+
32+
/**
33+
* Sample method annotated with {@link Query}. This method executes the CQL from the {@link Query} value.
34+
*
35+
* @param id
36+
* @return
37+
*/
38+
@Query("SELECT * from users where user_id in(?0)")
39+
User findUserByIdIn(long id);
40+
41+
/**
42+
* Derived query method. This query corresponds with {@code SELECT * FROM users WHERE uname = ?0}.
43+
* {@link User#username} is not part of the primary so it requires a secondary index.
44+
*
45+
* @param username
46+
* @return
47+
*/
48+
User findUserByUsername(String username);
49+
50+
/**
51+
* Derived query method using SASI (SSTable Attached Secondary Index) features through the {@code LIKE} keyword. This
52+
* query corresponds with {@code SELECT * FROM users WHERE lname LIKE '?0'}. {@link User#lastname} is not part of the
53+
* primary key so it requires a secondary index.
54+
*
55+
* @param lastnamePrefix
56+
* @return
57+
*/
58+
List<User> findUsersByLastnameStartsWith(String lastnamePrefix);
59+
60+
/**
61+
* Same as {@link #findUsersByLastnameStartsWith(String)} but reducing the result size to a given {@link Limit}.
62+
*
63+
* @param lastnamePrefix
64+
* @param maxResults the maximum number of results returned.
65+
* @return
66+
*/
67+
List<User> findUsersByLastnameStartsWith(String lastnamePrefix, Limit maxResults);
68+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
17+
/**
18+
* Package showing a simple repository interface to use basic query method execution functionality.
19+
*/
20+
package example.springdata.cassandra;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.cassandra.keyspace-name=example
2+
spring.cassandra.schema-action=recreate
3+
spring.cassandra.local-datacenter=datacenter1

cassandra/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
<modules>
2020
<module>util</module>
21+
<module>aot-optimization</module>
2122
<module>example</module>
2223
<module>kotlin</module>
2324
<module>reactive</module>

jdbc/aot-optimization/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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-jdbc-examples</artifactId>
9+
<version>2.0.0.BUILD-SNAPSHOT</version>
10+
</parent>
11+
12+
<groupId>org.example</groupId>
13+
<artifactId>spring-data-jdbc-aot-optimization</artifactId>
14+
<name>Spring Data JDBC - AOT Optimization Example</name>
15+
<description>Sample project showing the usage of Spring Data JDBC AOT
16+
Repositories.
17+
</description>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-maven-plugin</artifactId>
24+
<executions>
25+
<execution>
26+
<id>process-aot</id>
27+
<goals>
28+
<goal>process-aot</goal>
29+
</goals>
30+
</execution>
31+
</executions>
32+
</plugin>
33+
</plugins>
34+
</build>
35+
36+
</project>

0 commit comments

Comments
 (0)