有兩表Dept(單位)和Permission(權限),由hibernate維持多對多的關係
Dept
private Set<Permission> permissions;
<set name="permissions" lazy="true" cascade="none" table="dept_permission" outer-join="false">
<key column="permissions_id"/>
<many-to-many class="com.gdcy.bean.Permission" column="dept_id"/>
</set>
Permission
private Set<Dept> depts;
<set name="depts" lazy="true" cascade="none" table="dept_permissions" outer-join="false">
<key column="depts_id"/>
<many-to-many class="com.gdcy.bean.Dept" column="permission_id"/>
</set>
查詢具備某個權限的所有單位(關鍵,使用in elements)
select t from Dept t,Permission p where p.remark='權限' and p.id in elements(t.permissions)