Skip to content

Commit a80711a

Browse files
committed
Add JPA tests for property path usage.
1 parent e847b80 commit a80711a

3 files changed

Lines changed: 56 additions & 3 deletions

File tree

jpa/example/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<name>Spring Data JPA - Example</name>
1515
<description>Small sample project showing the usage of Spring Data JPA.</description>
1616

17+
<properties>
18+
<spring-data-bom.version>2026.0.0-SNAPSHOT</spring-data-bom.version>
19+
</properties>
20+
1721
<build>
1822
<plugins>
1923
<plugin>

jpa/example/src/test/java/example/springdata/jpa/simple/SimpleUserRepositoryTests.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2021 the original author or authors.
2+
* Copyright 2013-present the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,6 +51,7 @@
5151
* @author Christoph Strobl
5252
* @author Divya Srivastava
5353
* @author Jens Schauder
54+
* @author Mark Paluch
5455
*/
5556
@Transactional
5657
@SpringBootTest
@@ -204,6 +205,30 @@ void findTop2ByWithSort() {
204205
assertThat(resultDesc).containsExactly(user2, user1);
205206
}
206207

208+
@Test
209+
void findTop2ByWithTypedSort() {
210+
211+
var user0 = new User();
212+
user0.setLastname("lastname-0");
213+
214+
var user1 = new User();
215+
user1.setLastname("lastname-1");
216+
217+
var user2 = new User();
218+
user2.setLastname("lastname-2");
219+
220+
// we deliberately save the items in reverse
221+
repository.saveAll(Arrays.asList(user2, user1, user0));
222+
223+
var resultAsc = repository.findTop2By(Sort.by(ASC, User::getLastname));
224+
225+
assertThat(resultAsc).containsExactly(user0, user1);
226+
227+
var resultDesc = repository.findTop2By(Sort.by(Sort.Order.desc(User::getLastname)));
228+
229+
assertThat(resultDesc).containsExactly(user2, user1);
230+
}
231+
207232
@Test
208233
void findByFirstnameOrLastnameUsingSpEL() {
209234

jpa/example/src/test/java/example/springdata/jpa/simple/SpecificationTests.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2025 the original author or authors.
2+
* Copyright 2025-present the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,6 +16,7 @@
1616
package example.springdata.jpa.simple;
1717

1818
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.data.jpa.criteria.Expressions.*;
1920

2021
import jakarta.persistence.EntityManager;
2122

@@ -85,4 +86,27 @@ void updateWithSpecification() {
8586
assertThat(result).extracting(User::getLastname).containsOnly("Brown");
8687
}
8788

89+
@Test
90+
void updateWithTypedSpecification() {
91+
92+
PredicateSpecification<User> isFirstName = (from, cb) -> cb.equal(path(from, User::getFirstname), "Dave");
93+
PredicateSpecification<User> isLastName = (from, cb) -> cb.equal(path(from, User::getLastname), "Matthews");
94+
95+
UpdateSpecification<User> specification = UpdateSpecification.<User> update((root, update, criteriaBuilder) -> {
96+
update.set(path(root, User::getFirstname), "Dan");
97+
update.set(path(root, User::getLastname), "Brown");
98+
}).where(isFirstName.and(isLastName));
99+
100+
assertThat(repository.update(specification)).isEqualTo(1);
101+
entityManager.clear();
102+
103+
assertThat(repository.findAll(isFirstName)).isEmpty();
104+
105+
List<User> result = repository.findAll((from, cb) -> cb.equal(path(from, User::getFirstname), "Dan"));
106+
107+
assertThat(result).hasSize(1);
108+
assertThat(result).extracting(User::getFirstname).containsOnly("Dan");
109+
assertThat(result).extracting(User::getLastname).containsOnly("Brown");
110+
}
111+
88112
}

0 commit comments

Comments
 (0)