我通过Criteria API获取了多个数据。
我在实体中的关联映射是
public class UserGroupEntity{
private Set<UserEntity> users = new TreeSet<>();
private List<DocumentEntity> documents = new ArrayList<>();
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(
name="FG_DDA_User__UserGroup",
joinColumns={@JoinColumn(name="usergroup_id", nullable=false),},
inverseJoinColumns={@JoinColumn(name="user_id", nullable=false)})
public Set<UserEntity> getUsers() {
return users;
}
public void setUsers(Set<UserEntity> users) {
this.users = users;
}
/**
* @return the documents
*/
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(
name="FG_DDA_Document__UserGroup",
joinColumns={@JoinColumn(name="usergroup_id", nullable=false),},
inverseJoinColumns={@JoinColumn(name="document_id",nullable=false)})
public List<DocumentEntity> getDocuments() {
return documents;
}
}
我从标准中获取数据。代码是
Criterion criterian = Restrictions.idEq(id);
Criteria criteria = getSession().createCriteria(getPersistentClass());
criteria.add(criterian);
if(eagerFetchColumn != null){
for (String string : eagerFetchColumn) {
criteria.setFetchMode(string, FetchMode.JOIN);
}
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
}
List<UserGroupEntity> result = (List<UserGroupEntity>)criteria.list();
if (result.size() == 1) {
return result.iterator().next();
}
if (result.size() == 0) {
return null;
}
throw new RuntimeException("Found more than one object with the given ID, verify integrity of you data");
我正在获取UserGroupEntity的唯一数据,用户也是唯一的,因为它是Set。但文件是多个,因为它是列表,这是我的问题。我还可以通过列表中的多个加入来接收唯一的文档。
查询:
SELECT t_.id AS a,
t_.name AS b,
du_.usergroup_id AS c,
d_.id AS d,
d_.id AS e,
d_.activeVersion_id AS f,
d_.name AS g,
uu_.usergroup_id AS h,
u_.id AS i,
u_.id AS j,
u_.name AS k,
u_.password AS l,
u_.role_id AS m,
u_.toc_code AS n,
u_.email_id AS o,
u_.isValid AS p
FROM dbo.FG_DDA_UserGroup t_
LEFT OUTER JOIN dbo.FG_DDA_Document__UserGroup du_ ON t_.id=du_.usergroup_id
LEFT OUTER JOIN dbo.FG_DDA_Document d_ ON du_.document_id=d_.id
LEFT OUTER JOIN dbo.FG_DDA_User__UserGroup uu_ ON t_.id=uu_.usergroup_id
LEFT OUTER JOIN dbo.FG_DDA_User u_ ON uu_.user_id=u_.id
WHERE t_.id = ?