These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Help putting together ICriteria plz
PostPosted: Mon Mar 09, 2009 4:03 pm 
Newbie

Joined: Thu Feb 26, 2009 4:36 pm
Posts: 6
NHibernate 1.2
SQL Server 2005
.Net 3.5


I'm trying to find all entities that contain a child entity with a specific ID. The catch is that I want to load up the child entities in the same call.

My current solution.

Code:
return this.session.CreateCriteria(typeof(pARENT))
            .SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer())
            .CreateCriteria("children", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
               .Add(Expression.IdEq(chID))
               .List<Parent>();



This returns all of the parents that have a child ID, but it's not loading up all of the children. Instead it's only loading up the child with the ID that we're searching with.

Any ideas on how to do this? In general whats the best way to find parent objects based upon the child objects ID's?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 6:21 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try and add

Code:
            .SetFetchMode( "children", FetchMode.Join );

before you create the subcriteria. If that's still not working, use an alias:


Code:
this.session.CreateCriteria(typeof(pARENT),"p")
            .SetFetchMode("p.children", FetchMode.Join)
            .SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer())
            .CreateCriteria("p.children" )
               .Add(Expression.IdEq(chID))
               .List<Parent>();

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 10:59 am 
Newbie

Joined: Thu Feb 26, 2009 4:36 pm
Posts: 6
What I ended up doing to solve the problem is this:

Code:
return this.session.CreateCriteria(typeof(pARENT))
            .SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer())
            .CreateCriteria("children")
               .Add(Expression.IdEq(chID))
               .List<Parent>();



Note the lack of "NHibernate.SqlCommand.JoinType.LeftOuterJoin" in the CreateCriteria call.


My understanding is that normally you use this to pull in an entity and all of it's children in a single call. It has to be LeftOuterJoin otherwise entities with no children will not be included.

Why was this exhibiting the behavior I was witnessing before? A lot of times it seems like there's some magic that NHibernate is doing that I'm not aware of so I'd really like someone to explain this to me.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.