浏览 6943 次
锁定老帖子 主题:一个关于many-to-many的问题
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2005-06-17
user类role类,及映射文件如下 import java.util.*; public class User implements java.io.Serializable { private int id; private String username; private Set SetOfUserRole; private void setId (int id) { this.id = id; } public int getId() { return id; } public void setUsername(String name) { this.username = name; } public String getUsername() { return username(); } public Set getSetOfUserRole () { return this.SetOfUserRole; } public void setSetOfUserRole (Set SetOfUserRole) { this.SetOfUserRole = SetOfUserRole; } } public class Role implements java.io.Serializable { private int id; private String rolename; private Set SetOfUserRole; private void setId (int id) { this.id = id; } public int getId() { return id; } public void setRolename(String name) { this.rolename = name; } public String getRolename() { return rolename(); } public Set getSetOfUserRole () { return this.SetOfUserRole; } public void setSetOfUserRole (Set SetOfUserRole) { this.SetOfUserRole = SetOfUserRole; } } <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- Auto-generated mapping file from the hibernate.org cfg2hbm engine --> <class name="User" table="user" schema="dbo" lazy="false" > <id name="Id" type="int" > <column name="id" not-null="true" unique="true" /> <generator class="identity" /> </id> <property name="username" type="java.lang.String" > <column name="user_name" length="20"/> </property> <set name="SetOfUserrole" table="user_role" lazy="true" inverse="false" cascade="save-update" > <key> <column name="user_id" length="10" /> </key> <many-to-many class="Role" column="role_id" outer-join="auto" /> </set> </class> </hibernate-mapping> <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- Auto-generated mapping file from the hibernate.org cfg2hbm engine --> <class name=Role" table="role" schema="dbo" lazy="false" > <id name="Id" type="int" > <column name="id" not-null="true" unique="true" /> <generator class="identity" /> </id> <property name="username" type="java.lang.String" > <column name="role_name" length="20"/> </property> <set name="SetOfUserrole" table="user_role" lazy="true" inverse="false" cascade="save-update" > <key> <column name="role_id" length="10" /> </key> <many-to-many class="User" column="user_id" outer-join="auto" /> </set> </class> </hibernate-mapping> 我想查找roleid为1,2,3的用户,并且按用户名排序,如何用hql查询? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2005-06-17
select user
from User as user left join user.SetOfUserRole as role where role.id in (1,2,3) order by user.username |
|
返回顶楼 | |
发表时间:2005-06-17
还是有问题,搜索出的是list.结果不能用User类了,数据里多了role的字段。
|
|
返回顶楼 | |
发表时间:2005-06-17
select user from User as user 的select user 没有省略吧。
|
|
返回顶楼 | |
发表时间:2005-06-20
是我的错,没加select user
|
|
返回顶楼 | |
发表时间:2005-06-21
这样一次查询了三个表user,role,user_role,能不能只查询两个表,user,user_role
|
|
返回顶楼 | |
发表时间:2005-06-22
Ewaves 写道 这样一次查询了三个表user,role,user_role,能不能只查询两个表,user,user_role
应该不行,在hibernate里面,只认对象,不认表,user_role这个对象是不存在的,他是一种关系。我是没有用过,除非你用JDBC写。 |
|
返回顶楼 | |