Hibernate Criteria Query for Self Referencing Entity; incorrect result
I have a self-referencing entity, Department
, which can have child Departments
(n-level deep). Hibernate Criteria Query seems to be getting confused. It creates the tree correctly in that all parents have their correct children, however it also arbitrarily puts children directly under grand-parents, so a grand-parent ends up having it's children (which is correct) and the children's children (incorrect) directly under it.
There is a top level entity Organization
which can have n LoB
entities under it. Lob stands for Line of Business. Under each LoB
there is a hierarchy of Department
entities.
Organization:
public class Organization {
...
private Set<Lob> lobs = new HashSet<Lob>();
@OneToMany(mappedBy = "organization", cascade = { CascadeType.PERSIST,
CascadeType.MERGE, CascadeType.REMOVE })
public Set<Lob> getLobs() {
return lobs;
}
public void setLobs(Set<Lob> lobs) {
this.lobs = lobs;
}
}
LOB:
public class Lob {
...
private Long id;
private Organization organization;
private Set<Department> departments = new HashSet<Department>();
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ORG_ID", nullable = false)
public Organization getOrganization() {
return organization;
}
public void setOrganization(Organization organization) {
this.organization = organization;
}
@OneToMany(mappedBy = "lob", cascade = { CascadeType.PERSIST,
CascadeType.MERGE, CascadeType.REMOVE })
public Set<Department> getDepartments() {
return departments;
}
public void setDepartments(Set<Department> departments) {
this.departments = departments;
}
}
Department:
public class Department {
...
private Set<Department> children = new HashSet<Department>();
private Department parent;
private Lob lob;
@OneToMany(mappedBy = "parent")
public Set<Department> getChildren() {
return children;
}
public void setChildren(Set<Department> children) {
this.children = children;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_ID")
public Department getParent() {
return parent;
}
public void setParent(Department parent) {
this.parent = parent;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "LOB_ID")
public Lob getLob() {
return lob;
}
public void setLob(Lob lob) {
this.lob = lob;
}
}
Hibernate Criteria Query:
Session session = (Session) getEntityManager().getDelegate();
Criteria crit = session.createCriteria(Organization.class);
// Get the whole Org tree
org = (Organization) crit.createAlias("lobs", "l", Criteria.LEFT_JOIN)
.setFetchMode("l", FetchMode.JOIN)
.createAlias("l.departments", "d", Criteria.LEFT_JOIN)
.setFetchMode("d", FetchMode.JOIN)
.createAlias("d.children", "dc", Criteria.LEFT_JOIN)
.setFetchMode("dc", FetchMode.JOIN)
.add(Restrictions.eq("id", orgId))
.uniqueResult();
I'm not sure if I have mapped the self-referencing association right. Any help will be greatly appreciated.
you have mapped the Departments set that way that all Departments which reference the Lob are in the collection but the whole tree references the same Lob not only the top part. So you have to add that restriction to the set mapping. Using hibernate it would be
<set name="departments" where="parent_id IS NULL">
<key column="LOB_ID" />
<one-to-many class="Department" />
</set>
Maybe someone else can edit to add the corresponding annotations
链接地址: http://www.djcxy.com/p/37078.html上一篇: Hibernate Criteria多对多的同一个对象
下一篇: Hibernate Criteria Query for Self Referencing Entity; 错误的结果