Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

https://jira.baeldung.com/browse/BAEL-8561 #18086

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@

import java.util.List;

import com.baeldung.jpa.projection.view.AddressDto;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;

import com.baeldung.jpa.projection.model.Address;
import com.baeldung.jpa.projection.view.AddressView;
import org.springframework.data.repository.query.Param;

public interface AddressRepository extends Repository<Address, Long> {
List<AddressView> getAddressByState(String state);

@Query("SELECT c.zipCode as zipCode, c.person as person FROM Address c WHERE c.state = :state")
List<AddressView> getViewAddressByState(@Param("state") String state);

@Query("SELECT new com.baeldung.jpa.projection.view.AddressDto(a.zipCode," +
"new com.baeldung.jpa.projection.view.PersonDto(p.firstName, p.lastName)) " +
"FROM Address a JOIN a.person p WHERE a.state = :state")
List<AddressDto> findAddressByState(@Param("state") String state);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.baeldung.jpa.projection.view;

public class AddressDto {

private final String zipCode;
private final PersonDto person;

public AddressDto(String zipCode, PersonDto person) {
this.zipCode = zipCode;
this.person = person;
}

public String getZipCode() {
return zipCode;
}

public PersonDto getPerson() {
return person;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.jpa.show-sql=false
spring.jpa.show-sql=true
spring.jpa.defer-datasource-initialization=true
#MySql
#spring.datasource.url=jdbc:mysql://localhost:3306/baeldung
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import java.util.Arrays;
import java.util.List;

import com.baeldung.jpa.projection.view.AddressDto;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.jdbc.Sql;

import com.baeldung.jpa.projection.model.Person;
import com.baeldung.jpa.projection.repository.AddressRepository;
import com.baeldung.jpa.projection.repository.PersonRepository;
Expand Down Expand Up @@ -40,6 +40,16 @@ void whenUsingClosedProjections_thenViewWithRequiredPropertiesIsReturned() {
assertThat(personView.getLastName()).isEqualTo("Doe");
}

@Test
void whenUsingCustomQueryForNestedProjection_thenViewWithRequiredPropertiesIsReturned() {
AddressView addressView = addressRepository.getViewAddressByState("CA").get(0);
assertThat(addressView.getZipCode()).isEqualTo("90001");

PersonView personView = addressView.getPerson();
assertThat(personView.getFirstName()).isEqualTo("John");
assertThat(personView.getLastName()).isEqualTo("Doe");
}

@Test
void whenUsingOpenProjections_thenViewWithRequiredPropertiesIsReturned() {
PersonView personView = personRepository.findByLastName("Doe");
Expand Down Expand Up @@ -70,4 +80,17 @@ void whenUsingDynamicProjections_thenObjectWithRequiredPropertiesIsReturned() {
assertThat(personView.getFirstName()).isEqualTo("John");
assertThat(personDto.getFirstName()).isEqualTo("John");
}

@Test
void whenUsingDTOProjection_thenCorrectResultIsReturned() {
List<AddressDto> addresses = addressRepository.findAddressByState("CA");

AddressDto address = addresses.get(0);
assertThat(address.getZipCode()).isEqualTo("90001");

PersonDto person = address.getPerson();
assertThat(person.getFirstName()).isEqualTo("John");
assertThat(person.getLastName()).isEqualTo("Doe");
}

}