Skip to content

Commit 7e3d168

Browse files
committed
Added One To One with foreign key test
1 parent 8535c14 commit 7e3d168

File tree

3 files changed

+92
-16
lines changed

3 files changed

+92
-16
lines changed

src/NHibernate.Test/NHSpecificTest/GH1149/Classes.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,27 @@ public Address(Company company)
2525
public virtual string AddressLine1 { get; set; }
2626

2727
}
28+
29+
public class CompanyO2O
30+
{
31+
public virtual int Id { get; set; }
32+
public virtual string Name { get; set; }
33+
public virtual AddressO2O Address { get; set; }
34+
}
35+
36+
public class AddressO2O
37+
{
38+
public virtual int Id { get; set; }
39+
40+
public virtual CompanyO2O Company { get; set; }
41+
42+
public virtual string AddressLine1 { get; set; }
43+
44+
public virtual void SetCompany(CompanyO2O company)
45+
{
46+
Company = company;
47+
company.Address = this;
48+
}
49+
50+
}
2851
}

src/NHibernate.Test/NHSpecificTest/GH1149/Fixture.cs

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,26 @@ namespace NHibernate.Test.NHSpecificTest.GH1149
55
[TestFixture]
66
public class Fixture : BugTestCase
77
{
8-
private int _companyId = 0;
8+
protected override void OnTearDown()
9+
{
10+
using (ISession session = OpenSession())
11+
using (ITransaction transaction = session.BeginTransaction())
12+
{
13+
session.Delete("from Address");
14+
session.Delete("from Company");
15+
session.Delete("from AddressO2O");
16+
session.Delete("from CompanyO2O");
17+
session.Flush();
18+
transaction.Commit();
19+
}
20+
}
921

10-
protected override void OnSetUp()
22+
[Test]
23+
public void StatelessSessionLoadsOneToOneRelatedObject_PropertyRef()
1124
{
12-
base.OnSetUp();
25+
// Create and save company and address
26+
var companyId = 0;
27+
1328
using (ISession session = OpenSession())
1429
{
1530
using (ITransaction tx = session.BeginTransaction())
@@ -18,38 +33,55 @@ protected override void OnSetUp()
1833

1934
company.Address = new Address(company) { AddressLine1 = "Company Address" };
2035

21-
_companyId = (int) session.Save(company);
36+
companyId = (int) session.Save(company);
2237

2338
tx.Commit();
2439
}
2540
}
26-
}
2741

28-
protected override void OnTearDown()
29-
{
30-
using (ISession session = OpenSession())
31-
using (ITransaction transaction = session.BeginTransaction())
42+
using (var stateless = Sfi.OpenStatelessSession())
3243
{
33-
session.Delete("from Address");
34-
session.Delete("from Company");
35-
session.Flush();
36-
transaction.Commit();
44+
var loadedCompany = stateless.Get<Company>(companyId);
45+
46+
Assert.That(loadedCompany, Is.Not.Null);
47+
Assert.That(loadedCompany.Name, Is.Not.Null);
48+
Assert.That(loadedCompany.Address, Is.Not.Null);
49+
Assert.That(loadedCompany.Address.AddressLine1, Is.Not.Null);
3750
}
3851
}
3952

4053
[Test]
41-
public void StatelessSessionLoadsOneToOneRelatedObject()
54+
public void StatelessSessionLoadsOneToOneRelatedObject_WithoutPropertyRef()
4255
{
56+
var companyId = 0;
57+
58+
using (ISession session = OpenSession())
59+
{
60+
using (ITransaction tx = session.BeginTransaction())
61+
{
62+
var company = new CompanyO2O { Name = "Test Company" };
63+
var address = new AddressO2O {AddressLine1 = "Company Address"};
64+
65+
address.SetCompany(company);
66+
67+
// Have to save the address to get the company to be saved as well
68+
// Saving company doesn't save the address.
69+
companyId = (int) session.Save(address);
70+
71+
tx.Commit();
72+
}
73+
}
74+
75+
4376
using (var stateless = Sfi.OpenStatelessSession())
4477
{
45-
var loadedCompany = stateless.Get<Company>(_companyId);
78+
var loadedCompany = stateless.Get<CompanyO2O>(companyId);
4679

4780
Assert.That(loadedCompany, Is.Not.Null);
4881
Assert.That(loadedCompany.Name, Is.Not.Null);
4982
Assert.That(loadedCompany.Address, Is.Not.Null);
5083
Assert.That(loadedCompany.Address.AddressLine1, Is.Not.Null);
5184
}
5285
}
53-
5486
}
5587
}

src/NHibernate.Test/NHSpecificTest/GH1149/Mappings.hbm.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,28 @@
1919
<property name="AddressLine1" />
2020

2121
<many-to-one name="Company" class="Company" column="CompanyId" unique="true" />
22+
</class>
23+
24+
<class name="CompanyO2O" >
25+
<id name="Id">
26+
<generator class="native"/>
27+
</id>
28+
<property name="Name" />
29+
30+
<one-to-one name="Address"/>
31+
32+
</class>
33+
34+
<class name="AddressO2O">
35+
<id name="Id" type="int">
36+
<generator class="foreign">
37+
<param name="property">Company</param>
38+
</generator>
39+
</id>
40+
41+
<property name="AddressLine1" />
2242

43+
<one-to-one name="Company" cascade="save-update" constrained="true"/>
2344
</class>
2445

2546
</hibernate-mapping>

0 commit comments

Comments
 (0)