Skip to content

Commit 292e9b8

Browse files
seamus-hobanfredericDelaporte
authored andcommitted
GH1149 one-to-one related object not loaded with stateless session (#1620)
1 parent a801ec0 commit 292e9b8

File tree

5 files changed

+287
-1
lines changed

5 files changed

+287
-1
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using NUnit.Framework;
12+
13+
namespace NHibernate.Test.NHSpecificTest.GH1149
14+
{
15+
using System.Threading.Tasks;
16+
[TestFixture]
17+
public class FixtureAsync : BugTestCase
18+
{
19+
protected override void OnTearDown()
20+
{
21+
using (ISession session = OpenSession())
22+
using (ITransaction transaction = session.BeginTransaction())
23+
{
24+
session.Delete("from Address");
25+
session.Delete("from Company");
26+
session.Delete("from AddressO2O");
27+
session.Delete("from CompanyO2O");
28+
session.Flush();
29+
transaction.Commit();
30+
}
31+
}
32+
33+
[Test]
34+
public async Task StatelessSessionLoadsOneToOneRelatedObject_PropertyRefAsync()
35+
{
36+
// Create and save company and address
37+
var companyId = 0;
38+
39+
using (ISession session = OpenSession())
40+
{
41+
using (ITransaction tx = session.BeginTransaction())
42+
{
43+
var company = new Company { Name = "Test Company" };
44+
45+
company.Address = new Address(company) { AddressLine1 = "Company Address" };
46+
47+
companyId = (int) await (session.SaveAsync(company));
48+
49+
await (tx.CommitAsync());
50+
}
51+
}
52+
53+
using (var stateless = Sfi.OpenStatelessSession())
54+
{
55+
var loadedCompany = await (stateless.GetAsync<Company>(companyId));
56+
57+
Assert.That(loadedCompany, Is.Not.Null);
58+
Assert.That(loadedCompany.Name, Is.Not.Null);
59+
Assert.That(loadedCompany.Address, Is.Not.Null);
60+
Assert.That(loadedCompany.Address.AddressLine1, Is.Not.Null);
61+
}
62+
}
63+
64+
[Test]
65+
public async Task StatelessSessionLoadsOneToOneRelatedObject_WithoutPropertyRefAsync()
66+
{
67+
var companyId = 0;
68+
69+
using (ISession session = OpenSession())
70+
{
71+
using (ITransaction tx = session.BeginTransaction())
72+
{
73+
var company = new CompanyO2O { Name = "Test Company" };
74+
var address = new AddressO2O {AddressLine1 = "Company Address"};
75+
76+
address.SetCompany(company);
77+
78+
// Have to save the address to get the company to be saved as well
79+
// Saving company doesn't save the address.
80+
companyId = (int) await (session.SaveAsync(address));
81+
82+
await (tx.CommitAsync());
83+
}
84+
}
85+
86+
87+
using (var stateless = Sfi.OpenStatelessSession())
88+
{
89+
var loadedCompany = await (stateless.GetAsync<CompanyO2O>(companyId));
90+
91+
Assert.That(loadedCompany, Is.Not.Null);
92+
Assert.That(loadedCompany.Name, Is.Not.Null);
93+
Assert.That(loadedCompany.Address, Is.Not.Null);
94+
Assert.That(loadedCompany.Address.AddressLine1, Is.Not.Null);
95+
}
96+
}
97+
}
98+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH1149
2+
{
3+
public class Company
4+
{
5+
public virtual int Id { get; set; }
6+
public virtual string Name { get; set; }
7+
public virtual Address Address { get; set; }
8+
}
9+
10+
public class Address
11+
{
12+
public Address()
13+
{
14+
}
15+
16+
public Address(Company company)
17+
{
18+
this.Company = company;
19+
}
20+
21+
public virtual int Id { get; set; }
22+
23+
public virtual Company Company { get; set; }
24+
25+
public virtual string AddressLine1 { get; set; }
26+
27+
}
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+
}
51+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using NUnit.Framework;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH1149
4+
{
5+
[TestFixture]
6+
public class Fixture : BugTestCase
7+
{
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+
}
21+
22+
[Test]
23+
public void StatelessSessionLoadsOneToOneRelatedObject_PropertyRef()
24+
{
25+
// Create and save company and address
26+
var companyId = 0;
27+
28+
using (ISession session = OpenSession())
29+
{
30+
using (ITransaction tx = session.BeginTransaction())
31+
{
32+
var company = new Company { Name = "Test Company" };
33+
34+
company.Address = new Address(company) { AddressLine1 = "Company Address" };
35+
36+
companyId = (int) session.Save(company);
37+
38+
tx.Commit();
39+
}
40+
}
41+
42+
using (var stateless = Sfi.OpenStatelessSession())
43+
{
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);
50+
}
51+
}
52+
53+
[Test]
54+
public void StatelessSessionLoadsOneToOneRelatedObject_WithoutPropertyRef()
55+
{
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+
76+
using (var stateless = Sfi.OpenStatelessSession())
77+
{
78+
var loadedCompany = stateless.Get<CompanyO2O>(companyId);
79+
80+
Assert.That(loadedCompany, Is.Not.Null);
81+
Assert.That(loadedCompany.Name, Is.Not.Null);
82+
Assert.That(loadedCompany.Address, Is.Not.Null);
83+
Assert.That(loadedCompany.Address.AddressLine1, Is.Not.Null);
84+
}
85+
}
86+
}
87+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
namespace="NHibernate.Test.NHSpecificTest.GH1149"
4+
assembly="NHibernate.Test">
5+
<class name="Company" >
6+
<id name="Id">
7+
<generator class="native"/>
8+
</id>
9+
<property name="Name" />
10+
11+
<one-to-one name="Address" class="Address" property-ref="Company" cascade="all-delete-orphan"/>
12+
13+
</class>
14+
15+
<class name="Address" >
16+
<id name="Id">
17+
<generator class="native"/>
18+
</id>
19+
<property name="AddressLine1" />
20+
21+
<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" />
42+
43+
<one-to-one name="Company" cascade="save-update" constrained="true"/>
44+
</class>
45+
46+
</hibernate-mapping>

src/NHibernate/Impl/StatelessSessionImpl.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ public override void AfterTransactionCompletion(bool successful, ITransaction tx
228228

229229
public override object GetContextEntityIdentifier(object obj)
230230
{
231-
return null;
231+
using (BeginProcess())
232+
{
233+
EntityEntry entry = temporaryPersistenceContext.GetEntry(obj);
234+
return (entry != null) ? entry.Id : null;
235+
}
232236
}
233237

234238
public override object Instantiate(string clazz, object id)

0 commit comments

Comments
 (0)