Skip to content

Commit 9e020e2

Browse files
Add test case for one-to-one with property ref SOE
Co-authored-by: Michael Estermann <michael.estermann@bbv.ch>
1 parent 6e8f190 commit 9e020e2

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH1645
4+
{
5+
public abstract class EntityBase
6+
{
7+
public virtual Guid Id { get; protected set; }
8+
9+
public static bool operator ==(EntityBase left, EntityBase right)
10+
{
11+
return Equals(left, right);
12+
}
13+
14+
public static bool operator !=(EntityBase left, EntityBase right)
15+
{
16+
return !Equals(left, right);
17+
}
18+
19+
public override bool Equals(object obj)
20+
{
21+
return Equals(obj as EntityBase);
22+
}
23+
24+
public virtual bool Equals(EntityBase other)
25+
{
26+
if (other == null)
27+
{
28+
return false;
29+
}
30+
31+
if (ReferenceEquals(this, other))
32+
{
33+
return true;
34+
}
35+
36+
if (!IsTransient(this) && !IsTransient(other) && Equals(Id, other.Id))
37+
{
38+
var otherType = other.GetUnproxiedType();
39+
var thisType = GetUnproxiedType();
40+
return thisType.IsAssignableFrom(otherType) ||
41+
otherType.IsAssignableFrom(thisType);
42+
}
43+
44+
return false;
45+
}
46+
47+
public override int GetHashCode()
48+
{
49+
if (Equals(Id, default(Guid)))
50+
{
51+
return base.GetHashCode();
52+
}
53+
54+
return Id.GetHashCode();
55+
}
56+
57+
private static bool IsTransient(EntityBase obj)
58+
{
59+
return obj != null && Equals(obj.Id, default(Guid));
60+
}
61+
62+
private System.Type GetUnproxiedType()
63+
{
64+
return GetType();
65+
}
66+
}
67+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH1645
5+
{
6+
[TestFixture]
7+
public class Fixture : BugTestCase
8+
{
9+
private Guid _superParentId;
10+
11+
protected override void OnSetUp()
12+
{
13+
using (var session = OpenSession())
14+
using (var transaction = session.BeginTransaction())
15+
{
16+
var p = new Parent();
17+
session.Save(p);
18+
19+
_superParentId = (Guid) session.Save(new SuperParent { Parent = p });
20+
21+
transaction.Commit();
22+
}
23+
}
24+
25+
protected override void OnTearDown()
26+
{
27+
using (var session = OpenSession())
28+
using (var transaction = session.BeginTransaction())
29+
{
30+
// The HQL delete does all the job inside the database without loading the entities, but it does
31+
// not handle delete order for avoiding violating constraints if any. Use
32+
// session.Delete("from System.Object");
33+
// instead if in need of having NHbernate ordering the deletes, but this will cause
34+
// loading the entities in the session.
35+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
36+
37+
transaction.Commit();
38+
}
39+
}
40+
41+
[Test]
42+
public void SOEOnLoad()
43+
{
44+
using (var session = OpenSession())
45+
using (session.BeginTransaction())
46+
{
47+
var superParent = session.Load<SuperParent>(_superParentId);
48+
Assert.That(() => NHibernateUtil.Initialize(superParent), Throws.Nothing);
49+
Assert.That(() => NHibernateUtil.Initialize(superParent.Parent), Throws.Nothing);
50+
}
51+
}
52+
}
53+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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.GH1645">
4+
5+
<class name="SuperParent">
6+
<id name="Id" generator="guid.comb"/>
7+
<many-to-one name="Parent"/>
8+
</class>
9+
10+
<class name="Parent">
11+
<id name="Id" generator="guid.comb"/>
12+
<one-to-one name="SuperParent" property-ref="Parent"/>
13+
</class>
14+
15+
</hibernate-mapping>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH1645
2+
{
3+
public class Parent : EntityBase
4+
{
5+
public virtual SuperParent SuperParent { get; set; }
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH1645
2+
{
3+
public class SuperParent : EntityBase
4+
{
5+
public virtual Parent Parent { get; set; }
6+
}
7+
}

0 commit comments

Comments
 (0)