日期:2014-05-20  浏览次数:20718 次

求高手来帮小弟解决一个HQL的问题
@Entity
@Table(name = "T_User")
public class User
{
private int id;
private String userCode;
private String userName;
private Set<Role> roles = new HashSet<Role>();
private.......
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE },fetch=FetchType.lazy)
@JoinTable(name = "T_User_Role", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = { @JoinColumn(name = "roleId") })
public Set<Role> getRoles() {
return roles;
}

}

@Entity
@Table(name="t_Role")
public class Role
{
private int id;
private String roleCode;
private String roleName;
private Set<User> users = new HashSet<User>();
......
@ManyToMany(mappedBy="roles",fetch=FetchType.LAZY)
public Set<User> getUsers() {
return users;
}
}
表如下t_role 中间表 t_user_role
+----+------------+ +--------+--------+
----------+-------- | userId | roleId |
| id | roleCode| roleName| remark +--------+--------+
+----+------------+ | 1 | 1 |
+----+------------+ | 2 | 2 |
----------+-------- | 2 | 4 |
| 1 | dfdfdf| NULL | NULL | 3 | 5 |
| 2 | dfdfd| NULL | NULL +--------+--------+
| 3 | NULL |fg
| 4 | NULL |fg
| 5 | NULL |fg
+----+------------+

t_user表
+----------+------------------+-----------+
| id | roleCode| roleName| description |
+----+------------+---------+-------------+-
| 1 | NULL | NULL | NULL | NULL
| 2 | NULL | NULL | NULL | NULL
| 3 | NULL | NULL | NULL | NULL
| 4 | NULL | NULL | NULL | NULL
| 5 | NULL | NULL | NULL | NULL
| 6 | NULL | NULL | NULL | NULL
| 7 | NULL | NULL | NULL | NULL
| 8 | NULL | NULL | NULL | NULL
| 9 | NULL | NULL | NULL | NULL
| 10 | NULL | NULL | NULL | NULL
 | 11 | NULL | NULL | NULL | NULL
| 12 | NULL | NULL | NULL | NULL
| 13 | NULL | NULL | NULL | NULL
| 14 | NULL | NULL | NULL | NULL
| 15 | NULL | NULL | NULL | NULL
| 16 | NULL | NULL | NULL | NULL
已知user类的ID=1 ,HQL怎么写与user类的ID=1无联系(无关联)的role记录?

------解决方案--------------------
探讨

select * from t_role t where t.id in (select roleId from t_user_role r where r.userId<>1)

------解决方案--------------------
from role r where r.id not in ( select u.roles.id from user u where u.id =1 )

LZ试试?

不知道行不行 呵呵 这个 机器上没环境
------解决方案--------------------
什么叫不支持,牛的很了,现在hql,