我有四节课; 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>();
}
我想找到一个用户帐户的用户组,并准备了一个带有条件的方法。就像是;
@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();
}
但是,当我尝试获取该方法的结果时,DB2给了我
DB2 SQL Error: SQLCODE=-313, SQLSTATE=07004, SQLERRMC=null, DRIVER=3.63.75
可能是关于在多对多关系上创建别名。我不知道我该怎么做才能在多对多上创建别名。如何获得该功能的结果?
谢谢
最佳答案
@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();
}
这个对我有用。我的意思是“ id”的标准。但是我不明白为什么在有ManyToMany列表时我不能检查对象的相等性而不是id