Permalink
Browse files

Fix for NH-3280

  • Loading branch information...
1 parent 1ba05d6 commit 9d339f646296ade73c1b5041db3bca6fbde5188a @hazzik hazzik committed Oct 2, 2012
@@ -0,0 +1,25 @@
+namespace NHibernate.Test.MappingByCode.IntegrationTests.NH3280
+{
+ public class PersonDetail : PersonDetailBase
+ {
+ //public override Person Person { get; set; }
+ }
+
+ public class PersonDetailBase
+ {
+ public virtual string LastName { get; set; }
+ public virtual Person Person { get; set; }
+ public virtual int? PersonDetailId { get; set; }
+ }
+
+ public class Person : PersonBase
+ {
+ }
+
+ public class PersonBase
+ {
+ public virtual string FirstName { get; set; }
+ public virtual PersonDetail PersonDetail { get; set; }
+ public virtual int? PersonId { get; set; }
+ }
+}
@@ -0,0 +1,96 @@
+using NHibernate.Cfg.MappingSchema;
+using NHibernate.Mapping.ByCode;
+using NUnit.Framework;
+
+namespace NHibernate.Test.MappingByCode.IntegrationTests.NH3280
+{
+ [TestFixture]
+ public class OneToOneToInheritedProperty : 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 =>
+ {
+ oo.PropertyReference(typeof (PersonDetail).GetProperty("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.Get<PersonDetail>(1);
+
+ Assert.IsNull(person2.PersonDetail);
+ Assert.IsNotNull(person1.PersonDetail);
+ Assert.AreEqual(person1.PersonDetail.LastName, personDetail.LastName);
+ Assert.AreEqual(person1.FirstName, personDetail.Person.FirstName);
+ }
+ }
+ }
+}
@@ -591,6 +591,8 @@
<Compile Include="MappingByCode\IntegrationTests\NH2825\Fixture.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH2825\FixtureByCode.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH2825\Parent.cs" />
+ <Compile Include="MappingByCode\IntegrationTests\NH3280\Domain.cs" />
+ <Compile Include="MappingByCode\IntegrationTests\NH3280\OneToOneToInheritedProperty.cs" />
<Compile Include="MappingByCode\MappersTests\AnyMapperTest.cs" />
<Compile Include="MappingByCode\MappersTests\IdMapperTest.cs" />
<Compile Include="MappingByCode\MappersTests\ManyToOneMapperTest.cs" />
@@ -73,11 +73,14 @@ public void PropertyReference(MemberInfo propertyInTheOtherSide)
return;
}
- if (_member != null && propertyInTheOtherSide.DeclaringType != _member.GetPropertyOrFieldType())
+ var declaringType = propertyInTheOtherSide.DeclaringType;
+ if (_member != null && !declaringType.IsAssignableFrom(_member.GetPropertyOrFieldType()))
{
throw new ArgumentOutOfRangeException("propertyInTheOtherSide",
- string.Format("Expected a member of {0} found the member {1} of {2}", _member.GetPropertyOrFieldType(), propertyInTheOtherSide,
- propertyInTheOtherSide.DeclaringType));
+ string.Format("Expected a member of {0} found the member {1} of {2}",
+ _member.GetPropertyOrFieldType(),
+ propertyInTheOtherSide,
+ declaringType));
}
_oneToOne.propertyref = propertyInTheOtherSide.Name;

0 comments on commit 9d339f6

Please sign in to comment.