public void Prepare() { ISession session = tc.OpenNewSession(); ITransaction txn = session.BeginTransaction(); var mother = new Human {BodyWeight = 10, Description = "mother"}; var father = new Human {BodyWeight = 15, Description = "father"}; var child1 = new Human {BodyWeight = 5, Description = "child1"}; var child2 = new Human {BodyWeight = 6, Description = "child2"}; var friend = new Human {BodyWeight = 20, Description = "friend"}; child1.Mother = mother; child1.Father = father; mother.AddOffspring(child1); father.AddOffspring(child1); child2.Mother = mother; child2.Father = father; mother.AddOffspring(child2); father.AddOffspring(child2); father.Friends = new[] {friend}; session.Save(mother); session.Save(father); session.Save(child1); session.Save(child2); session.Save(friend); txn.Commit(); session.Close(); }
public void UpdateOnImplicitJoinFails() { ISession s = OpenSession(); ITransaction t = s.BeginTransaction(); var human = new Human {Name = new Name {First = "Steve", Initial = 'E', Last = null}}; var mother = new Human {Name = new Name {First = "Jane", Initial = 'E', Last = null}}; human.Mother = (mother); s.Save(human); s.Save(mother); s.Flush(); t.Commit(); t = s.BeginTransaction(); var e = Assert.Throws<QueryException>( () => s.CreateQuery("update Human set mother.name.initial = :initial").SetString("initial", "F").ExecuteUpdate()); Assert.That(e.Message, Text.StartsWith("Implied join paths are not assignable in update")); s.CreateQuery("delete Human where mother is not null").ExecuteUpdate(); s.CreateQuery("delete Human").ExecuteUpdate(); t.Commit(); s.Close(); }
public void UpdateWithWhereExistsSubquery() { // multi-table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ISession s = OpenSession(); ITransaction t = s.BeginTransaction(); var joe = new Human {Name = new Name {First = "Joe", Initial = 'Q', Last = "Public"}}; s.Save(joe); var doll = new Human {Name = new Name {First = "Kyu", Initial = 'P', Last = "Doll"}, Friends = new[] {joe}}; s.Save(doll); t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); string updateQryString = "update Human h " + "set h.description = 'updated' " + "where exists (" + " select f.id " + " from h.friends f " + " where f.name.last = 'Public' " + ")"; int count = s.CreateQuery(updateQryString).ExecuteUpdate(); Assert.That(count, Is.EqualTo(1)); s.Delete(doll); s.Delete(joe); t.Commit(); s.Close(); // single-table (one-to-many & many-to-many) ~~~~~~~~~~~~~~~~~~~~~~~~~~ s = OpenSession(); t = s.BeginTransaction(); var entity = new SimpleEntityWithAssociation(); var other = new SimpleEntityWithAssociation(); entity.Name = "main"; other.Name = "many-to-many-association"; entity.ManyToManyAssociatedEntities.Add(other); entity.AddAssociation("one-to-many-association"); s.Save(entity); t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); // one-to-many test updateQryString = "update SimpleEntityWithAssociation e set e.Name = 'updated' where " + "exists(select a.id from e.AssociatedEntities a " + "where a.Name = 'one-to-many-association')"; count = s.CreateQuery(updateQryString).ExecuteUpdate(); Assert.That(count, Is.EqualTo(1)); // many-to-many test if (Dialect.SupportsSubqueryOnMutatingTable) { updateQryString = "update SimpleEntityWithAssociation e set e.Name = 'updated' where " + "exists(select a.id from e.ManyToManyAssociatedEntities a " + "where a.Name = 'many-to-many-association')"; count = s.CreateQuery(updateQryString).ExecuteUpdate(); Assert.That(count, Is.EqualTo(1)); } IEnumerator mtm = entity.ManyToManyAssociatedEntities.GetEnumerator(); mtm.MoveNext(); s.Delete(mtm.Current); s.Delete(entity); t.Commit(); s.Close(); }
public void UpdateOnComponent() { ISession s = OpenSession(); ITransaction t = s.BeginTransaction(); var human = new Human {Name = new Name {First = "Stevee", Initial = 'X', Last = "Ebersole"}}; s.Save(human); t.Commit(); string correctName = "Steve"; t = s.BeginTransaction(); int count = s.CreateQuery("update Human set name.first = :correction where id = :id").SetString("correction", correctName). SetInt64("id", human.Id).ExecuteUpdate(); Assert.That(count, Is.EqualTo(1), "incorrect update count"); t.Commit(); t = s.BeginTransaction(); s.Refresh(human); Assert.That(human.Name.First, Is.EqualTo(correctName), "Update did not execute properly"); s.CreateQuery("delete Human").ExecuteUpdate(); t.Commit(); s.Close(); }
public void UpdateWithWhereExistsSubquery() { // multi-table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ISession s = OpenSession(); ITransaction t = s.BeginTransaction(); var joe = new Human { Name = new Name { First = "Joe", Initial = 'Q', Last = "Public" } }; s.Save(joe); var doll = new Human { Name = new Name { First = "Kyu", Initial = 'P', Last = "Doll" }, Friends = new[] { joe } }; s.Save(doll); t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); string updateQryString = "update Human h " + "set h.description = 'updated' " + "where exists (" + " select f.id " + " from h.friends f " + " where f.name.last = 'Public' " + ")"; int count = s.CreateQuery(updateQryString).ExecuteUpdate(); Assert.That(count, Is.EqualTo(1)); s.Delete(doll); s.Delete(joe); t.Commit(); s.Close(); // single-table (one-to-many & many-to-many) ~~~~~~~~~~~~~~~~~~~~~~~~~~ s = OpenSession(); t = s.BeginTransaction(); var entity = new SimpleEntityWithAssociation(); var other = new SimpleEntityWithAssociation(); entity.Name = "main"; other.Name = "many-to-many-association"; entity.ManyToManyAssociatedEntities.Add(other); entity.AddAssociation("one-to-many-association"); s.Save(entity); t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); // one-to-many test updateQryString = "update SimpleEntityWithAssociation e set e.Name = 'updated' where " + "exists(select a.id from e.AssociatedEntities a " + "where a.Name = 'one-to-many-association')"; count = s.CreateQuery(updateQryString).ExecuteUpdate(); Assert.That(count, Is.EqualTo(1)); // many-to-many test if (Dialect.SupportsSubqueryOnMutatingTable) { updateQryString = "update SimpleEntityWithAssociation e set e.Name = 'updated' where " + "exists(select a.id from e.ManyToManyAssociatedEntities a " + "where a.Name = 'many-to-many-association')"; count = s.CreateQuery(updateQryString).ExecuteUpdate(); Assert.That(count, Is.EqualTo(1)); } IEnumerator mtm = entity.ManyToManyAssociatedEntities.GetEnumerator(); mtm.MoveNext(); s.Delete(mtm.Current); s.Delete(entity); t.Commit(); s.Close(); }