Reputation: 13038
I have created an query with criteria api that retrieves an entity by another linked entity:
public List<Booking> getBookingsByUser(User user) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Booking> createQuery = cb.createQuery(Booking.class);
Root<Booking> booking = createQuery.from(Booking.class);
Join<Booking, UsersProjects> join = booking.join(Booking_.userProject, JoinType.INNER);
createQuery.where(cb.equal(join.get(UsersProjects_.user), user));
createQuery.select(booking);
return em.createQuery(createQuery).getResultList();
}
This is working fine. But how to rewrite this to find entities by userId (Long)? Metamodel of User has User_.id (SingularAttribute).
User is also an Entity. And a "UsersProject" hast exactly one User and one Project.
Upvotes: 0
Views: 788
Reputation: 16273
Add one more join clause between UserProjects
and User
:
Join<Booking, UsersProjects> userProjectsJoin = booking.join(Booking_.userProject, JoinType.INNER);
Join<UsersProjects, User> userJoin = userProjectsJoin.join(UserProjects_.user);
createQuery.where(cb.equal(userJoin.get(User_.id), userId));
Upvotes: 1