Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring data jpa Spring JPA projections可以有集合吗?_Spring Data Jpa_Projection - Fatal编程技术网

Spring data jpa Spring JPA projections可以有集合吗?

Spring data jpa Spring JPA projections可以有集合吗?,spring-data-jpa,projection,Spring Data Jpa,Projection,我有一个客户实体,我只想从中选择几个字段及其关联的CustomerAddress。我定义了一个Spring数据JPA投影接口,如下所示: public interface CustomerWithAddresses { Integer getId(); String getFirstName(); String getLastName(); String getBrandCode(); String getCustomerNumber(); Set

我有一个客户实体,我只想从中选择几个字段及其关联的CustomerAddress。我定义了一个Spring数据JPA投影接口,如下所示:

public interface CustomerWithAddresses {
    Integer getId();
    String getFirstName();
    String getLastName();
    String getBrandCode();
    String getCustomerNumber();
    Set<CustomerAddress> getCustomerAddresses();
}
对于具有多个CustomerAddress的客户,我不断收到UnuniquereSultException。投影是否必须具有平面结构,即它们不像真实实体那样支持集合?

您有
设置GetCustomerAddresss()这是X对多关系。当spring数据为CustomerWithAddresses选择时,它会在结果集中加入N个记录(N—id=id的CustomerWithAddresses的CustomerAddress数量)。您可以检查是否将CustomerWithAddresses更改为CustomerWithAddresses列表

List<CustomerWithAddresses> findCustomerWithAddressesById(@Param("id") Integer id);
并使用您的查询

2) 使用@Query

@Query("select adr from CustomerWithAddressesEntity adr where adr.id=:id")
CustomerWithAddresses findCustomerWithAddressesById(@Param("id") Integer id);
@Value("#{target.id}")
Integer getId();
@Query("select adr from CustomerWithAddressesEntity adr where adr.id=:id")
CustomerWithAddresses findCustomerWithAddressesById(@Param("id") Integer id);