2

Consider objects like these:

public partial class Record
{
    public int RecordID {get; set;}
    public DateTime? Created { get; set; }
    public int CreatorUserUserObjectNDX { get; set; }
    public UserObject Creator { get; set; }
    ...
}

and

public partial class UserObject
{
  public virtual int ID {get; set;}
  public virtual string Name {get; set;}
}

and their mappings like:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="..." namespace="..." assembly="...">
<class name="Record" table="records" lazy="false" schema="..">
    <id name="RecordID" column="ndx">
        <generator class="native" />
    </id>
    <property name="Created" column="created" />
    <property name="CreatorUserUserObjectNDX" column="creator_user_user_object_ndx" />

    <many-to-one name="Creator" column="creator_user_user_object_ndx" lazy="proxy"/>
</class>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="..." namespace="..." assembly="...">
<class name="UserObject" table="user_objects" lazy="false">
    <id name="NDX" column="ndx">
        <generator class="native" />
    </id>

    <property name="Name" column="name" />
</class>

They are kept simple intentionally here, in real they're more heavy with one-to-many collections etc, but these work, so they're not the point here.

The problem I ran into is, when I use definitions & mappings like these, and do a simple

var results = session.QueryOver<Record>()
  .List();

NHibernate does NOT create a proxy for the creator property, it just uses left outer joins in the executed SQL statement and inserts a UserObject directly. When I change lazy='false' to lazy='true' within the mapping for UserObject it works, proxy is created and the executed SQL has no outer joins.

So, is it strictly necessary to set lazy='true' for all classes, that may be a reference anywhere? I thought I could use set it to false per default for all mappings and set lazy='proxy' in the many-to-one definition, when needed.

I use NHibernate 3.2, so no config about proxyfactory. What am I missing here?

1 Answer 1

3

If you set lazy=false in your mapping, there is no need for NHibernate to create a proxy, because all properties will be eagerly loaded.

You should not change lazy loading settings unless it is required that you do eager loading. Even then, you can use "fetch" (in hql for example) to eagerly load what you need.

Sign up to request clarification or add additional context in comments.

5 Comments

But my problem IS, that NH does eager loading, unless I specify lazy=true in class mapping and I WANT lazy loading, not eager
NH does lazy loading by default. NH does eager loading in your case because of the lazy="false" in your class declaration.
Oh dear... now I get it. So that default rule for my class mappings is the reason? And so it does mean, when I set lazy to false, there is not chance for lazy loading for this type?
Not sure, that's why I prefer leaving NH doing his job, and setting custom behavior on a per-query basis.
So yeah, that's enough for me as an answer... Thx for your time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.