Skip to content

GH1149 one-to-one related object not loaded with stateless session #1620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1149/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


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<Company>(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<CompanyO2O>(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);
}
}
}
}
51 changes: 51 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1149/Classes.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}
}
87 changes: 87 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1149/Fixture.cs
Original file line number Diff line number Diff line change
@@ -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<Company>(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<CompanyO2O>(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);
}
}
}
}
46 changes: 46 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1149/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHibernate.Test.NHSpecificTest.GH1149"
assembly="NHibernate.Test">
<class name="Company" >
<id name="Id">
<generator class="native"/>
</id>
<property name="Name" />

<one-to-one name="Address" class="Address" property-ref="Company" cascade="all-delete-orphan"/>
Copy link
Member

@fredericDelaporte fredericDelaporte Mar 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May you add a test with an actual one-to-one in db? I mean, a one-to-one not using a property-ref but with one entity pk being also the pk of the other entity (so a pk which is also a fk), both side mapped as one-to-one with one side being constrained. (PersonTrueO2O with AddressTrueO2O in NH3931 tests is an example of this.)

Unless this case is already tested in NHibernate.Test and working (meaning the issue report was not really precise).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, PR updated.


</class>

<class name="Address" >
<id name="Id">
<generator class="native"/>
</id>
<property name="AddressLine1" />

<many-to-one name="Company" class="Company" column="CompanyId" unique="true" />
</class>

<class name="CompanyO2O" >
<id name="Id">
<generator class="native"/>
</id>
<property name="Name" />

<one-to-one name="Address"/>

</class>

<class name="AddressO2O">
<id name="Id" type="int">
<generator class="foreign">
<param name="property">Company</param>
</generator>
</id>

<property name="AddressLine1" />

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

</hibernate-mapping>
6 changes: 5 additions & 1 deletion src/NHibernate/Impl/StatelessSessionImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down