如有user表,role表,userrole表,3者之间的关系是,user表和role表通过userrole表多对多关联。
user类:
public class user implements java.io.Serializable {
private Integer userId;
private String userName;
private Set userrole= new HashSet(0);
//get和set省略
}
role类:
public role implements java.io.Serializable {
private Integer roleId;
private String roleName;
private Set userrole= new HashSet(0);
//get和set省略
}
userrole类:
public userrole implements java.io.Serializable {
private Integer userroleID;
private role role;
private user user;
//get和set省略
}
对应的xml(只捡关联部分):
user.hbm.xml:
<set name="userrole" inverse="true">
<key>
<column name="userId" not-null="true" />
</key>
<one-to-many class="mo.hibernate.map.userrole" />
</set>
role.hbm.xml:
<set name="userrole" inverse="true">
<key>
<column name="roleId" not-null="true" />
</key>
<one-to-many class="mo.hibernate.map.userrole" />
</set>
userrole.hbm.xml:
<many-to-one name="role" class="mo.hibernate.map.role" fetch="select">
<column name="roleId" not-null="true" />
</many-to-one>
<many-to-one name="user" class="mo.hibernate.map.user" fetch="select">
<column name="userId" not-null="true" />
</many-to-one>
问题是:如何用hql查找出角色为admin的用户的id和名字?
hql:select u.userId,u.userName from user u,userrole ur,role r where u.userID in(ur.user) and r.roleName='admin' and ur.userroleID in elements(r.userrole)
本例用到了in和in elements关键字,区别在于in elements用于set,in用于类对象。
分享到:
相关推荐
hql多表查询hql多表查询hql多表查询
HQL多表查询
Hibernate中的关联查询实际上生成的是数据库表连接查询的SQL语句
hql模糊查询hql模糊查询hql模糊查询hql模糊查询
hql查询多个实体类,类之间有外键关系,但是没有外键关联
Hibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询....
Hibernate 多对多HQL语句写法
Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此Hibernate将HQL查询方式立为官方推荐的标准查询方式,HQL查询...
NULL 博文链接:https://tanjunxiaoge.iteye.com/blog/1750865
HQL查询 帮助文档
HQL查询及语法,java web开发必备。
hibernate查询之HQLhibernate查询之HQLhibernate查询之HQL
HQL查询语言基础,HQL查询语言基础,HQL查询语言基础
HQL查询语言基础.
SSH笔记-HQL查询(基于位置参数查询、基于命名参数查询、分页查询、命名查询、投影查询报表查询、内/迫切内/左外/迫切左外连接查询)、QBC(QBC检索查询、统计查询、排序查询、HQL操作数据库)、本地SQL查询
Hql多结果集 有很细的解释 适合新手 值得下载
HQL是指定连接查询方式,而且显式指定了关联级别的查询策略
HQL快速入门的好资料,先看看此资料,在有手册就差不多了。
HQL语法入门学习HQL语法入门学习HQL语法入门学习HQL语法入门学习HQL语法入门学习HQL语法入门学习HQL语法入门学习HQL语法入门学习HQL语法入门学习HQL语法入门学习HQL语法入门学习HQL语法入门学习HQL语法入门学习HQL...