In NHibernate, is it possible to have a query return a mapped object that's partially filled with data (the rest being lazy loaded)?
In my lega开发者_运维知识库cy database, there is a giant table with 40+ columns. I would like to be able to partially fill my giant mapped class with data, so I'm only getting the data I need. How can I query nhibernate to return me my giant mapped object that has only the properties I want filled in?
Even better would be: If I do need another property that isn't filled in.. it would be lazily loaded.
Is this possible?
Thanks!
Isaac
Your best bet would be to project the individual properties in your query. As far as I know, NHibernate doesn't allow you to lazy load individual properties.
Yes it is possible.
http://ayende.com/blog/4377/nhibernate-new-feature-lazy-properties
The byte code manipulator can be pretty powerful. According to this article only the Castle one supports it but it has been a year. I would guess Li supports it now.
And EVEN if that didn't work. It should be fairly straight forward to turn each property into it's own mapped-class in which the lazy loader would take affect. It's not ideal but it would be a work around if it didn't already support lazy properties. Luckily it appears NHibernate does.
This is possible with Fluent NHibernate, so I assume it is with NHibernate. This code will work for FNH (and I am sorry, but I do not know the NHibernate equivalent, but if you know HBM files you should be able to work it out).
You have two ways of doing this, either load all as lazy by default, and then specify the properties you want to load fully separately, or the opposite where all properties are loaded by default and you specify those that you want to be lazy.
An example mapping for the first option (in FNH, again, sorry) would be something like this:
public partial class ActionableEventMap : SubclassMap<ActionableEvent>
{
public ActionableEventMap()
{
References(x => x.Branch).Access.Property();
References(x => x.Department).Access.Property();
Map(x => x.Cost).Not.LazyLoad().Access.Property();
Map(x => x.PurchaseOrderNumber).Not.LazyLoad().Access.Property();
References(x => x.UserQualification).Access.Property();
}
}
Note how you can specify the lazy
property individually
Edit: actually, here's the hbm
<property access="property" name="LastReminder" type="System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" lazy="false">
<column name="LastReminder" />
</property>
<property access="property" name="CCEmailString" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" lazy="false">
<column name="CCEmailString" />
</property>
精彩评论