diff --git a/src/NHibernate.Test/Extralazy/ExtraLazyFixture.cs b/src/NHibernate.Test/Extralazy/ExtraLazyFixture.cs index b6652c499ef..46437ade7ab 100644 --- a/src/NHibernate.Test/Extralazy/ExtraLazyFixture.cs +++ b/src/NHibernate.Test/Extralazy/ExtraLazyFixture.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Linq; using NUnit.Framework; namespace NHibernate.Test.Extralazy @@ -28,11 +29,11 @@ public void ExtraLazyWithWhereClause() using (ISession s = OpenSession()) using (ITransaction t = s.BeginTransaction()) { - s.CreateSQLQuery("insert into Users (Name,Password) values('gavin','secret')") + s.CreateSQLQuery("insert into users (Name,Password) values('gavin','secret')") .UniqueResult(); - s.CreateSQLQuery("insert into Photos (Title,Owner) values('PRVaaa','gavin')") + s.CreateSQLQuery("insert into photos (Title,Owner) values('PRVaaa','gavin')") .UniqueResult(); - s.CreateSQLQuery("insert into Photos (Title,Owner) values('PUBbbb','gavin')") + s.CreateSQLQuery("insert into photos (Title,Owner) values('PUBbbb','gavin')") .UniqueResult(); t.Commit(); } @@ -47,9 +48,9 @@ public void ExtraLazyWithWhereClause() using (ISession s = OpenSession()) using (ITransaction t = s.BeginTransaction()) { - s.CreateSQLQuery("delete from Photos") + s.CreateSQLQuery("delete from photos") .UniqueResult(); - s.CreateSQLQuery("delete from Users") + s.CreateSQLQuery("delete from users") .UniqueResult(); t.Commit(); @@ -72,7 +73,7 @@ public void OrphanDelete() hia = new Document("HiA", "blah blah blah", gavin); hia2 = new Document("HiA2", "blah blah blah blah", gavin); gavin.Documents.Add(hia); // NH: added ; I don't understand how can work in H3.2.5 without add - gavin.Documents.Add(hia2); // NH: added + gavin.Documents.Add(hia2); // NH: added s.Persist(gavin); t.Commit(); } @@ -106,6 +107,52 @@ public void OrphanDelete() } } + [Test] + public void OrphanEnumerableDelete() + { + User gavin = null; + Widget widget = null; + Widget widget2 = null; + + using (var s = OpenSession()) + using (var t = s.BeginTransaction()) + { + gavin = new User("gavin", "secret"); + gavin.AddWidget("widg1", "blah blah blah"); + gavin.AddWidget("widg2", "blah blah blah blah"); + s.Persist(gavin); + t.Commit(); + } + + using (var s = OpenSession()) + using (var t = s.BeginTransaction()) + { + gavin = s.Get("gavin"); + widget = s.Get("widg1"); + //Uncomment this to initialise the collection and make the test pass: + //Assert.IsTrue(gavin.Widgets.Any(w => w == widget)); + gavin.RemoveWidget(widget); + Assert.IsFalse(gavin.Widgets.Any(w => w == widget)); + Assert.IsTrue(gavin.Widgets.Any(w => w.Title == "widg2")); + Assert.AreEqual(1, gavin.Widgets.Count()); + t.Commit(); + } + + using (var s = OpenSession()) + using (var t = s.BeginTransaction()) + { + gavin = s.Get("gavin"); + Assert.AreEqual(1, gavin.Widgets.Count()); + Assert.IsFalse(gavin.Widgets.Any(w => w.Title == "widg1")); + Assert.IsTrue(gavin.Widgets.Any(w => w.Title == "widg2")); + Assert.That(s.Get("widg1"), Is.Null); + widget2 = s.Get("widg2"); + gavin.RemoveWidget(widget2); + s.Delete(gavin); + t.Commit(); + } + } + [Test] public void Get() { @@ -329,4 +376,4 @@ public void AddToUninitializedSetWithLaterLazyLoad() } } } -} \ No newline at end of file +} diff --git a/src/NHibernate.Test/Extralazy/User.cs b/src/NHibernate.Test/Extralazy/User.cs index 5279d6d0dd6..b397c4a3804 100644 --- a/src/NHibernate.Test/Extralazy/User.cs +++ b/src/NHibernate.Test/Extralazy/User.cs @@ -9,6 +9,7 @@ public class User private IDictionary session = new Dictionary(); private ISet documents = new HashSet(); private ISet photos = new HashSet(); + private ISet widgets = new HashSet(); protected User() {} public User(string name, string password) { @@ -46,5 +47,20 @@ public virtual ISet Photos get { return photos; } set { photos = value; } } + + public virtual IEnumerable Widgets + { + get { return widgets; } + } + + public virtual void AddWidget(string title, string description) + { + widgets.Add(new Widget(title, description, this)); + } + + public virtual void RemoveWidget(Widget widget) + { + widgets.Remove(widget); + } } -} \ No newline at end of file +} diff --git a/src/NHibernate.Test/Extralazy/UserGroup.hbm.xml b/src/NHibernate.Test/Extralazy/UserGroup.hbm.xml index 19d201f2bd4..6137021455e 100644 --- a/src/NHibernate.Test/Extralazy/UserGroup.hbm.xml +++ b/src/NHibernate.Test/Extralazy/UserGroup.hbm.xml @@ -12,7 +12,7 @@ - + @@ -29,6 +29,10 @@ + + + + @@ -39,7 +43,12 @@ - + + + + + + @@ -48,22 +57,22 @@ - - + - select - lower(u.name) as {u.Name}, lower(u.password) as {u.Password}, - lower(s.userName) as {s.key}, lower(s.name) as {s.index}, s.id as {s.element}, + select + lower(u.name) as {u.Name}, lower(u.password) as {u.Password}, + lower(s.userName) as {s.key}, lower(s.name) as {s.index}, s.id as {s.element}, {s.element.*} - from users u + from users u join session_attributes s on lower(s.userName) = lower(u.name) where u.name like :uname - - + + diff --git a/src/NHibernate.Test/Extralazy/Widget.cs b/src/NHibernate.Test/Extralazy/Widget.cs new file mode 100644 index 00000000000..0d937e50c65 --- /dev/null +++ b/src/NHibernate.Test/Extralazy/Widget.cs @@ -0,0 +1,62 @@ +namespace NHibernate.Test.Extralazy +{ + public class Widget + { + private string title; + private string description; + private User owner; + + protected Widget() { } + public Widget(string title, string description, User owner) + { + this.title = title; + this.description = description; + this.owner = owner; + } + + public virtual string Title + { + get { return title; } + set { title = value; } + } + + public virtual string Description + { + get { return description; } + set { description = value; } + } + + public virtual User Owner + { + get { return owner; } + set { owner = value; } + } + + public override bool Equals(object obj) + { + var other = obj as Widget; + + if (other == null) + { + return false; + } + + return Title.Equals(other.Title); + } + + public static bool operator ==(Widget lhs, Widget rhs) + { + return Equals(lhs, rhs); + } + + public static bool operator !=(Widget lhs, Widget rhs) + { + return !Equals(lhs, rhs); + } + + public override int GetHashCode() + { + return Title.GetHashCode(); + } + } +}