Skip to content

Fix dynamic delete for one-to-one association #2652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2631/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Linq;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH2631
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var person = new Person
{
Name = "Testing"
};
person.Address = new Address
{
Person = person,
Street = "Mulberry"
};
session.Save(person);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from Address").ExecuteUpdate();
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public async Task IndexOutOfRangeOnDeleteAsync()
{
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
var persons = await (session.Query<Person>().ToListAsync());

foreach (var person in persons)
{
await (session.DeleteAsync(person));
}

await (t.CommitAsync());
}
}

[Test]
public async Task UpdateAsync()
{
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
var persons = await (session.Query<Person>().ToListAsync());

foreach (var person in persons)
{
person.Name = "x";
person.Address = null;
}

await (t.CommitAsync());
}
}
}
}
13 changes: 13 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2631/Address.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH2631
{
public class Address
{
public virtual Guid Id { get; set; }

public virtual Person Person { get; set; }

public virtual string Street { get; set; }
}
}
76 changes: 76 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2631/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Linq;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2631
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var person = new Person
{
Name = "Testing"
};
person.Address = new Address
{
Person = person,
Street = "Mulberry"
};
session.Save(person);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from Address").ExecuteUpdate();
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public void IndexOutOfRangeOnDelete()
{
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
var persons = session.Query<Person>().ToList();

foreach (var person in persons)
{
session.Delete(person);
}

t.Commit();
}
}

[Test]
public void Update()
{
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
var persons = session.Query<Person>().ToList();

foreach (var person in persons)
{
person.Name = "x";
person.Address = null;
}

t.Commit();
}
}
}
}
26 changes: 26 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2631/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH2631">

<class name="Person" optimistic-lock="dirty" dynamic-update="true">
<id name="Id">
<generator class="guid.comb"/>
</id>

<one-to-one name="Address" cascade="all-delete-orphan"/>

<property name="Name"/>
</class>

<class name="Address" optimistic-lock="dirty" dynamic-update="true">
<id name="Id">
<generator class="foreign">
<param name="property">Person</param>
</generator>
</id>
<one-to-one name="Person" constrained="true"/>

<property name="Street"/>
</class>

</hibernate-mapping>
13 changes: 13 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2631/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH2631
{
public class Person
{
public virtual Guid Id { get; set; }

public virtual string Name { get; set; }

public virtual Address Address { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ public async Task DeleteAsync(object id, object version, int j, object obj, SqlC
IType[] types = PropertyTypes;
for (int i = 0; i < entityMetamodel.PropertySpan; i++)
{
if (IsPropertyOfTable(i, j) && versionability[i])
if (IsPropertyOfTable(i, j) && versionability[i] && types[i].GetOwnerColumnSpan(Factory) > 0)
{
// this property belongs to the table and it is not specifically
// excluded from optimistic locking by optimistic-lock="false"
Expand Down
6 changes: 3 additions & 3 deletions src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3347,7 +3347,7 @@ public void Delete(object id, object version, int j, object obj, SqlCommandInfo
IType[] types = PropertyTypes;
for (int i = 0; i < entityMetamodel.PropertySpan; i++)
{
if (IsPropertyOfTable(i, j) && versionability[i])
if (IsPropertyOfTable(i, j) && versionability[i] && types[i].GetOwnerColumnSpan(Factory) > 0)
{
// this property belongs to the table and it is not specifically
// excluded from optimistic locking by optimistic-lock="false"
Expand Down Expand Up @@ -3583,13 +3583,13 @@ protected SqlCommandInfo[] GenerateSQLDeleteStrings(object[] loadedState)
{
delete.SetComment("delete " + EntityName + " [" + j + "]");
}

bool[] versionability = PropertyVersionability;
IType[] types = PropertyTypes;
for (int i = 0; i < entityMetamodel.PropertySpan; i++)
{
bool include = versionability[i] &&
IsPropertyOfTable(i, j);
IsPropertyOfTable(i, j)
&& types[i].GetOwnerColumnSpan(Factory) > 0;

if (include)
{
Expand Down