Skip to content

Commit 96ad67b

Browse files
committed
Merge pull request #297 from rjperes/NH-3657
NH-3657
2 parents 59d378e + d1629f9 commit 96ad67b

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System.Linq;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Linq;
4+
using NHibernate.Mapping.ByCode;
5+
using NUnit.Framework;
6+
using NHibernate.Test.MappingByCode.IntegrationTests.NH3041;
7+
8+
namespace NHibernate.Test.MappingByCode.IntegrationTests.NH3657
9+
{
10+
[TestFixture]
11+
public class OneToOneToPropertyReferenceWithExplicitClassSet : TestCaseMappingByCode
12+
{
13+
protected override void OnSetUp()
14+
{
15+
using (var session = OpenSession())
16+
using (var tx = session.BeginTransaction())
17+
{
18+
var person1 = new Person { FirstName = "Jack" };
19+
session.Save(person1);
20+
21+
var person2 = new Person { FirstName = "Robert" };
22+
session.Save(person2);
23+
24+
var personDetail = new PersonDetail { LastName = "Smith", Person = person1 };
25+
session.Save(personDetail);
26+
27+
tx.Commit();
28+
}
29+
}
30+
31+
protected override void OnTearDown()
32+
{
33+
using (var session = OpenSession())
34+
using (var tx = session.BeginTransaction())
35+
{
36+
session.Delete("from System.Object");
37+
tx.Commit();
38+
}
39+
}
40+
41+
protected override HbmMapping GetMappings()
42+
{
43+
var mapper = new ModelMapper();
44+
mapper.Class<PersonDetail>(m =>
45+
{
46+
m.Id(t => t.PersonDetailId, a => a.Generator(Generators.Identity));
47+
m.Property(t => t.LastName,
48+
c =>
49+
{
50+
c.NotNullable(true);
51+
c.Length(32);
52+
});
53+
m.ManyToOne(t => t.Person,
54+
c =>
55+
{
56+
c.Column("PersonId");
57+
c.Unique(true);
58+
c.NotNullable(false);
59+
c.NotFound(NotFoundMode.Ignore);
60+
});
61+
});
62+
63+
mapper.Class<Person>(m =>
64+
{
65+
m.Id(t => t.PersonId, a => a.Generator(Generators.Identity));
66+
m.Property(t => t.FirstName,
67+
c =>
68+
{
69+
c.NotNullable(true);
70+
c.Length(32);
71+
});
72+
m.OneToOne(t => t.PersonDetail,
73+
oo =>
74+
{
75+
//NH-3657
76+
oo.Class(typeof(PersonDetail));
77+
oo.PropertyReference(x => x.Person);
78+
oo.Cascade(Mapping.ByCode.Cascade.All);
79+
});
80+
});
81+
82+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
83+
}
84+
85+
[Test]
86+
public void ShouldConfigureSessionCorrectly()
87+
{
88+
using (var session = OpenSession())
89+
{
90+
var person1 = session.Get<Person>(1);
91+
var person2 = session.Get<Person>(2);
92+
var personDetail = session.Query<PersonDetail>().Single();
93+
94+
Assert.IsNull(person2.PersonDetail);
95+
Assert.IsNotNull(person1.PersonDetail);
96+
Assert.AreEqual(person1.PersonDetail.LastName, personDetail.LastName);
97+
Assert.AreEqual(person1.FirstName, personDetail.Person.FirstName);
98+
}
99+
}
100+
}
101+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@
592592
<Compile Include="MappingByCode\For.cs" />
593593
<Compile Include="MappingByCode\ImportTests.cs" />
594594
<Compile Include="MappingByCode\IntegrationTests\NH3041\Domain.cs" />
595+
<Compile Include="MappingByCode\IntegrationTests\NH3657\OneToOneToPropertyReferenceWithExplicitClassSet.cs" />
595596
<Compile Include="MappingByCode\IntegrationTests\NH3041\OneToOneToPropertyReference.cs" />
596597
<Compile Include="MappingByCode\IntegrationTests\NH2728\Cat.cs" />
597598
<Compile Include="MappingByCode\IntegrationTests\NH2728\Dog.cs" />
@@ -3630,4 +3631,4 @@ if exist hibernate.cfg.xml (del hibernate.cfg.xml)
36303631
if exist "$(ProjectDir)hibernate.cfg.xml" (copy "$(ProjectDir)hibernate.cfg.xml" "hibernate.cfg.xml")
36313632
copy /y "..\..\..\NHibernate.DomainModel\ABC.hbm.xml" "ABC.hbm.xml"</PostBuildEvent>
36323633
</PropertyGroup>
3633-
</Project>
3634+
</Project>

src/NHibernate/Mapping/ByCode/IOneToOneMapper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public interface IOneToOneMapper : IEntityPropertyMapper
1212
void PropertyReference(MemberInfo propertyInTheOtherSide);
1313
void Formula(string formula);
1414
void ForeignKey(string foreignKeyName);
15+
void Class(System.Type clazz);
1516
}
1617

1718
public interface IOneToOneMapper<T> : IOneToOneMapper

src/NHibernate/Mapping/ByCode/Impl/OneToOneMapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public void Cascade(Cascade cascadeStyle)
3232
_oneToOne.cascade = (cascadeStyle.Exclude(ByCode.Cascade.DeleteOrphans)).ToCascadeString();
3333
}
3434

35+
public void Class(System.Type clazz)
36+
{
37+
_oneToOne.@class = clazz.FullName;
38+
}
39+
3540
#endregion
3641

3742
#region Implementation of IAccessorPropertyMapper

0 commit comments

Comments
 (0)