Reputation: 516
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
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