diff --git a/src/NHibernate.Test/Async/NHSpecificTest/GH1149/Fixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/GH1149/Fixture.cs new file mode 100644 index 00000000000..efa2cc99cd4 --- /dev/null +++ b/src/NHibernate.Test/Async/NHSpecificTest/GH1149/Fixture.cs @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by AsyncGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.GH1149 +{ + using System.Threading.Tasks; + [TestFixture] + public class FixtureAsync : BugTestCase + { + protected override void OnTearDown() + { + using (ISession session = OpenSession()) + using (ITransaction transaction = session.BeginTransaction()) + { + session.Delete("from Address"); + session.Delete("from Company"); + session.Delete("from AddressO2O"); + session.Delete("from CompanyO2O"); + session.Flush(); + transaction.Commit(); + } + } + + [Test] + public async Task StatelessSessionLoadsOneToOneRelatedObject_PropertyRefAsync() + { + // Create and save company and address + var companyId = 0; + + using (ISession session = OpenSession()) + { + using (ITransaction tx = session.BeginTransaction()) + { + var company = new Company { Name = "Test Company" }; + + company.Address = new Address(company) { AddressLine1 = "Company Address" }; + + companyId = (int) await (session.SaveAsync(company)); + + await (tx.CommitAsync()); + } + } + + using (var stateless = Sfi.OpenStatelessSession()) + { + var loadedCompany = await (stateless.GetAsync(companyId)); + + Assert.That(loadedCompany, Is.Not.Null); + Assert.That(loadedCompany.Name, Is.Not.Null); + Assert.That(loadedCompany.Address, Is.Not.Null); + Assert.That(loadedCompany.Address.AddressLine1, Is.Not.Null); + } + } + + [Test] + public async Task StatelessSessionLoadsOneToOneRelatedObject_WithoutPropertyRefAsync() + { + var companyId = 0; + + using (ISession session = OpenSession()) + { + using (ITransaction tx = session.BeginTransaction()) + { + var company = new CompanyO2O { Name = "Test Company" }; + var address = new AddressO2O {AddressLine1 = "Company Address"}; + + address.SetCompany(company); + + // Have to save the address to get the company to be saved as well + // Saving company doesn't save the address. + companyId = (int) await (session.SaveAsync(address)); + + await (tx.CommitAsync()); + } + } + + + using (var stateless = Sfi.OpenStatelessSession()) + { + var loadedCompany = await (stateless.GetAsync(companyId)); + + Assert.That(loadedCompany, Is.Not.Null); + Assert.That(loadedCompany.Name, Is.Not.Null); + Assert.That(loadedCompany.Address, Is.Not.Null); + Assert.That(loadedCompany.Address.AddressLine1, Is.Not.Null); + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1149/Classes.cs b/src/NHibernate.Test/NHSpecificTest/GH1149/Classes.cs new file mode 100644 index 00000000000..2b131b23c61 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1149/Classes.cs @@ -0,0 +1,51 @@ +namespace NHibernate.Test.NHSpecificTest.GH1149 +{ + public class Company + { + public virtual int Id { get; set; } + public virtual string Name { get; set; } + public virtual Address Address { get; set; } + } + + public class Address + { + public Address() + { + } + + public Address(Company company) + { + this.Company = company; + } + + public virtual int Id { get; set; } + + public virtual Company Company { get; set; } + + public virtual string AddressLine1 { get; set; } + + } + + public class CompanyO2O + { + public virtual int Id { get; set; } + public virtual string Name { get; set; } + public virtual AddressO2O Address { get; set; } + } + + public class AddressO2O + { + public virtual int Id { get; set; } + + public virtual CompanyO2O Company { get; set; } + + public virtual string AddressLine1 { get; set; } + + public virtual void SetCompany(CompanyO2O company) + { + Company = company; + company.Address = this; + } + + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1149/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/GH1149/Fixture.cs new file mode 100644 index 00000000000..152f182438c --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1149/Fixture.cs @@ -0,0 +1,87 @@ +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.GH1149 +{ + [TestFixture] + public class Fixture : BugTestCase + { + protected override void OnTearDown() + { + using (ISession session = OpenSession()) + using (ITransaction transaction = session.BeginTransaction()) + { + session.Delete("from Address"); + session.Delete("from Company"); + session.Delete("from AddressO2O"); + session.Delete("from CompanyO2O"); + session.Flush(); + transaction.Commit(); + } + } + + [Test] + public void StatelessSessionLoadsOneToOneRelatedObject_PropertyRef() + { + // Create and save company and address + var companyId = 0; + + using (ISession session = OpenSession()) + { + using (ITransaction tx = session.BeginTransaction()) + { + var company = new Company { Name = "Test Company" }; + + company.Address = new Address(company) { AddressLine1 = "Company Address" }; + + companyId = (int) session.Save(company); + + tx.Commit(); + } + } + + using (var stateless = Sfi.OpenStatelessSession()) + { + var loadedCompany = stateless.Get(companyId); + + Assert.That(loadedCompany, Is.Not.Null); + Assert.That(loadedCompany.Name, Is.Not.Null); + Assert.That(loadedCompany.Address, Is.Not.Null); + Assert.That(loadedCompany.Address.AddressLine1, Is.Not.Null); + } + } + + [Test] + public void StatelessSessionLoadsOneToOneRelatedObject_WithoutPropertyRef() + { + var companyId = 0; + + using (ISession session = OpenSession()) + { + using (ITransaction tx = session.BeginTransaction()) + { + var company = new CompanyO2O { Name = "Test Company" }; + var address = new AddressO2O {AddressLine1 = "Company Address"}; + + address.SetCompany(company); + + // Have to save the address to get the company to be saved as well + // Saving company doesn't save the address. + companyId = (int) session.Save(address); + + tx.Commit(); + } + } + + + using (var stateless = Sfi.OpenStatelessSession()) + { + var loadedCompany = stateless.Get(companyId); + + Assert.That(loadedCompany, Is.Not.Null); + Assert.That(loadedCompany.Name, Is.Not.Null); + Assert.That(loadedCompany.Address, Is.Not.Null); + Assert.That(loadedCompany.Address.AddressLine1, Is.Not.Null); + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1149/Mappings.hbm.xml b/src/NHibernate.Test/NHSpecificTest/GH1149/Mappings.hbm.xml new file mode 100644 index 00000000000..450c71d524b --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1149/Mappings.hbm.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Company + + + + + + + + + diff --git a/src/NHibernate/Impl/StatelessSessionImpl.cs b/src/NHibernate/Impl/StatelessSessionImpl.cs index 1775af7f0c4..d09e17aede4 100644 --- a/src/NHibernate/Impl/StatelessSessionImpl.cs +++ b/src/NHibernate/Impl/StatelessSessionImpl.cs @@ -228,7 +228,11 @@ public override void AfterTransactionCompletion(bool successful, ITransaction tx public override object GetContextEntityIdentifier(object obj) { - return null; + using (BeginProcess()) + { + EntityEntry entry = temporaryPersistenceContext.GetEntry(obj); + return (entry != null) ? entry.Id : null; + } } public override object Instantiate(string clazz, object id)