Skip to content

NH-3657 #297

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
merged 2 commits into from
Aug 17, 2014
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Linq;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
using NHibernate.Test.MappingByCode.IntegrationTests.NH3041;

namespace NHibernate.Test.MappingByCode.IntegrationTests.NH3657
{
[TestFixture]
public class OneToOneToPropertyReferenceWithExplicitClassSet : TestCaseMappingByCode
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var person1 = new Person { FirstName = "Jack" };
session.Save(person1);

var person2 = new Person { FirstName = "Robert" };
session.Save(person2);

var personDetail = new PersonDetail { LastName = "Smith", Person = person1 };
session.Save(personDetail);

tx.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
session.Delete("from System.Object");
tx.Commit();
}
}

protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<PersonDetail>(m =>
{
m.Id(t => t.PersonDetailId, a => a.Generator(Generators.Identity));
m.Property(t => t.LastName,
c =>
{
c.NotNullable(true);
c.Length(32);
});
m.ManyToOne(t => t.Person,
c =>
{
c.Column("PersonId");
c.Unique(true);
c.NotNullable(false);
c.NotFound(NotFoundMode.Ignore);
});
});

mapper.Class<Person>(m =>
{
m.Id(t => t.PersonId, a => a.Generator(Generators.Identity));
m.Property(t => t.FirstName,
c =>
{
c.NotNullable(true);
c.Length(32);
});
m.OneToOne(t => t.PersonDetail,
oo =>
{
//NH-3657
oo.Class(typeof(PersonDetail));
oo.PropertyReference(x => x.Person);
oo.Cascade(Mapping.ByCode.Cascade.All);
});
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

[Test]
public void ShouldConfigureSessionCorrectly()
{
using (var session = OpenSession())
{
var person1 = session.Get<Person>(1);
var person2 = session.Get<Person>(2);
var personDetail = session.Query<PersonDetail>().Single();

Assert.IsNull(person2.PersonDetail);
Assert.IsNotNull(person1.PersonDetail);
Assert.AreEqual(person1.PersonDetail.LastName, personDetail.LastName);
Assert.AreEqual(person1.FirstName, personDetail.Person.FirstName);
}
}
}
}
3 changes: 2 additions & 1 deletion src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@
<Compile Include="MappingByCode\For.cs" />
<Compile Include="MappingByCode\ImportTests.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH3041\Domain.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH3657\OneToOneToPropertyReferenceWithExplicitClassSet.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH3041\OneToOneToPropertyReference.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH2728\Cat.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH2728\Dog.cs" />
Expand Down Expand Up @@ -3630,4 +3631,4 @@ if exist hibernate.cfg.xml (del hibernate.cfg.xml)
if exist "$(ProjectDir)hibernate.cfg.xml" (copy "$(ProjectDir)hibernate.cfg.xml" "hibernate.cfg.xml")
copy /y "..\..\..\NHibernate.DomainModel\ABC.hbm.xml" "ABC.hbm.xml"</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>
1 change: 1 addition & 0 deletions src/NHibernate/Mapping/ByCode/IOneToOneMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IOneToOneMapper : IEntityPropertyMapper
void PropertyReference(MemberInfo propertyInTheOtherSide);
void Formula(string formula);
void ForeignKey(string foreignKeyName);
void Class(System.Type clazz);
}

public interface IOneToOneMapper<T> : IOneToOneMapper
Expand Down
5 changes: 5 additions & 0 deletions src/NHibernate/Mapping/ByCode/Impl/OneToOneMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public void Cascade(Cascade cascadeStyle)
_oneToOne.cascade = (cascadeStyle.Exclude(ByCode.Cascade.DeleteOrphans)).ToCascadeString();
}

public void Class(System.Type clazz)
{
_oneToOne.@class = clazz.FullName;
}

#endregion

#region Implementation of IAccessorPropertyMapper
Expand Down