|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using NHibernate.Cfg.MappingSchema; |
| 4 | +using NHibernate.Mapping.ByCode; |
| 5 | +using NUnit.Framework; |
| 6 | + |
| 7 | +namespace NHibernate.Test.NHSpecificTest.GH1413 |
| 8 | +{ |
| 9 | + [TestFixture] |
| 10 | + public class ByCodeFixture : TestCaseMappingByCode |
| 11 | + { |
| 12 | + private Guid ParentId; |
| 13 | + |
| 14 | + protected override HbmMapping GetMappings() |
| 15 | + { |
| 16 | + var mapper = new ModelMapper(); |
| 17 | + mapper.Class<EntityParent>(rc => |
| 18 | + { |
| 19 | + rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); |
| 20 | + rc.Property(x => x.Name); |
| 21 | + rc.ManyToOne(ep => ep.Child); |
| 22 | + rc.Bag(x => x.Children, m => |
| 23 | + { |
| 24 | + m.Cascade(Mapping.ByCode.Cascade.All); |
| 25 | + m.Inverse(true); |
| 26 | + }, a => a.OneToMany() |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + mapper.Class<EntityChild>(rc => |
| 31 | + { |
| 32 | + rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); |
| 33 | + rc.Property(x => x.Name); |
| 34 | + }); |
| 35 | + |
| 36 | + return mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 37 | + } |
| 38 | + |
| 39 | + protected override void OnSetUp() |
| 40 | + { |
| 41 | + using (ISession session = OpenSession()) |
| 42 | + using (ITransaction transaction = session.BeginTransaction()) |
| 43 | + { |
| 44 | + var child = new EntityChild { Name = "InitialChild" }; |
| 45 | + var parent = new EntityParent |
| 46 | + { |
| 47 | + Name = "InitialParent", |
| 48 | + Child = child |
| 49 | + }; |
| 50 | + session.Save(child); |
| 51 | + session.Save(parent); |
| 52 | + |
| 53 | + session.Flush(); |
| 54 | + transaction.Commit(); |
| 55 | + ParentId = parent.Id; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + protected override void OnTearDown() |
| 60 | + { |
| 61 | + using (ISession session = OpenSession()) |
| 62 | + using (ITransaction transaction = session.BeginTransaction()) |
| 63 | + { |
| 64 | + session.Delete("from System.Object"); |
| 65 | + |
| 66 | + session.Flush(); |
| 67 | + transaction.Commit(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + public void SessionIsDirtyShouldNotFailForNewManyToOneObject() |
| 73 | + { |
| 74 | + using (ISession session = OpenSession()) |
| 75 | + using (session.BeginTransaction()) |
| 76 | + { |
| 77 | + EntityParent parent = GetParent(session); |
| 78 | + |
| 79 | + //parent.Child entity is not cascaded, I want to save it explictilty later |
| 80 | + parent.Child = new EntityChild { Name = "NewManyToOneChild" }; |
| 81 | + |
| 82 | + bool isDirty = false; |
| 83 | + Assert.That(() => isDirty = session.IsDirty(), Throws.Nothing, () => "ISession.IsDirty() call should not fail for transient many-to-one object referenced in session."); |
| 84 | + Assert.That(isDirty, "ISession.IsDirty() call should return true."); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + public void SessionIsDirtyShouldNotTriggerCascadeSaving() |
| 90 | + { |
| 91 | + using (ISession session = OpenSession()) |
| 92 | + using (session.BeginTransaction()) |
| 93 | + { |
| 94 | + var parent = GetParent(session); |
| 95 | + var entityChild = new EntityChild |
| 96 | + { |
| 97 | + Name = "NewListElem" |
| 98 | + }; |
| 99 | + |
| 100 | + //parent.Children is cascaded |
| 101 | + parent.Children.Add(entityChild); |
| 102 | + |
| 103 | + var isDirty = session.IsDirty(); |
| 104 | + Assert.That(entityChild.Id, Is.EqualTo(Guid.Empty), "Transient objects should not be saved by ISession.IsDirty() call (expected empty Guid Id)"); |
| 105 | + Assert.That(isDirty, "ISession.IsDirty() call should return true."); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private EntityParent GetParent(ISession session) |
| 110 | + { |
| 111 | + return session.Get<EntityParent>(ParentId); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments