|
| 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 | +} |
0 commit comments