Skip to content

Commit fe88250

Browse files
committed
Add projection example using PredicateSpecification.
Closes #706
1 parent a7292e1 commit fe88250

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

jpa/example/src/main/java/example/springdata/jpa/projections/CustomerRepository.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2015-2021 the original author or authors.
2+
* Copyright 2025 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-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://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,
@@ -21,13 +21,15 @@
2121
import org.springframework.beans.factory.annotation.Value;
2222
import org.springframework.data.domain.Page;
2323
import org.springframework.data.domain.Pageable;
24+
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
2425
import org.springframework.data.jpa.repository.Query;
2526
import org.springframework.data.repository.CrudRepository;
2627

2728
/**
2829
* @author Oliver Gierke
30+
* @author Mark Paluch
2931
*/
30-
public interface CustomerRepository extends CrudRepository<Customer, Long> {
32+
public interface CustomerRepository extends CrudRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
3133

3234
/**
3335
* Uses a projection interface to indicate the fields to be returned. As the projection doesn't use any dynamic

jpa/example/src/test/java/example/springdata/jpa/projections/CustomerRepositoryIntegrationTest.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2015-2021 the original author or authors.
2+
* Copyright 2025 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-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://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,
@@ -37,8 +37,8 @@
3737
*
3838
* @author Oliver Gierke
3939
* @author Divya Srivastava
40+
* @author Mark Paluch
4041
*/
41-
4242
@SpringBootTest
4343
@Transactional
4444
class CustomerRepositoryIntegrationTest {
@@ -136,4 +136,14 @@ void supportsProjectionInCombinationWithPagination() {
136136
void appliesProjectionToOptional() {
137137
assertThat(customers.findOptionalProjectionByLastname("Beauford")).isPresent();
138138
}
139+
140+
@Test
141+
void projectsWithSpecification() {
142+
143+
var result = customers.findBy((query, cb) -> cb.equal(query.get("firstname"), "Dave"),
144+
it -> it.as(CustomerDto.class).all());
145+
146+
assertThat(result).hasSize(1);
147+
assertThat(result).extracting(CustomerDto::firstname).containsOnly("Dave");
148+
}
139149
}

0 commit comments

Comments
 (0)