java Hibernate 在多对多列表上创建别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9853310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate create alias on many to many list
提问by Ahmet DAL
I have four class; UserGroup, UserAccount, Role, UserGroupRoleRelation and my db is IBM DB2
我有四节课;UserGroup、UserAccount、Role、UserGroupRoleRelation 和我的数据库是 IBM DB2
@Entity
@Table(name = "USER_GROUP")
public class UserGroup implements Serializable {
@Id
@Column(name = "USER_GROUP_ID")
@GeneratedValue
private Long id;
......
..
@OneToMany(mappedBy = "userGroup", cascade = CascadeType.ALL, orphanRemoval = true)
private List<UserGroupRoleRelation> userAccountsRole = new ArrayList<UserGroupRoleRelation>();
}
@Entity
@Table(name = "ROLE")
public class Role implements Serializable {
@Id
@Column(name = "ROLE_ID")
@GeneratedValue
private Long id;
......
@OneToMany(mappedBy = "role")
private List<UserGroupRoleRelation> userAccountInGroup = new ArrayList<UserGroupRoleRelation>();
}
@Entity
@Table(name = "USER_GROUP_ROLE_LINE", uniqueConstraints = @UniqueConstraint(columnNames = { "ROLE_ID", "USER_GROUP_ID" }))
public class UserGroupRoleRelation {
@Id
@GeneratedValue
@Column(name = "RELATION_ID")
private Long relationId;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "USER_ACCOUNT_USER_GROUP_ROLE_LINE", joinColumns = { @JoinColumn(name = "RELATION_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") }, uniqueConstraints = @UniqueConstraint(columnNames = { "USER_ID", "RELATION_ID" }))
private List<UserAccount> userAccountList = new ArrayList<UserAccount>();
@ManyToOne
@JoinColumn(name = "USER_GROUP_ID")
private UserGroup userGroup;
@ManyToOne
@JoinColumn(name = "ROLE_ID")
private Role role;
}
@Entity
@Table(name = "USER_ACCOUNT")
public class UserAccount implements Serializable {
@Id
@Column(name = "USER_ID")
@GeneratedValue
private Long id;
.....
@ManyToMany(mappedBy = "userAccountList", cascade = CascadeType.ALL)
private List<UserGroupRoleRelation> rolesInGroup = new ArrayList<UserGroupRoleRelation>();
}
I wanna find usergroups of a useraccount and i prepared a method with criteria. its like;
我想找到一个用户帐户的用户组,我准备了一个带有标准的方法。就像是;
@Override
@Transactional
public List<UserGroup> findUserGroupOf(UserAccount userAccount) {
Criteria criteria = getSession().createCriteria(UserGroup.class);
criteria.createAlias("userAccountsRole", "userAccountsRole");
criteria.add(Restrictions.eq("userAccountsRole.userAccountList", userAccount));
return criteria.list();
}
But when i try to get result of that method, DB2 gives to me DB2 SQL Error: SQLCODE=-313, SQLSTATE=07004, SQLERRMC=null, DRIVER=3.63.75
但是当我尝试获得该方法的结果时,DB2 给了我 DB2 SQL Error: SQLCODE=-313, SQLSTATE=07004, SQLERRMC=null, DRIVER=3.63.75
Probably its about creating alias on many to many relation. I dont know what should i do to create alias on many to many. How can I get result of that function?
可能是关于在多对多关系上创建别名。我不知道我应该怎么做才能在多对多上创建别名。我怎样才能得到那个函数的结果?
Thank
感谢
回答by Ahmet DAL
@Override
@Transactional
public List<UserGroup> findUserGroupOf(UserAccount userAccount) {
Criteria criteria = getSession().createCriteria(UserGroup.class);
criteria.createAlias("userAccountsRole", "userAccountsRole");
criteria.createAlias("userAccountsRole.userAccountList", "userAccountList");
criteria.add(Restrictions.eq("userAccountList.id", userAccount.getId()));
return criteria.list();
}
It works for me. I mean criteria on "id". But I don't understand why I cant check equality on object instead of id when there is ManyToMany list
这个对我有用。我的意思是关于“id”的标准。但是我不明白为什么当有 ManyToMany 列表时我不能检查对象而不是 id 的相等性
回答by PVR
It is not of creating alias. You are passing an object to hibernate on which it can not make any criteria. You need to create bidirectional mapping for that.Or else if you your requirement is just to fetch the the list of UserAccountList of particular UserGroup class you can follow the below code.
这不是创建别名。您正在将一个对象传递给休眠,它无法根据它制定任何标准。您需要为此创建双向映射。否则,如果您的要求只是获取特定 UserGroup 类的 UserAccountList 列表,则可以按照以下代码进行操作。
@Override
@Transactional
public List<UserGroup> findUserGroupOf(long userGroupId) {
Criteria criteria = getSession().createCriteria(UserGroup.class);
criteria.add(Restrictions.eq("id",userGroupId));
criteria.createAlias("userAccountsRole", "uar");
criteria.setFetchMode("uar.userAccountList",FetchMode.JOIN);
return criteria.list();
}