Skip to content

Commit 635da76

Browse files
mickfoldmickfold
authored andcommitted
For NH-1262: Ported cascade delete orphans functionality for one-to-one mappings from Hibernate to NHibernate.
1 parent b434f83 commit 635da76

33 files changed

+13115
-11
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using NUnit.Framework;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH1262.fk.bidirectional
4+
{
5+
[TestFixture]
6+
public class DeleteOneToOneOrphansTest : BugTestCase
7+
{
8+
public override string BugNumber
9+
{
10+
get { return "NH1262.fk.bidirectional"; }
11+
}
12+
13+
protected override void OnSetUp()
14+
{
15+
base.OnSetUp();
16+
using (var s = OpenSession())
17+
using (var t = s.BeginTransaction())
18+
{
19+
var emp = new Employee();
20+
emp.info = new EmployeeInfo(emp);
21+
22+
s.Save(emp);
23+
t.Commit();
24+
}
25+
}
26+
27+
protected override void OnTearDown()
28+
{
29+
base.OnTearDown();
30+
31+
using (var session = OpenSession())
32+
using (var tx = session.BeginTransaction())
33+
{
34+
session.Delete("from EmployeeInfo");
35+
session.Delete("from Employee");
36+
tx.Commit();
37+
}
38+
}
39+
40+
[Test]
41+
public void testOrphanedWhileManaged()
42+
{
43+
long empId = 0;
44+
45+
using (var s = OpenSession())
46+
using (var t = s.BeginTransaction())
47+
{
48+
var empInfoResults = s.CreateQuery("from EmployeeInfo").List<EmployeeInfo>();
49+
Assert.AreEqual(1, empInfoResults.Count);
50+
51+
var empResults = s.CreateQuery("from Employee").List<Employee>();
52+
Assert.AreEqual(1, empResults.Count);
53+
54+
var emp = empResults[0];
55+
Assert.NotNull(emp);
56+
57+
empId = emp.id;
58+
emp.info = null;
59+
t.Commit();
60+
}
61+
62+
using (var s = OpenSession())
63+
using (var t = s.BeginTransaction())
64+
{
65+
var emp = s.Get<Employee>(empId);
66+
Assert.Null(emp.info);
67+
68+
var empInfoResults = s.CreateQuery("from EmployeeInfo").List<EmployeeInfo>();
69+
Assert.AreEqual(0, empInfoResults.Count);
70+
71+
var empResults = s.CreateQuery("from Employee").List<Employee>();
72+
Assert.AreEqual(1, empResults.Count);
73+
74+
t.Commit();
75+
}
76+
}
77+
78+
}
79+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
namespace="NHibernate.Test.NHSpecificTest.NH1262.fk.bidirectional"
4+
assembly="NHibernate.Test">
5+
6+
<class name="Employee">
7+
<id name="id" type="long" column="id">
8+
<generator class="increment" />
9+
</id>
10+
<one-to-one name="info"
11+
property-ref="employee"
12+
class="EmployeeInfo"
13+
cascade="all,delete-orphan"
14+
constrained="false" />
15+
</class>
16+
17+
<class name="EmployeeInfo">
18+
<id name="id" type="long" column="id">
19+
<generator class="increment" />
20+
</id>
21+
<many-to-one name="employee"
22+
column="employee_id"
23+
unique="true"
24+
not-null="true" />
25+
</class>
26+
27+
</hibernate-mapping>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+

2+
namespace NHibernate.Test.NHSpecificTest.NH1262.fk.bidirectional
3+
{
4+
public class Employee
5+
{
6+
public virtual long id { get; set; }
7+
public virtual EmployeeInfo info { get; set; }
8+
9+
public Employee()
10+
{
11+
12+
}
13+
}
14+
15+
public class EmployeeInfo
16+
{
17+
public virtual long id { get; set; }
18+
public virtual Employee employee { get; set; }
19+
20+
public EmployeeInfo()
21+
{
22+
23+
}
24+
25+
public EmployeeInfo(Employee emp)
26+
{
27+
employee = emp;
28+
}
29+
}
30+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using NUnit.Framework;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH1262.fk.composite
4+
{
5+
public class DeleteOneToOneOrphansTest : BugTestCase
6+
{
7+
8+
public override string BugNumber
9+
{
10+
get { return "NH1262.fk.composite"; }
11+
}
12+
13+
protected override void OnSetUp()
14+
{
15+
base.OnSetUp();
16+
using (var s = OpenSession())
17+
using (var t = s.BeginTransaction())
18+
{
19+
var emp = new Employee();
20+
emp.info = new EmployeeInfo( 1L, 1L);
21+
22+
s.Save(emp);
23+
t.Commit();
24+
}
25+
}
26+
27+
protected override void OnTearDown()
28+
{
29+
base.OnTearDown();
30+
31+
using (var session = OpenSession())
32+
using (var tx = session.BeginTransaction())
33+
{
34+
session.Delete("from EmployeeInfo");
35+
session.Delete("from Employee");
36+
tx.Commit();
37+
}
38+
}
39+
40+
41+
[Test]
42+
public void testOrphanedWhileManaged()
43+
{
44+
long empId = 0;
45+
46+
using (var s = OpenSession())
47+
using (var t = s.BeginTransaction())
48+
{
49+
var infoList = s.CreateQuery( "from EmployeeInfo" ).List<EmployeeInfo>();
50+
Assert.AreEqual(1, infoList.Count );
51+
52+
var empList = s.CreateQuery( "from Employee" ).List<Employee>();
53+
Assert.AreEqual(1, empList.Count);
54+
55+
var emp = empList[0];
56+
Assert.NotNull(emp.info);
57+
58+
empId = emp.id;
59+
emp.info = null ;
60+
t.Commit();
61+
62+
}
63+
64+
65+
using (var s = OpenSession())
66+
using (var t = s.BeginTransaction())
67+
{
68+
var emp = s.Get<Employee>(empId);
69+
Assert.IsNull(emp.info);
70+
71+
var empInfoList = s.CreateQuery("from EmployeeInfo").List<EmployeeInfo>();
72+
Assert.AreEqual(0, empInfoList.Count);
73+
74+
var empList = s.CreateQuery("from Employee").List<Employee>();
75+
Assert.AreEqual(1, empList.Count);
76+
}
77+
}
78+
}
79+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
namespace="NHibernate.Test.NHSpecificTest.NH1262.fk.composite"
4+
assembly="NHibernate.Test">
5+
6+
<class name="Employee">
7+
<id name="id" type="long" column="id">
8+
<generator class="increment" />
9+
</id>
10+
<many-to-one name="info" unique="true" cascade="all,delete-orphan" not-found="exception">
11+
<column name="COMP_ID" />
12+
<column name="PERS_ID" />
13+
</many-to-one>
14+
</class>
15+
16+
<class name="EmployeeInfo">
17+
<composite-id class="EmployeeInfo+Id" name="id">
18+
<key-property name="companyId" column="COMP_ID" />
19+
<key-property name="personId" column="PERS_ID" />
20+
</composite-id>
21+
</class>
22+
23+
</hibernate-mapping>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH1262.fk.composite
4+
{
5+
public class Employee
6+
{
7+
public virtual long id { get; set; }
8+
public virtual EmployeeInfo info { get; set; }
9+
10+
public Employee()
11+
{
12+
13+
}
14+
}
15+
16+
public class EmployeeInfo
17+
{
18+
public class Id
19+
{
20+
public virtual long companyId { get; set; }
21+
public virtual long personId { get; set; }
22+
23+
public Id()
24+
{
25+
26+
}
27+
28+
public Id(long companyId, long personId)
29+
{
30+
this.companyId = companyId;
31+
this.personId = personId;
32+
}
33+
34+
35+
public override bool Equals(Object o)
36+
{
37+
if (this == o)
38+
{
39+
return true;
40+
}
41+
42+
var t = this.GetType();
43+
var u = o.GetType();
44+
45+
46+
if (o == null || !t.IsAssignableFrom(u) || !u.IsAssignableFrom(t))
47+
{
48+
return false;
49+
}
50+
51+
var id = o as Id;
52+
53+
return companyId.Equals(id.companyId)
54+
&& personId.Equals(id.personId);
55+
56+
}
57+
58+
public override int GetHashCode()
59+
{
60+
return (31 * companyId.GetHashCode()) + personId.GetHashCode();
61+
}
62+
}
63+
64+
public virtual Id id { get; set; }
65+
public virtual Employee employee { get; set; }
66+
67+
public EmployeeInfo()
68+
{
69+
70+
}
71+
72+
public EmployeeInfo(long companyId, long personId)
73+
{
74+
this.id = new Id(companyId, personId);
75+
}
76+
77+
public EmployeeInfo(Id id)
78+
{
79+
this.id = id;
80+
}
81+
}
82+
83+
84+
}

0 commit comments

Comments
 (0)