tags:

views:

79

answers:

3

I don't like the idea of proxy and lazy loading. I don't need that. I want pure POCO. And I want to control loading associations explicitly when I need.

Here is entity

public class Post
{
 public long Id { get; set; }
 public long OwnerId { get; set; }
 public string Content { get; set; }

 public User Owner { get; set; }
}

and mapping

<class name="Post">
    <id name="Id" />
    <property name="OwnerId" />
    <property name="Content" />
    <many-to-one name="Owner" column="OwnerId" />
</class>

However if I specify lazy="false" in the mapping, Owner is always eagerly fetched. I can't remove many-to-one mapping because that also disables explicit loading or a query like

from x in session.Query<Post>()
where x.Owner.Title == "hello"
select x;

I specified lazy="true" and set use_proxy_validator property to false. But that also eager loads Owner.

Is there any way to load only Post entity?

A: 

In short, it is not possible with out of box NH. But here is attempt at just, lazy loading without proxies

http://thinkbeforecoding.com/post/2009/02/07/Lazy-load-and-persistence-ignorance

epitka
A: 

Set the class User to lazy = false on the mapping

<class name="User" table="Users" lazy="false">
Claudio Redi
No effect. Still eager loads Owner.
Alice
A: 

Remove this property <property name="OwnerId" />... to get the owner id you can use Owner.Id. This will not trigger a lazy load. Owner will only be loaded if you hit any property besides the id. To make it a flat/simple POCO, you can use projections and ResultTransformers.

Davy Brion - Must Everything be Virtual with NHibernate

dotjoe
If I don't make every member virtual all associations are eager loaded regardless of lazy load setting in the mapping. I want to load only Post entity without setting my member virtual.
Alice