smile
smile

Reputation: 516

Spring data JPA JPQL query on child property

I am using spring-data jpa. When querying parent object with child object property,I was expecting parent with aggregated child objects.I have OneToMany relation between User and Phone. Just typing some part of code.

@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;
}

My query returns multiple User object based on active phone quantity. I was expecting one User object and all aggregated phone object in the list as part of User object. Is my assumption is wrong or am I doing something wrong?

Upvotes: 4

Views: 9655

Answers (1)

Robert Niestroj
Robert Niestroj

Reputation: 16131

Try:

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

Upvotes: 4

Related Questions