我在我的实体中使用lazyload属性。它工作正常,但是当通过方法更改此类中的任何属性时,NHibernate不会进行更新。如果直接更改属性,则一切正常。
我认为在返回lazy属性代理对象的情况下存在问题,并且此代理不处理内部更改。
我在下面添加了测试代码。它在NHibernate 3.1中运行良好,但在NHibernate 3.2和3.3中没有调用更新
也许有人知道如何解决这个问题?
感谢。
实体:
/// <summary>
/// Class for testing lazy properties.
/// </summary>
public class TestClass
{
public virtual int Id { get; set; }
public virtual string LazyData { get; set; }
public virtual string TestData { get; set; }
public virtual void ChangeTestData(string data)
{
TestData = data;
}
}
映射:
public class TestClassMap : ClassMap<TestClass>
{
public TestClassMap()
{
Id(m => m.Id);
Map(m => m.LazyData).LazyLoad();
Map(m => m.TestData);
}
}
测试代码:
[Test]
public void LazyProperityTest1()
{
var testObj = new TestClass();
Session.Save(testObj);
Session.Flush();
Session.Clear();
// Change property and flush session.
var persistedObj = Session.CreateCriteria<TestClass>().List<TestClass>().First();
persistedObj.ChangeTestData("test");
Session.Flush();
Session.Clear();
}
设置LazyLoad时生成的sql请求:
NHibernate: INSERT INTO "TestClass" (LazyData, TestData) VALUES (@p0, @p1); select last_insert_rowid();@p0 = NULL [Type: String (0)], @p1 = NULL [Type: String (0)]
NHibernate: SELECT this_.Id as Id0_0_, this_.TestData as TestData0_0_ FROM "TestClass" this_
未设置LazyLoad时生成的sql请求:
NHibernate: INSERT INTO "TestClass" (LazyData, TestData) VALUES (@p0, @p1); select last_insert_rowid();@p0 = NULL [Type: String (0)], @p1 = NULL [Type: String (0)]
NHibernate: SELECT this_.Id as Id0_0_, this_.LazyData as LazyData0_0_, this_.TestData as TestData0_0_ FROM "TestClass" this_
NHibernate: UPDATE "TestClass" SET LazyData = @p0, TestData = @p1 WHERE Id = @p2;@p0 = NULL [Type: String (0)], @p1 = 'test' [Type: String (0)], @p2 = 1 [Type: Int32 (0)]