views:

205

answers:

3

I've one object User which can have multiple Posts.

Example:

Load the user with lazy loading on the Posts IList<User> users = User.LoadAll()

Then I want to read only "half" of the users[2].Posts[3] (retrieve only the attributes that I want and not all of them from that post object), is this possible to make?

(Note, I do not want to use a View).

Edit: Could someone give me a simple example code please? I tried to find one without success. Thanks

A: 

Yes, this is possible.

When NHibernate reconstitutes an object from the database, it can also reconstitute all the object's members, creating an entire object graph. This is called cascading, it's a property of an association defined in your mappings file, and by the sound of things you do not want it. See the docuemntation for more.

David Seiler
A: 

Sure.

You just have to declare an extra property of a collection type (e.g. 'map') on your 'User' class, set its 'lazy' attribute to false and the 'where' attribute to the required SQL-where clause.

Thomas Weller
+2  A: 

If I understand your question well - you want to get an object - Post in your case - but only some of it's properties - e.g. Post.Annotation and not Post.Content which both are strings.

This is NOT possible at the moment. Every object retrieved from database will have all its properties that are not relations nor collections loaded.

You can do a workaround:

  1. by turning the biggish properties into a separate entity and then do many-to-one mapping and utilize lazy loading

  2. by creating custom query be it HQL or criteria with projections (basically only few columns). This will however not return the full object.

Hope I got your question right...

Rashack
Yes, that was my question, be able from lazy loading get only some properties from that object and not all. I was wondering if the NHibernate team would think of some way to create "custom lazy loading mapping", allowing to have more than one way to load one object (full, part of it).
emanyalpsid