如何编写一个hql来获取没有权限的所有记录

时间:2013-02-04 16:43:46

标签: sql hibernate hql

我有三张桌子

Project
   id, name
ProjectAuthorization
   id,  project_id, user_id , permission
User
   id, name

Project.java

@OneToMany(mappedBy = "project", cascade = CascadeType.ALL)
private List<ProjectAuthorization> permissions;

User.java

@OneToMany(mappedBy = "user",cascade =CascadeType.ALL)
private List<ProjectAuthorization> permissions;

ProjectAuthorization.java

@ManyToOne()
private Project project;
@ManyToOne()
private User user;

我想获得当前用户没有任何权限的所有项目,如何通过hibernate查询编写它?

更新

像这样的hql:

select p from Project as p left join p.permissions as pa where pa.user.id!=:userid and p.id not in (select pa2.project.id from ProjectAuthorization as pa2 where pa2.user.id=:userid)");

1 个答案:

答案 0 :(得分:0)

例如,可以使用以下查询(也是有效的JPQL,不使用Hibernate特定的构造):

SELECT p
FROM Project p
WHERE p.id NOT IN
  (SELECT authorization.project.id
   FROM ProjectAuthorization authorization
   WHERE authorization.user = :currentUser)