Skip to content

Commit e8d101d

Browse files
committed
NH-3468 - Fix delete of entity with lazy uninitialized component
1 parent 5e3f957 commit e8d101d

File tree

6 files changed

+147
-1
lines changed

6 files changed

+147
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace NHibernate.Test.LazyComponentTest
7+
{
8+
public class Address
9+
{
10+
public virtual string Country { get; set; }
11+
public virtual string City { get; set; }
12+
}
13+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System.Collections;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.LazyComponentTest
5+
{
6+
[TestFixture]
7+
public class LazyComponentTestFixture : TestCase
8+
{
9+
protected override IList Mappings
10+
{
11+
get { return new[] {"LazyComponentTest.Person.hbm.xml"}; }
12+
}
13+
14+
protected override string MappingsAssembly
15+
{
16+
get { return "NHibernate.Test"; }
17+
}
18+
19+
protected override void OnSetUp()
20+
{
21+
using (var s = OpenSession())
22+
using (var t = s.BeginTransaction())
23+
{
24+
var person = new Person
25+
{
26+
Name = "Gabor",
27+
Address = new Address
28+
{
29+
Country = "HUN",
30+
City = "Budapest"
31+
}
32+
};
33+
s.Persist(person);
34+
t.Commit();
35+
}
36+
}
37+
38+
protected override void OnTearDown()
39+
{
40+
using (var s = OpenSession())
41+
using (var t = s.BeginTransaction())
42+
{
43+
s.CreateQuery("delete from Person").ExecuteUpdate();
44+
t.Commit();
45+
}
46+
}
47+
48+
[Test]
49+
public void LazyLoadTest()
50+
{
51+
using (var s = OpenSession())
52+
using (var t = s.BeginTransaction())
53+
{
54+
var p = s.CreateQuery("from Person p where name='Gabor'").UniqueResult<Person>();
55+
// make sure component has not been initialized yet
56+
Assert.That(NHibernateUtil.IsPropertyInitialized(p, "Address"), Is.False);
57+
58+
t.Commit();
59+
}
60+
}
61+
62+
[Test]
63+
public void LazyDeleteTest()
64+
{
65+
using (var s = OpenSession())
66+
using (var t = s.BeginTransaction())
67+
{
68+
var p = s.CreateQuery("from Person p where name='Gabor'").UniqueResult<Person>();
69+
// make sure component has not been initialized yet
70+
Assert.That(NHibernateUtil.IsPropertyInitialized(p, "Address"), Is.False);
71+
s.Delete(p);
72+
t.Commit();
73+
}
74+
}
75+
76+
[Test]
77+
public void LazyUpdateTest()
78+
{
79+
using (var s = OpenSession())
80+
using (var t = s.BeginTransaction())
81+
{
82+
var p = s.CreateQuery("from Person p where name='Gabor'").UniqueResult<Person>();
83+
// make sure component has not been initialized yet
84+
Assert.That(!NHibernateUtil.IsPropertyInitialized(p, "Address"));
85+
86+
p.Address.City = "Baja";
87+
s.Update(p);
88+
89+
t.Commit();
90+
}
91+
using (var s = OpenSession())
92+
using (var t = s.BeginTransaction())
93+
{
94+
var p = s.CreateQuery("from Person p where name='Gabor'").UniqueResult<Person>();
95+
Assert.That(p.Address.City, Is.EqualTo("Baja"));
96+
97+
t.Commit();
98+
}
99+
}
100+
}
101+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace NHibernate.Test.LazyComponentTest
7+
{
8+
public class Person
9+
{
10+
public virtual string Name { get; set; }
11+
public virtual Address Address { get; set; }
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.LazyComponentTest">
5+
6+
<class name="Person">
7+
<id name="Name"/>
8+
<component name="Address" lazy="true">
9+
<property name="Country" />
10+
<property name="City" />
11+
</component>
12+
</class>
13+
14+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@
497497
<Compile Include="KnownBugAttribute.cs" />
498498
<Compile Include="Join\JoinedFilters.cs" />
499499
<Compile Include="Join\TennisPlayer.cs" />
500+
<Compile Include="LazyComponentTest\Address.cs" />
501+
<Compile Include="LazyComponentTest\LazyComponentTestFixture.cs" />
502+
<Compile Include="LazyComponentTest\Person.cs" />
500503
<Compile Include="LazyOneToOne\Employee.cs" />
501504
<Compile Include="LazyOneToOne\Employment.cs" />
502505
<Compile Include="LazyOneToOne\LazyOneToOneTest.cs" />
@@ -3062,6 +3065,7 @@
30623065
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
30633066
</ItemGroup>
30643067
<ItemGroup>
3068+
<EmbeddedResource Include="LazyComponentTest\Person.hbm.xml" />
30653069
<EmbeddedResource Include="NHSpecificTest\NH3570\Mappings.hbm.xml" />
30663070
<EmbeddedResource Include="NHSpecificTest\NH3455\Mappings.hbm.xml" />
30673071
<EmbeddedResource Include="NHSpecificTest\NH3590\Mappings.hbm.xml" />

src/NHibernate/Tuple/Component/PocoComponentTuplizer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using NHibernate.Bytecode;
3+
using NHibernate.Intercept;
34
using NHibernate.Properties;
45

56
namespace NHibernate.Tuple.Component
@@ -67,7 +68,7 @@ public override System.Type MappedClass
6768
public override object[] GetPropertyValues(object component)
6869
{
6970
// NH Different behavior : for NH-1101
70-
if (Equals(BackrefPropertyAccessor.Unknown, component) || component == null)
71+
if (Equals(BackrefPropertyAccessor.Unknown, component) || Equals(LazyPropertyInitializer.UnfetchedProperty, component) || component == null)
7172
{
7273
return new object[propertySpan];
7374
}

0 commit comments

Comments
 (0)