-->
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.  [ 7 posts ] 
Author Message
 Post subject: .Net 2.0 Generics and Lazy Collections
PostPosted: Tue Feb 21, 2006 4:25 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
I am using NHibernate on a .Net 2.0 project.

I have my mappings working fine and the data is persisting to the database. But, I want to use lazy loading. (At least, I *think* I do.) However, I cannot get it to work. When I load an object, all of the collections come with it. There is a deep chain of objects, (User > UserDetail > Role > RoleDetail > Permission > PermissionDetail) so I don't want to always load all of that data.

I have two guesses as to the problem.

My first guess is, because I'm using generics, whatever it is that makes lazy loading work fails because NHibernate doesn't understand generics.

My second guess is, if the generics aren't a problem, there is something wrong with the way I'm mapping my collections.

I'll wait to hear an answer for guess #1 before getting into my mapping details.

--Brian


Top
 Profile  
 
 Post subject: Lazy
PostPosted: Tue Feb 21, 2006 11:42 pm 
Regular
Regular

Joined: Tue Jan 03, 2006 7:21 am
Posts: 85
I had a similar problem earlier on and the reason turned out to be that I had not defined the properties of the class that was to be lazily loaded as virtual.
Please check that the class that are meant to be lazily loaded are marked lazy=true in the mapping document and that all the properties are marked virtual.


Top
 Profile  
 
 Post subject: Re: Lazy
PostPosted: Wed Feb 22, 2006 6:34 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
samujob wrote:
check that the class that are meant to be lazily loaded are marked lazy=true


Okay, I had to fix my class by added "lazy = true" to my child class's mapping.

So now my child class is marked lazy, my parent class's collection is marked lazy and all of the child class' properties are virtual.

But, it still doesn't work.

When I load a User, I still get the UserDetails collection populated and the rest of the objects below that.

I'm loading a User like this:

Code:
MyObject a = (MyObject) Session.Load(typeof(MyObject), long.Parse("5000"));


Is this wrong? Do I need to load it a special way to enable lazy loading?

--Brian


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 24, 2006 8:17 am 
Newbie

Joined: Fri Feb 24, 2006 8:15 am
Posts: 1
Have you tried to use ISession.Get() instead of ISession.Load() in order for lazy loading to work?

Best regards, jorgas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 24, 2006 12:56 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
jorgas wrote:
Have you tried to use ISession.Get() instead of ISession.Load() in order for lazy loading to work?


I tried this too, but it still loads everything.

The log from using Load() looks the same as using Get().


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 26, 2006 12:16 am 
Beginner
Beginner

Joined: Fri Nov 11, 2005 1:04 pm
Posts: 22
pelton wrote:
jorgas wrote:
Have you tried to use ISession.Get() instead of ISession.Load() in order for lazy loading to work?


I tried this too, but it still loads everything.

The log from using Load() looks the same as using Get().


How do you know it's not lazy loading? Are you looking at the SQL or testing to see if the object is loaded after closing the session?


Top
 Profile  
 
 Post subject: another solution of using Generic Collections wit NHibernate
PostPosted: Sun Feb 26, 2006 4:04 pm 
Beginner
Beginner

Joined: Wed Dec 28, 2005 3:14 pm
Posts: 29
one of the solutions is to use non-generic collection as fields and let Nhibernate directly access them and use generic collection wrappers as properties to expose this field to the rest of your application. In this way you don't need to "guess" how Nhibernate is going to work with generic collections.
Here is an article about a Generic ISet and a set of simple wrappers:
http://www.codeproject.com/csharp/GenericISet.asp


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.