简体   繁体   中英

How to determine if two lists share any objects with Hibernate

I have this example SQL query that I created that tries to find all REPORT s that have an associated REPORT_PERMISSION object with one of the USER_GROUP s that the current user also has. So there are many REPORT_PERMISSION objects that tie a report to a group, and a user can have many groups, just one of those have to match up to allow the user to view the report. I'm just not sure how to write this in HQL

SELECT * FROM REPORT r
JOIN REPORT_PERMISSION rp 
    on r.id = rp.report_id and rp.user_group_id in 
        (SELECT l.user_group_id FROM USER_GROUP_LINK l where l.user_id = 2)
where r.type = 'GENERAL';

it should be something like :

Query reportQuery = entityManager.createQuery
   ("select distinct rep from Report rep  
                         join rep.reportPermissions per 
                         join per.userGroups gr 
                         join gr.users u 
                         where u.id = :userId and rep.type = 'GENERAL'");
reportQuery.setParameter("userId" , user.getId());

example mapping (names that are used in hql , and fileds name in mappings ) :

rep.reportPermissions -- Collection < ReportPermission> reportPermissions in Report;

per.userGroups -- Collection < UserGroup> userGroups in ReportPermission;

gr.users -- Collection < User> users in UserGroup;

u.id -- @Id Long id; in user class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM