[英]How to determine if two lists share any objects with Hibernate
我有我創建的試圖找到所有這些例子的SQL查詢REPORT
有一個相連的S REPORT_PERMISSION
物體的一個USER_GROUP
s表示當前用戶也有。 因此,有許多REPORT_PERMISSION
對象將一個報表綁定到一個組,一個用戶可以有多個組,只有其中一個必須匹配才能允許用戶查看該報表。 我只是不確定如何在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';
它應該是這樣的:
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());
映射示例(在hql中使用的名稱,在映射中使用fileds的名稱):
rep.reportPermissions-報表中的集合<ReportPermission> reportPermissions;
per.userGroups-ReportPermission中的Collection <UserGroup> userGroups;
gr.users-用戶組中的集合<用戶>用戶;
u.id-@Id長ID; 在用戶類別中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.