Nhibernate 2 linq eager load

I start with nHibernate and have a simple example that I cannot work as I would like.

I have two model objects (Blog and posts), and I would like to load them all in one request for one scenario. I want lazy loading in other cases.

I naively thought I could write something like this:

var blogs = session.Linq<Blog>().Expand("Posts"); 

But this will give me a blog instance for each post, rather than adding blog entries.

I know I'm doing something stupid. Can someone point out what it is? Do I need to bind post and blog objects in my linq request?

Code and Mappings:

 public class Blog { public Blog() { Posts = new HashSet<Post>(); } public virtual long Identifier { get; set; } public virtual string Name { get; set; } public virtual ICollection<Post> Posts { get; set; } public virtual Post AddPost(Post post) { post.Blog = this; Posts.Add(post); return post; } } public class Post { public virtual long Identifier { get; set; } public virtual string Name { get; set; } public virtual Blog Blog { get; set; } } <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="nhibEx" namespace="nhibEx"> <class name="Blog" lazy="true"> <id name="Identifier"> <generator class="native" /> </id> <property name="Name" not-null="true" length="100"/> <set name="Posts" inverse="true" cascade="save-update" lazy="true"> <key column="BlogIdentifier" foreign-key="fk_Post_Blog"/> <one-to-many class="Post"/> </set> </class> </hibernate-mapping> <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="nhibEx" namespace="nhibEx"> <class name="Post" lazy="true"> <id name="Identifier"> <generator class="native" /> </id> <property name="Name" not-null="true" length="255"/> <many-to-one name="Blog" column="BlogIdentifier" class="Blog" /> </class> </hibernate-mapping> 
+4
source share
3 answers

The difference is what you need ...

Edit: When this does not work: make a selection after the list. I do not know why NHibernate loads the same number of objects as the number of database records returned, and does not perform automatic allocation. This problem / function is not specific to Linq, but will also occur when using criteria or hql.

 session.Linq<Blog>().Expand("Posts").ToList().Distinct(); 

Sometimes it can be more efficient to execute 2 queries (separately or using multiquery / future) than to execute one query with a left outer join.

+1
source

After searching for other forums (maybe I should do it right first!) I use this solution:

 var blogs = session.Linq<Blog>(); blogs.QueryOptions.RegisterCustomAction( criteria => criteria.SetResultTransformer(new DistinctRootEntityResultTransformer())); var results = blogs.Expand("Posts"); 

I did not want to use Distinct as I wanted to return IQueryable

It seems to need work. I just need to know the theory :)

http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries.aspx

+4
source

We have the same problem. It seems to me that linq is always in boot mode. Therefore you do not need to do exapnd. However, this is very bad. Have you tried contacting the HN guys in your google group?

-3
source

All Articles