Hibernate version:
3.2GA
Mapping documents:
What I have is a parent with a set of child objects beneath it. Each of these child objects has other objects in an object graph. What I'd like to do is for each parent object, I'd like to get some subset of the full collection of child objects using a fetch join (to do this in one hit from the database.)
For example:
DetachedCriteria c = DetachedCriteria.forClass(Individual.class);
c.createAlias("phenoHistories", "pah").setFetchMode("pah", FetchMode.JOIN);
c.createAlias("pah.phenoAttributes", "pa").setFetchMode("pa", FetchMode.JOIN);
c.createAlias("pa.phenoProjAttributeType", "ppat").setFetchMode("ppat", FetchMode.JOIN);
c.createAlias("ppat.phenoAttributeType", "pat").setFetchMode("pat", FetchMode.JOIN);
c.add(Restrictions.in("pat.attributeTypeId", phenoAttributeTypeIDs));
So, there are a few associations from the root object (I) to PAH->PA->PPAT->PAT, and a restriction on PAT.
That restriction on PAT restricts the list of Individuals that come out to the list that contain those PATs. It also restricts the PAH's the individual contains to the ones that have those PATs.
What I see happening is that the SQL generated select is exactly what I want to see. In my test case, there are 230 rows representing 84 individuals, and all of the relevant data for the graph. If an object graph was created containing exactly this data (and ONLY this data), I'd be happy.
Instead, when I visit the each individual and look at it's set of PAHs, the set is listed as uninitialized, despite the fact that I did a fetch join. Hibernate then initializes the set by retrieving all values for that set. However, I don't want to get all the items in the set, just the ones defined by the initial query.
The mapping of Individual to PAH:
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY, mappedBy="attributeHistoryId")
@JoinColumn(name="LSID", referencedColumnName="LSID", nullable = true)
public Set<PhenoAttributeHistory> getPhenoHistories() {
return this.phenoHistories;
}
There is no mapping from the other direction (is this a problem in a one way association?)
Is there any way to not retrive the entire collection in the object graph, and just work with that without the Lazy aspect kicking in? Should I be turning the LAZY aspect off? I know I don't want EAGER (and those left outer joins) on this association, but those seem to be the only options.
Any help is greatly appreciated.
|