Looking for some expertise before I declare the NHibernate framework broken or myself crazy!
I'm trying to eagerly load a self-referencing tree with NHibernate and can successfully load most children lists, however I can't seem to get it to work with leaf nodes. My query is:
"select p from Proposal p join fetch p.Structures s join fetch s.theChildrenList c " +
"where p._persistenceId = :p1 and s.theProposal = :p1 and c.theProposal = :p1")
.SetParameter("p1", aProposalId)
.SetResultTransformer(new DistinctRootEntityResultTransformer()).
UniqueResult<Proposal>();
This will return all the records correctly, but does not properly populate the childrenList of leaf nodes (which should be by definition empty). Instead when I get a proxy and upon searching the tree, thousands of pointless zero record queries.
I've tried: 1. using left join rather than join 2. Making the children's list not-lazy and fetch="join" and removing the hql (as a proof of concept, performance downside is unacceptable elsewhere) 3. Messing with the not-found property that I saw someone use in the hibernate forums
All of which give me the same results... one large query with all the data (good) and thousands of small queries which return no data (bad). Any ideas?
I'm using NHibernate 2.2 and here is the relevant part of the mappings file for reference:
<many-to-one name="theParentStructure"
column="PARENT_STRUCTURE_ID"
class="Structure"
access="field"
update="false"
insert="false"
not-found="ignore"/>
<bag name="theChildrenList"
generic="true"
table="STRUCTURE"
access="field"
cascade="all-delete-orphan"
inverse="true" fetch="join" lazy="false"> <--Both with and without the last two properties
<key column="PARENT_STRUCTURE_ID" />
<one-to-many class="Structure"/>
</bag>
Any help would be appreciated!!