我有一个具有一个托凡关系(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)