使用hibernate多对多关联关系的HQL查询语句

Daoimpl层 以角色和权限表为例
常见inner join
public List<Function> getRoleFunction(int rid) {
	return this.getHibernateTemplate().find("select f from Function as f inner join f.roles as r where r.roleid="+rid);
}


使用elements  网上找的,还不懂elements具体用法??
public List<Function> getRoleFunction(int rid) {
		return this.getHibernateTemplate().find("select elements(r.functions) from Role as r where r.roleid=2");
}



其中role.hbm.xml
 <set name="functions" table="tb_role_function" cascade="save-update" lazy="false">
		<key>
			<column name="role_id"></column>
		</key>
		<many-to-many class="Function" column="function_id"/>
		</set> 


function.hbm.xml
<set name="roles" table="tb_role_function" cascade="save-update" lazy="false">
		<key>
			<column name="function_id"></column>
		</key>
		<many-to-many class="Role" column="role_id"/>
		</set>

猜你喜欢

转载自422979391.iteye.com/blog/2237945