JPQL and Join Table

谁说我不能喝 提交于 2019-12-02 22:52:46

Using JPQL it would be:

TypedQuery<Group> query = em.createQuery(
    "SELECT DISTINCT g FROM User u LEFT JOIN u.groupCollection g " +
    "WHERE u = :user", Group.class);
query.setParameter("user", user);
List<Group> = query.getResultsList();

where em is your EntityManager and user is the instance of the User class for which to load group list. If you only have the user id, change with:

TypedQuery<Group> query = em.createQuery(
    "SELECT DISTINCT g FROM User u LEFT JOIN u.groupCollection g " +
    "WHERE u.id = :user", Group.class);
query.setParameter("user", userId);

It would be better to use a Set or SortedSet (or maybe a List if the user can be in the same group more than once) instead of a Collection.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!