I have some big data objects, stored in CompressedData property, so I don’t want to load this data every time, when I do any query. So I tried to mark this property as LazyLoad() in mapping class.
There is no access to CompressedData out of entity class, because I have UncompressedData property that give me functionality of compress/decompress data. So I call CompressedData only inside TestEntity class.
But I got a trouble. NHibernate 3.2.0.4000 (or FluentNHibernate 1.3.0.717) didn’t want to load LazyLoad property if there are no requests to this property out of entity class. If try to run test, I will got an empty string.
public class TestEntity {
public TestEntity(){}
public TestEntity(string otherData, string dataForCompress){
OtherData = otherData;
UncompressedData = dataForCompress;
}
public virtual string OtherData {get;set;}
public virtual byte[] CompressedData {get;set;}
public virtual string UncompressedData {
get {
return SomeLongFunctionForDecompress(CompressedData);
}
set {
CompressedData = SomeLongFunctionForCompress(value);
}
}
}
public class TestEntityMapping : ClassMap<TestEntity> {
public TestEntityMapping(){
Map(OtherData);
Map(CompressedData).LazyLoad();
}
}
[Test]
public void can_readLazyLoadedProperty(){
TestEntity obj1 = new TestEntity("test","long data here");
using (nHibernateHelper.CreateNewSession(){
Session.Save(obj1);
}
using (nHibernateHelper.CreateNewSession(){
TestEntity obj2 = Session.Get(obj1.Id);
//byte[] data1 = obj2.CompressedData;
string data2 = obj2.UncompressedData;
Assert.AreEqual("long data here",data2);
}
}
But if uncomment byte[] data1 = obj2.CompressedData; line all works fine. Maybe I need add some attribute to CompressedData property or add some properties to mapping?
I suppose .NET gives similar mechanism as Java, and from your description it seems to be that way:
Hibernate modifies entity classes wrapping them in its own objects, that modify getters so that LazyLoad is working
Calling getters from inside you call unmodified version of getters, which do not handle LazyLoad.
So this what you get is feature, not bug. The solution could be moving logic outside entity, that should have fields and accessors only.