Skip to content

Commit 03e5fa6

Browse files
RogerKratzhazzik
authored andcommitted
Add test for bidirectional list fail when session only knows about child
1 parent 3f9d6d6 commit 03e5fa6

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using NUnit.Framework;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH2089
4+
{
5+
[KnownBug("gh-2089")]
6+
public class Fixture : BugTestCase
7+
{
8+
private Parent _parent;
9+
10+
protected override void OnSetUp()
11+
{
12+
using (var s = OpenSession())
13+
using (var tx = s.BeginTransaction())
14+
{
15+
_parent = new Parent();
16+
_parent.AddChild(new Child());
17+
s.Save(_parent);
18+
tx.Commit();
19+
}
20+
}
21+
22+
[Test]
23+
public virtual void CanAddChild()
24+
{
25+
var newChild = new Child();
26+
_parent.AddChild(newChild);
27+
using (var s = OpenSession())
28+
using (var tx = s.BeginTransaction())
29+
{
30+
s.Merge(newChild);
31+
tx.Commit();
32+
}
33+
34+
using (var s = OpenSession())
35+
{
36+
Assert.That(s.Get<Parent>(_parent.Id).Children.Count, Is.EqualTo(2));
37+
}
38+
}
39+
40+
41+
protected override void OnTearDown()
42+
{
43+
using (var s = OpenSession())
44+
using (var tx = s.BeginTransaction())
45+
{
46+
s.Delete(s.Load<Parent>(_parent.Id));
47+
tx.Commit();
48+
}
49+
}
50+
}
51+
}
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"
3+
namespace="NHibernate.Test.NHSpecificTest.GH2089"
4+
assembly="NHibernate.Test">
5+
6+
<class name="Parent">
7+
<id name="Id">
8+
<generator class="native"/>
9+
</id>
10+
<property name="Name" />
11+
<list name="Children" cascade="all-delete-orphan" inverse="true">
12+
<key column="parent_id" not-null="true"/>
13+
<index column="child_index" />
14+
<one-to-many class="Child" />
15+
</list>
16+
</class>
17+
18+
<class name="Child">
19+
<id name="Id">
20+
<generator class="native"/>
21+
</id>
22+
<property name="Name" />
23+
<many-to-one name="Parent" class="Parent" column="parent_id"/>
24+
<property name="OrderIndex" access="readonly" column="child_index" />
25+
</class>
26+
</hibernate-mapping>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH2089
4+
{
5+
public class Parent
6+
{
7+
public virtual int Id { get; set; }
8+
public virtual string Name { get; set; }
9+
public virtual IList<Child> Children { get; protected set; } = new List<Child>();
10+
11+
public virtual void AddChild(Child child)
12+
{
13+
Children.Add(child);
14+
child.Parent = this;
15+
}
16+
17+
public override bool Equals(object obj)
18+
{
19+
return obj is Parent other && Id == other.Id;
20+
}
21+
22+
public override int GetHashCode()
23+
{
24+
return 0;
25+
}
26+
}
27+
28+
public class Child
29+
{
30+
public virtual int Id { get; set; }
31+
public virtual string Name { get; set; }
32+
public virtual Parent Parent { get; set; }
33+
34+
public virtual int OrderIndex
35+
{
36+
get
37+
{
38+
if (Parent == null)
39+
return -2;
40+
return Parent.Children.IndexOf(this);
41+
}
42+
}
43+
44+
public override bool Equals(object obj)
45+
{
46+
return obj is Child other && Id == other.Id;
47+
}
48+
49+
public override int GetHashCode()
50+
{
51+
return 0;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)