小编lbd*_*d01的帖子

使用OneToMany的Spring Data Projection返回太多结果

我有一个具有一个托凡关系(ContactInfo)的JPA实体(人)。

@Entity
public class Person {
    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private String lastname;
    private String sshKey;
    @OneToMany(mappedBy = "personId")
    private List<ContactInfo> contactInfoList;
}

@Entity
public class ContactInfo {
    @Id
    @GeneratedValue
    private Integer id;
    private Integer personId;
    private String description;
}
Run Code Online (Sandbox Code Playgroud)

我已经定义了一个投影接口,其中包括这里描述的这种千丝万缕的关系。

public interface PersonProjection {
    Integer getId();
    String getName();
    String getLastname();
    List<ContactInfo> getContactInfoList();
}

public interface PersonRepository extends JpaRepository<Person,Integer> {
    List<PersonProjection> findAllProjectedBy();
}
Run Code Online (Sandbox Code Playgroud)

当我使用findAllProjectedBy检索数据时,结果包含太多行。看起来返回的数据是联接查询的结果,类似于:

select p.id, p.name, p.lastname, ci.id, ci.person_id, ci.description 
from …
Run Code Online (Sandbox Code Playgroud)

java jpa projection one-to-many spring-data

5
推荐指数
1
解决办法
4220
查看次数

标签 统计

java ×1

jpa ×1

one-to-many ×1

projection ×1

spring-data ×1