Skip to content

Commit e92877b

Browse files
bahusoidfredericDelaporte
authored andcommitted
Fix dynamic delete for one-to-one association
1 parent 39a28aa commit e92877b

File tree

7 files changed

+220
-4
lines changed

7 files changed

+220
-4
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Linq;
12+
using NUnit.Framework;
13+
using NHibernate.Linq;
14+
15+
namespace NHibernate.Test.NHSpecificTest.GH2631
16+
{
17+
using System.Threading.Tasks;
18+
[TestFixture]
19+
public class FixtureAsync : BugTestCase
20+
{
21+
protected override void OnSetUp()
22+
{
23+
using (var session = OpenSession())
24+
using (var transaction = session.BeginTransaction())
25+
{
26+
var person = new Person
27+
{
28+
Name = "Testing"
29+
};
30+
person.Address = new Address
31+
{
32+
Person = person,
33+
Street = "Mulberry"
34+
};
35+
session.Save(person);
36+
37+
transaction.Commit();
38+
}
39+
}
40+
41+
protected override void OnTearDown()
42+
{
43+
using (var session = OpenSession())
44+
using (var transaction = session.BeginTransaction())
45+
{
46+
session.CreateQuery("delete from Address").ExecuteUpdate();
47+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
48+
49+
transaction.Commit();
50+
}
51+
}
52+
53+
[Test]
54+
public async Task IndexOutOfRangeOnDeleteAsync()
55+
{
56+
using (var session = OpenSession())
57+
using (var t = session.BeginTransaction())
58+
{
59+
var persons = await (session.Query<Person>().ToListAsync());
60+
61+
foreach (var person in persons)
62+
{
63+
await (session.DeleteAsync(person));
64+
}
65+
66+
await (t.CommitAsync());
67+
}
68+
}
69+
70+
[Test]
71+
public async Task UpdateAsync()
72+
{
73+
using (var session = OpenSession())
74+
using (var t = session.BeginTransaction())
75+
{
76+
var persons = await (session.Query<Person>().ToListAsync());
77+
78+
foreach (var person in persons)
79+
{
80+
person.Name = "x";
81+
person.Address = null;
82+
}
83+
84+
await (t.CommitAsync());
85+
}
86+
}
87+
}
88+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH2631
4+
{
5+
public class Address
6+
{
7+
public virtual Guid Id { get; set; }
8+
9+
public virtual Person Person { get; set; }
10+
11+
public virtual string Street { get; set; }
12+
}
13+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System.Linq;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH2631
5+
{
6+
[TestFixture]
7+
public class Fixture : BugTestCase
8+
{
9+
protected override void OnSetUp()
10+
{
11+
using (var session = OpenSession())
12+
using (var transaction = session.BeginTransaction())
13+
{
14+
var person = new Person
15+
{
16+
Name = "Testing"
17+
};
18+
person.Address = new Address
19+
{
20+
Person = person,
21+
Street = "Mulberry"
22+
};
23+
session.Save(person);
24+
25+
transaction.Commit();
26+
}
27+
}
28+
29+
protected override void OnTearDown()
30+
{
31+
using (var session = OpenSession())
32+
using (var transaction = session.BeginTransaction())
33+
{
34+
session.CreateQuery("delete from Address").ExecuteUpdate();
35+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
36+
37+
transaction.Commit();
38+
}
39+
}
40+
41+
[Test]
42+
public void IndexOutOfRangeOnDelete()
43+
{
44+
using (var session = OpenSession())
45+
using (var t = session.BeginTransaction())
46+
{
47+
var persons = session.Query<Person>().ToList();
48+
49+
foreach (var person in persons)
50+
{
51+
session.Delete(person);
52+
}
53+
54+
t.Commit();
55+
}
56+
}
57+
58+
[Test]
59+
public void Update()
60+
{
61+
using (var session = OpenSession())
62+
using (var t = session.BeginTransaction())
63+
{
64+
var persons = session.Query<Person>().ToList();
65+
66+
foreach (var person in persons)
67+
{
68+
person.Name = "x";
69+
person.Address = null;
70+
}
71+
72+
t.Commit();
73+
}
74+
}
75+
}
76+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.GH2631">
4+
5+
<class name="Person" optimistic-lock="dirty" dynamic-update="true">
6+
<id name="Id">
7+
<generator class="guid.comb"/>
8+
</id>
9+
10+
<one-to-one name="Address" cascade="all-delete-orphan"/>
11+
12+
<property name="Name"/>
13+
</class>
14+
15+
<class name="Address" optimistic-lock="dirty" dynamic-update="true">
16+
<id name="Id">
17+
<generator class="foreign">
18+
<param name="property">Person</param>
19+
</generator>
20+
</id>
21+
<one-to-one name="Person" constrained="true"/>
22+
23+
<property name="Street"/>
24+
</class>
25+
26+
</hibernate-mapping>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH2631
4+
{
5+
public class Person
6+
{
7+
public virtual Guid Id { get; set; }
8+
9+
public virtual string Name { get; set; }
10+
11+
public virtual Address Address { get; set; }
12+
}
13+
}

src/NHibernate/Async/Persister/Entity/AbstractEntityPersister.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ public async Task DeleteAsync(object id, object version, int j, object obj, SqlC
896896
IType[] types = PropertyTypes;
897897
for (int i = 0; i < entityMetamodel.PropertySpan; i++)
898898
{
899-
if (IsPropertyOfTable(i, j) && versionability[i])
899+
if (IsPropertyOfTable(i, j) && versionability[i] && types[i].GetOwnerColumnSpan(Factory) > 0)
900900
{
901901
// this property belongs to the table and it is not specifically
902902
// excluded from optimistic locking by optimistic-lock="false"

src/NHibernate/Persister/Entity/AbstractEntityPersister.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3347,7 +3347,7 @@ public void Delete(object id, object version, int j, object obj, SqlCommandInfo
33473347
IType[] types = PropertyTypes;
33483348
for (int i = 0; i < entityMetamodel.PropertySpan; i++)
33493349
{
3350-
if (IsPropertyOfTable(i, j) && versionability[i])
3350+
if (IsPropertyOfTable(i, j) && versionability[i] && types[i].GetOwnerColumnSpan(Factory) > 0)
33513351
{
33523352
// this property belongs to the table and it is not specifically
33533353
// excluded from optimistic locking by optimistic-lock="false"
@@ -3583,13 +3583,13 @@ protected SqlCommandInfo[] GenerateSQLDeleteStrings(object[] loadedState)
35833583
{
35843584
delete.SetComment("delete " + EntityName + " [" + j + "]");
35853585
}
3586-
35873586
bool[] versionability = PropertyVersionability;
35883587
IType[] types = PropertyTypes;
35893588
for (int i = 0; i < entityMetamodel.PropertySpan; i++)
35903589
{
35913590
bool include = versionability[i] &&
3592-
IsPropertyOfTable(i, j);
3591+
IsPropertyOfTable(i, j)
3592+
&& types[i].GetOwnerColumnSpan(Factory) > 0;
35933593

35943594
if (include)
35953595
{

0 commit comments

Comments
 (0)