Skip to content

Commit e066a60

Browse files
authored
Merge pull request #18086 from Michaelin007/nestprojection
https://jira.baeldung.com/browse/BAEL-8561
2 parents 8f1d08f + 9f33d86 commit e066a60

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

persistence-modules/spring-data-jpa-simple/src/main/java/com/baeldung/jpa/projection/repository/AddressRepository.java

+12
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@
22

33
import java.util.List;
44

5+
import com.baeldung.jpa.projection.view.AddressDto;
6+
import org.springframework.data.jpa.repository.Query;
57
import org.springframework.data.repository.Repository;
68

79
import com.baeldung.jpa.projection.model.Address;
810
import com.baeldung.jpa.projection.view.AddressView;
11+
import org.springframework.data.repository.query.Param;
912

1013
public interface AddressRepository extends Repository<Address, Long> {
1114
List<AddressView> getAddressByState(String state);
15+
16+
@Query("SELECT c.zipCode as zipCode, c.person as person FROM Address c WHERE c.state = :state")
17+
List<AddressView> getViewAddressByState(@Param("state") String state);
18+
19+
@Query("SELECT new com.baeldung.jpa.projection.view.AddressDto(a.zipCode," +
20+
"new com.baeldung.jpa.projection.view.PersonDto(p.firstName, p.lastName)) " +
21+
"FROM Address a JOIN a.person p WHERE a.state = :state")
22+
List<AddressDto> findAddressByState(@Param("state") String state);
23+
1224
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.jpa.projection.view;
2+
3+
public class AddressDto {
4+
5+
private final String zipCode;
6+
private final PersonDto person;
7+
8+
public AddressDto(String zipCode, PersonDto person) {
9+
this.zipCode = zipCode;
10+
this.person = person;
11+
}
12+
13+
public String getZipCode() {
14+
return zipCode;
15+
}
16+
17+
public PersonDto getPerson() {
18+
return person;
19+
}
20+
}

persistence-modules/spring-data-jpa-simple/src/test/java/com/baeldung/jpa/projection/JpaProjectionIntegrationTest.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
import java.util.Arrays;
77
import java.util.List;
88

9+
import com.baeldung.jpa.projection.view.AddressDto;
910
import org.junit.jupiter.api.Test;
1011
import org.springframework.beans.factory.annotation.Autowired;
1112
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
1213
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
1314
import org.springframework.test.context.jdbc.Sql;
14-
1515
import com.baeldung.jpa.projection.model.Person;
1616
import com.baeldung.jpa.projection.repository.AddressRepository;
1717
import com.baeldung.jpa.projection.repository.PersonRepository;
@@ -40,6 +40,16 @@ void whenUsingClosedProjections_thenViewWithRequiredPropertiesIsReturned() {
4040
assertThat(personView.getLastName()).isEqualTo("Doe");
4141
}
4242

43+
@Test
44+
void whenUsingCustomQueryForNestedProjection_thenViewWithRequiredPropertiesIsReturned() {
45+
AddressView addressView = addressRepository.getViewAddressByState("CA").get(0);
46+
assertThat(addressView.getZipCode()).isEqualTo("90001");
47+
48+
PersonView personView = addressView.getPerson();
49+
assertThat(personView.getFirstName()).isEqualTo("John");
50+
assertThat(personView.getLastName()).isEqualTo("Doe");
51+
}
52+
4353
@Test
4454
void whenUsingOpenProjections_thenViewWithRequiredPropertiesIsReturned() {
4555
PersonView personView = personRepository.findByLastName("Doe");
@@ -70,4 +80,15 @@ void whenUsingDynamicProjections_thenObjectWithRequiredPropertiesIsReturned() {
7080
assertThat(personView.getFirstName()).isEqualTo("John");
7181
assertThat(personDto.firstName()).isEqualTo("John");
7282
}
83+
84+
@Test
85+
void whenUsingDTOProjection_thenCorrectResultIsReturned() {
86+
List<AddressDto> addresses = addressRepository.findAddressByState("CA");
87+
AddressDto address = addresses.get(0);
88+
assertThat(address.getZipCode()).isEqualTo("90001");
89+
90+
PersonDto person = address.getPerson();
91+
assertThat(person.firstName()).isEqualTo("John");
92+
assertThat(person.lastName()).isEqualTo("Doe");
93+
}
7394
}

0 commit comments

Comments
 (0)