Skip to content

Bidirectional list fails if session only knows about child #2089

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 3 commits into from
Jun 20, 2019
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
51 changes: 51 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2089/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2089
{
[KnownBug("gh-2089")]
public class Fixture : BugTestCase
{
private Parent _parent;

protected override void OnSetUp()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
_parent = new Parent();
_parent.AddChild(new Child());
s.Save(_parent);
tx.Commit();
}
}

[Test]
public virtual void CanAddChild()
{
var newChild = new Child();
_parent.AddChild(newChild);
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.Merge(newChild);
tx.Commit();
}

using (var s = OpenSession())
{
Assert.That(s.Get<Parent>(_parent.Id).Children.Count, Is.EqualTo(2));
}
}


protected override void OnTearDown()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.Delete(s.Load<Parent>(_parent.Id));
tx.Commit();
}
}
}
}
26 changes: 26 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2089/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"
namespace="NHibernate.Test.NHSpecificTest.GH2089"
assembly="NHibernate.Test">

<class name="Parent">
<id name="Id">
<generator class="native"/>
</id>
<property name="Name" />
<list name="Children" cascade="all-delete-orphan" inverse="true">
<key column="parent_id" not-null="true"/>
<index column="child_index" />
<one-to-many class="Child" />
</list>
</class>

<class name="Child">
<id name="Id">
<generator class="native"/>
</id>
<property name="Name" />
<many-to-one name="Parent" class="Parent" column="parent_id"/>
<property name="OrderIndex" access="readonly" column="child_index" />
</class>
</hibernate-mapping>
54 changes: 54 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2089/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH2089
{
public class Parent
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Child> Children { get; protected set; } = new List<Child>();

public virtual void AddChild(Child child)
{
Children.Add(child);
child.Parent = this;
}

public override bool Equals(object obj)
{
return obj is Parent other && Id == other.Id;
}

public override int GetHashCode()
{
return 0;
}
}

public class Child
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Parent Parent { get; set; }

public virtual int OrderIndex
{
get
{
if (Parent == null)
return -2;
return Parent.Children.IndexOf(this);
}
}

public override bool Equals(object obj)
{
return obj is Child other && Id == other.Id;
}

public override int GetHashCode()
{
return 0;
}
}
}