1

On properties inside my domain objects which I do not want lazy loading, I omit the virtual modifier, and also update the mapping file to reflect this using for example:

<property name="UserName" column="Name" type="String" length="40" lazy="false"/>

I would have though that setting the property lazy to false would make it accept that the relevant property inside the domain object not be virtual.

Can anyone explain how I can not make my eager load properties virtual I simply want:

public string UserName{
    get{ return _userName; }
    set{ _userName = value; }
}

Many Thanks,

Andrew

2
  • davybrion.com/blog/2009/03/… Commented Jul 22, 2009 at 20:45
  • I found an article explaining this, and with this tag line : "That’s a question that many people who are new to NHibernate have" lol thanks anyway Commented Jul 22, 2009 at 20:45

2 Answers 2

2

I don't think properties can be lazy loaded — just collections and references — unless lazy loading by column was added recently.

I'm not sure what lazy="false" does on that property in NHibernate.

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

5 Comments

Its weird cause it complains that it wants the properties to be virtual.
Yah. It makes all properties virtual because it wants to be able to lazy load the entity as a whole.
As an example ... Phone has a many-to-one with User. Phone is in the session, but User is lazy loaded. Phone.User.UserName would result in a database query to build the User object. This is why User.UserName has to be virtual.
Cheers for the explanation. :-)
NHibernate can lazy load properties too. This attribute being available here is not an error. And I believe it was already there in 2009. Yes, that means it will load by default only properties not flagged as lazy. Then, when trying to get a lazy property, it will get it from db lazily. Be aware that such lazy loading is currently not batched, thus being vulnerable to select n+1 performance issues even when batch fetching of lazy loads is enabled.
0

If your whole entity in non-lazy, you can declare it without anything virtual. This requires setting lazy="false" on the <class> mapping.

If your entity has to support being lazily-loaded, you must have all its non-private members declared as virtual for the lazy-loading to be able to build a proxy for it.

When an entity is lazily loaded, NHibernate just return a proxy for it, deriving from it, holding its Id, and nothing else. Any access to other members will trigger its loading. For this to works, it needs to have them all virtual.

If you want to have properties not lazily-loaded, that indeed means you want to disable the lazy-loading for the entity, while maybe enabling lazy-loading for some properties on it. The lazy attribute on <property> is there for that. This mechanism still requires to proxify the entity, although it is not lazy loaded. I have not checked this, but at least the lazily loaded properties will have to be virtual, and likely all the other non-private members will still need to be virtual.
Be aware that such lazy loading is currently not batched, thus being vulnerable to select n+1 performance issues even when batch fetching of lazy loads is enabled.

Comments

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.