2017-07-18 78 views
6

Mam jednostkę Customer, z której chcę wybrać tylko kilka pól i powiązane z nimi CustomerAddresses. Mam zdefiniowany interfejs projekcji Wiosna danych WZP następująco:Czy wiosenne projekcje JPA mogą mieć kolekcje?

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

Ale z mojego repozytorium metody:

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

Wciąż dostaję NonUniqueResultException dla Klientów z wieloma CustomerAddresses. Czy projekcje muszą mieć płaską strukturę, tj. Nie obsługują kolekcji w taki sam sposób, jak robią to prawdziwe jednostki?

Odpowiedz

0

masz Set<CustomerAddress> getCustomerAddresses(); jest stosunek X do wielu. Gdy dane sprężynowe wybierają dla CustomerWithAddresses, dołącza się w zestawie wyników N-records (N - ilość CustomerAddress dla CustomerWithAddresses z id = id). Możesz sprawdzić, czy zmienisz CustomerWithAddresses na listę CustomerWithAddresses.

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

podczas korzystania z danych sping jednostka gropu pomnożyć doprowadzić do jednego elementu, gouped go jako identyfikatora ID to unikalny identyfikator.

można zrobić:

1) dodać do interfejsu CustomerWithAddresses

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

i używać zapytanie

2) używać @query

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