Spring数据子属性上的JPA JPQL查询

Spring数据子属性上的JPA JPQL查询,jpa,spring-data-jpa,Jpa,Spring Data Jpa,我使用的是spring数据jpa。当查询具有子对象属性的父对象时,我希望父对象具有聚合的子对象。我在用户和电话之间有一对多关系。只是输入部分代码 @Query(select u from User u inner join u.phone ph where ph.active=:active) Page<User> getAllUsers(@Param("active") int active); @Entity User{ @OneToMany(fetch=FetchTy

我使用的是spring数据jpa。当查询具有子对象属性的父对象时,我希望父对象具有聚合的子对象。我在用户和电话之间有一对多关系。只是输入部分代码

@Query(select u from User u inner join u.phone ph where ph.active=:active)
Page<User> getAllUsers(@Param("active") int active);

@Entity
User{  
  @OneToMany(fetch=FetchType.LAZY)
  List<Phone> phone;
}

@Entity
Phone{
  @ManyToOne
  User user;
}
我的查询基于活动电话数量返回多个用户对象。我希望列表中有一个用户对象和所有聚合电话对象作为用户对象的一部分。是我的假设错了还是我做错了什么?

试试:

@Query(select distinct u from User u inner join u.phone ph where ph.active=:active)
Page<User> getAllUsers(@Param("active") int active);

如果添加DISTINCT,会发生什么情况:从用户u中选择DISTINCT u@罗伯特尼斯特罗,谢谢你解决了多目标问题,现在我只有一个。我想知道这是解决方法还是真正的解决方案?如果我想预取手机?那么我会使用JPA EntityGraph的through-Spring数据JPA@EntityGraph注释