Can Spring JPA projects have collections?

I have a Customer object from which I only want to select a few fields and the associated CustomerAddresses. I defined the Spring Data JPA projection interface as follows:

public interface CustomerWithAddresses {
    Integer getId();
    String getFirstName();
    String getLastName();
    String getBrandCode();
    String getCustomerNumber();
    Set<CustomerAddress> getCustomerAddresses();
}

But from my repository method:

CustomerWithAddresses findCustomerWithAddressesById(@Param("id") Integer id);

I keep getting NonUniqueResultException for customers with multiple CustomerAddresses. Should the projections have a flat structure, i.e. Do they not support collections in the same way as true entities?

+6
source share
1 answer

Set<CustomerAddress> getCustomerAddresses(); X-to-Many. spring CustomerWithAddresses, , N- (N - CustomerAddress CustomerWithAddresses id = id). , CustomerWithAddresses CustomerWithAddresses.

List<CustomerWithAddresses> findCustomerWithAddressesById(@Param("id") Integer id);

data sping data gropu, , id .

:

1) CustomerWithAddresses

@Value("#{target.id}")
Integer getId();

2) @Query

@Query("select adr from CustomerWithAddressesEntity adr where adr.id=:id")
CustomerWithAddresses findCustomerWithAddressesById(@Param("id") Integer id);
0

All Articles