public void FindBySQLStar() { ISession session = OpenSession(); Category s = new Category(); s.Name = nextLong.ToString(); nextLong++; session.Save( s ); Simple simple = new Simple(); simple.Init(); session.Save( simple, nextLong++ ); A a = new A(); session.Save( a ); //B b = new B(); //session.Save( b ); session.CreateSQLQuery( "select {category.*} from Category {category}", "category", typeof( Category ) ).List(); session.CreateSQLQuery( "select {simple.*} from Simple {simple}", "simple", typeof( Simple ) ).List(); session.CreateSQLQuery( "select {a.*} from A {a}", "a", typeof( A ) ).List(); session.Delete( s ); session.Delete( simple ); session.Delete( a ); //session.Delete( b ); session.Flush(); session.Close(); }
public void CachedCollectionRefresh() { ISession s = OpenSession(); Category c = new Category(); IList list = new ArrayList(); c.Subcategories = list; list.Add(new Category()); c.Name = "root"; object id = s.Save(c); s.Flush(); s.Close(); s = OpenSession(); c = (Category) s.Load(typeof(Category), id); NHibernateUtil.Initialize(c.Subcategories); ISession ss = OpenSession(); Category c2 = (Category) ss.Load(typeof(Category), id); ss.Delete(c2.Subcategories[0]); c2.Subcategories.Clear(); ss.Flush(); ss.Close(); s.Refresh(c); Assert.AreEqual(0, c.Subcategories.Count); ss = OpenSession(); c2 = (Category) ss.Load(typeof(Category), id); c2.Subcategories.Add(new Category()); c2.Subcategories.Add(new Category()); ss.Flush(); ss.Close(); s.Refresh(c); Assert.AreEqual(2, c.Subcategories.Count); s.Flush(); s.Close(); s = OpenSession(); c = (Category) s.Load(typeof(Category), id); Assert.AreEqual(2, c.Subcategories.Count); s.Delete(c); s.Flush(); s.Close(); }
public void CopyCascade() { ISession s = OpenSession(); Category child = new Category(); child.Name = "child"; s.Save(child); s.Flush(); s.Close(); s = OpenSession(); Category parent = new Category(); parent.Name = "parent"; parent.Subcategories.Add(child); child.Name = "child2"; // Save parent and cascade update detached child Category persistentParent = s.Merge(parent); Assert.IsTrue(persistentParent.Subcategories.Count == 1); Assert.AreEqual(((Category) persistentParent.Subcategories[0]).Name, "child2"); s.Flush(); s.Close(); s = OpenSession(); s.Delete(persistentParent); s.Flush(); s.Close(); }
public void PolymorphicCriteria() { ISession s = OpenSession(); ITransaction txn = s.BeginTransaction(); Category f = new Category(); Single b = new Single(); b.Id = "asdfa"; b.String = "asdfasdf"; s.Save(f); s.Save(b); IList list = s.CreateCriteria(typeof(object)).List(); Assert.AreEqual(2, list.Count); Assert.IsTrue(list.Contains(f)); Assert.IsTrue(list.Contains(b)); s.Delete(f); s.Delete(b); txn.Commit(); s.Close(); }
public void QueuedBagAdds() { ISession s = OpenSession(); Assignable a = new Assignable(); a.Id = "foo"; a.Categories = new ArrayList(); Category c = new Category(); c.Assignable = a; a.Categories.Add(c); s.Save(a); s.Flush(); s.Close(); sessions.EvictCollection("NHibernate.DomainModel.Assignable.Categories"); s = OpenSession(); a = (Assignable) s.Get(typeof(Assignable), "foo"); c = new Category(); c.Assignable = a; a.Categories.Add(c); Assert.IsFalse(NHibernateUtil.IsInitialized(a.Categories)); Assert.AreEqual(2, a.Categories.Count); s.Flush(); s.Close(); sessions.EvictCollection("NHibernate.DomainModel.Assignable.Categories"); s = OpenSession(); a = (Assignable) s.Get(typeof(Assignable), "foo"); c = new Category(); c.Assignable = a; a.Categories.Add(c); Assert.IsFalse(NHibernateUtil.IsInitialized(a.Categories)); s.Flush(); Assert.IsFalse(NHibernateUtil.IsInitialized(a.Categories)); Assert.AreEqual(3, a.Categories.Count); s.Close(); sessions.EvictCollection("NHibernate.DomainModel.Assignable.Categories"); s = OpenSession(); a = (Assignable) s.Get(typeof(Assignable), "foo"); Assert.AreEqual(3, a.Categories.Count); s.Delete(a); s.Flush(); s.Close(); }
public void CollectionRefresh() { ISession s = OpenSession(); Category c = new Category(); IList list = new ArrayList(); c.Subcategories = list; list.Add(new Category()); c.Name = "root"; object id = s.Save(c); s.Flush(); s.Close(); s = OpenSession(); c = (Category) s.Load(typeof(Category), id); s.Refresh(c); s.Flush(); Assert.AreEqual(1, c.Subcategories.Count); s.Flush(); s.Close(); s = OpenSession(); c = (Category) s.Load(typeof(Category), id); Assert.AreEqual(1, c.Subcategories.Count); // modify the collection in another session ISession s2 = OpenSession(); Category c2 = (Category) s2.Load(typeof(Category), id); c2.Subcategories.Add(new Category()); s2.Flush(); s2.Close(); // now lets refresh the collection and see if it picks up // the new objects s.Refresh(c); Assert.AreEqual(2, c.Subcategories.Count, "should have gotten the addition from s2"); s.Delete(c); s.Flush(); s.Close(); }
public void PolymorphicCriteria() { ISession s = OpenSession(); Category f = new Category(); DomainModel.Single b = new DomainModel.Single(); b.Id = "asdfa"; b.String = "asdfasdf"; s.Save( f ); s.Save( b ); IList list = s.CreateCriteria( typeof( object )).List(); Assert.AreEqual( 2, list.Count ); Assert.IsTrue( list.Contains( f ) ); Assert.IsTrue( list.Contains( b ) ); s.Delete( f ); s.Delete( b ); s.Flush(); s.Close(); }
public void Categories() { Category c = new Category(); c.Name = Category.RootCategory; Category c1 = new Category(); Category c2 = new Category(); Category c3 = new Category(); c.Subcategories.Add(c1); c.Subcategories.Add(c2); c2.Subcategories.Add(null); c2.Subcategories.Add(c3); using (ISession s = OpenSession()) { s.Save(c); s.Flush(); } using (ISession s = OpenSession()) { c = (Category) s.Load(typeof(Category), c.Id); Assert.IsNotNull(c.Subcategories[0]); Assert.IsNotNull(c.Subcategories[1]); IList list = ((Category) c.Subcategories[1]).Subcategories; Assert.IsNull(list[0]); Assert.IsNotNull(list[1]); IEnumerator enumer = s.CreateQuery("from c in class Category where c.Name = NHibernate.DomainModel.Category.RootCategory").Enumerable(). GetEnumerator(); Assert.IsTrue(enumer.MoveNext()); } using (ISession s = OpenSession()) { s.Delete("from Category"); s.Flush(); } }
public void FindBySQLParameters() { ISession s = OpenSession(); Category c = new Category(); c.Name = "Good"; Assignable assn = new Assignable(); assn.Id = "i.d."; IList l = new ArrayList(); l.Add(c); assn.Categories = l; c.Assignable = assn; s.Save(assn); s.Flush(); c = new Category(); c.Name = "Best"; assn = new Assignable(); assn.Id = "i.d.2"; l = new ArrayList(); l.Add(c); assn.Categories = l; c.Assignable = assn; s.Save(assn); s.Flush(); c = new Category(); c.Name = "Better"; assn = new Assignable(); assn.Id = "i.d.7"; l = new ArrayList(); l.Add(c); assn.Categories = l; c.Assignable = assn; s.Save(assn); s.Flush(); assn = new Assignable(); assn.Id = "i.d.3"; s.Save(assn); s.Flush(); s.Close(); s = OpenSession(); IQuery basicParam = s.CreateSQLQuery("select {category.*} from Category {category} where {category}.Name = 'Best'", "category", typeof( Category )); IList list = basicParam.List(); Assert.AreEqual(1, list.Count); IQuery unnamedParam = s.CreateSQLQuery("select {category.*} from Category {category} where {category}.Name = ? or {category}.Name = ?", "category", typeof( Category )); unnamedParam.SetString(0, "Good"); unnamedParam.SetString(1, "Best"); list = unnamedParam.List(); Assert.AreEqual(2, list.Count); IQuery namedParam = s.CreateSQLQuery("select {category.*} from Category {category} where ({category}.Name=:firstCat or {category}.Name=:secondCat)", "category", typeof( Category )); namedParam.SetString("firstCat", "Better"); namedParam.SetString("secondCat", "Best"); list = namedParam.List(); Assert.AreEqual(2, list.Count); s.Delete("from Assignable"); s.Delete("from Category"); s.Flush(); s.Close(); }
public void FindBySQLMultipleObject() { ISession s = OpenSession(); Category c = new Category(); c.Name = "NAME"; Assignable assn = new Assignable(); assn.Id = "i.d."; IList l = new ArrayList(); l.Add( c ); assn.Categories = l; c.Assignable = assn; s.Save( assn ); s.Flush(); c = new Category(); c.Name = "NAME2"; assn = new Assignable(); assn.Id = "i.d.2"; l = new ArrayList(); l.Add( c ); assn.Categories = l; c.Assignable = assn; s.Save( assn ); s.Flush(); assn = new Assignable(); assn.Id = "i.d.3"; s.Save( assn ); s.Flush(); s.Close(); s = OpenSession(); if ( !(dialect is Dialect.MySQLDialect) ) { IList list = s.CreateSQLQuery( "select {category.*}, {assignable.*} from Category {category}, \"assign able\" {assignable}", new string[] { "category", "assignable" }, new System.Type[] { typeof( Category ), typeof( Assignable ) } ).List(); Assert.AreEqual( 6, list.Count, "Count differs" ); // cross-product of 2 categories x 3 assignables; Assert.IsTrue( list[0] is object[] ); } s.Delete( "from Assignable" ); s.Delete( "from Category" ); s.Flush(); s.Close(); }
public void FindBySQLAssociatedObject() { ISession s = OpenSession(); Category c = new Category(); c.Name = "NAME"; Assignable assn = new Assignable(); assn.Id = "i.d."; IList l = new ArrayList(); l.Add( c ); assn.Categories = l; c.Assignable = assn; s.Save( assn ); s.Flush(); s.Close(); s = OpenSession(); IList list = s.CreateSQLQuery( "select {category.*} from Category {category}", "category", typeof( Category ) ).List(); Assert.AreEqual( 1, list.Count, "Count differs" ); s.Delete( "from Assignable" ); s.Delete( "from Category" ); s.Flush(); s.Close(); }
public void FindBySQLProperties() { ISession session = OpenSession(); Category s = new Category(); s.Name = nextLong.ToString(); nextLong++; session.Save( s ); s = new Category(); s.Name = "WannaBeFound"; session.Flush(); IQuery query = session.CreateSQLQuery( "select {category.*} from Category {category} where {category}.Name = :Name", "category", typeof( Category ) ); query.SetProperties( s ); query.List(); session.Delete( "from Category" ); session.Flush(); session.Close(); }
public void FindBySQLMultipleObject() { ISession s = OpenSession(); Category c = new Category(); c.Name = "NAME"; Assignable assn = new Assignable(); assn.Id = "i.d."; assn.Categories = new List<Category> {c}; c.Assignable = assn; s.Save(assn); s.Flush(); c = new Category(); c.Name = "NAME2"; assn = new Assignable(); assn.Id = "i.d.2"; assn.Categories = new List<Category> { c }; c.Assignable = assn; s.Save(assn); s.Flush(); assn = new Assignable(); assn.Id = "i.d.3"; s.Save(assn); s.Flush(); s.Close(); s = OpenSession(); if (!(Dialect is MySQLDialect)) { IList list = s.CreateSQLQuery("select {category.*}, {assignable.*} from Category {category}, \"assign able\" {assignable}") .AddEntity("category", typeof(Category)) .AddEntity("assignable", typeof(Assignable)) .List(); Assert.AreEqual(6, list.Count, "Count differs"); // cross-product of 2 categories x 3 assignables; Assert.IsTrue(list[0] is object[]); } s.Delete("from Assignable"); s.Delete("from Category"); s.Flush(); s.Close(); }
public void CollectionReplace2() { ISession s = OpenSession(); ITransaction t = s.BeginTransaction(); Category c = new Category(); IList<Category> list = new List<Category>(); c.Subcategories = list; list.Add(new Category()); Category c2 = new Category(); s.Save(c2); s.Save(c); t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); c = (Category) s.Load(typeof(Category), c.Id, LockMode.Upgrade); IList<Category> list2 = c.Subcategories; t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); c2 = (Category) s.Load(typeof(Category), c2.Id, LockMode.Upgrade); c2.Subcategories = list2; t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); c2 = (Category) s.Load(typeof(Category), c2.Id, LockMode.Upgrade); Assert.AreEqual(1, c2.Subcategories.Count); s.Delete(c2); s.Delete(s.Load(typeof(Category), c.Id)); t.Commit(); s.Close(); }
public void Copy() { Category catWA = new Category(); catWA.Name = "HSQL workaround"; Category cat = new Category(); cat.Name = "foo"; Category subCatBar = new Category(); subCatBar.Name = "bar"; Category subCatBaz = new Category(); subCatBaz.Name = "baz"; cat.Subcategories.Add( subCatBar ); cat.Subcategories.Add( subCatBaz ); using( ISession s = OpenSession() ) { s.Save( catWA ); s.Save( cat ); s.Flush(); } cat.Name = "new foo"; subCatBar.Name = "new bar"; cat.Subcategories.Remove( subCatBaz ); Category newCat = new Category(); newCat.Name = "new"; cat.Subcategories.Add( newCat ); Category newSubCat = new Category(); newSubCat.Name = "new sub"; newCat.Subcategories.Add( newSubCat ); using( ISession s = OpenSession() ) { s.SaveOrUpdateCopy( cat ); s.Flush(); } using( ISession s = OpenSession() ) { cat = (Category) s.CreateQuery( "from Category cat where cat.Name='new foo'").UniqueResult(); newSubCat = (Category) s.CreateQuery( "from Category cat where cat.Name='new sub'").UniqueResult(); newSubCat.Subcategories.Add( cat ); subCatBaz = (Category) s.SaveOrUpdateCopy( newSubCat, subCatBaz.Id ); Assert.IsTrue( subCatBaz.Name.Equals( "new sub" ) ); Assert.IsTrue( subCatBaz.Subcategories.Count == 1 && subCatBaz.Subcategories[0] == cat ); newSubCat.Subcategories.Remove( cat ); s.Delete( cat ); s.Delete( subCatBaz ); s.Delete( catWA ); s.Flush(); } }
public void MixNativeAssigned() { // if HSQLDialect then skip test ISession s = OpenSession(); Category c = new Category(); c.Name = "NAME"; Assignable assn = new Assignable(); assn.Id = "i.d."; IList l = new ArrayList(); l.Add(c); assn.Categories = l; c.Assignable = assn; s.Save(assn); s.Flush(); s.Close(); s = OpenSession(); s.Delete(assn); s.Flush(); s.Close(); }
public void CollectionReplace() { ISession s = OpenSession(); ITransaction t = s.BeginTransaction(); Category c = new Category(); IList list = new ArrayList(); c.Subcategories = list; list.Add(new Category()); s.Save(c); t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); c = (Category) s.Load(typeof(Category), c.Id, LockMode.Upgrade); c.Subcategories = list; t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); c = (Category) s.Load(typeof(Category), c.Id, LockMode.Upgrade); IList list2 = c.Subcategories; t.Commit(); s.Close(); Assert.IsFalse(NHibernateUtil.IsInitialized(c.Subcategories)); s = OpenSession(); t = s.BeginTransaction(); c = (Category) s.Load(typeof(Category), c.Id, LockMode.Upgrade); c.Subcategories = list2; t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); c = (Category) s.Load(typeof(Category), c.Id, LockMode.Upgrade); Assert.AreEqual(1, c.Subcategories.Count); s.Delete(c); t.Commit(); s.Close(); }
public void FindBySQLSimpleByDiffSessions() { if ( dialect is Dialect.MySQLDialect ) return; ISession session = OpenSession(); Category s = new Category(); nextLong++; s.Name = nextLong.ToString(); session.Save(s); session.Flush(); session.Close(); session = OpenSession(); IQuery query = session.CreateSQLQuery("select s.category_key_col as {category.id}, s.Name as {category.Name}, s.\"assign able id\" as {category.Assignable} from {category} s", "category", typeof( Category )); IList list = query.List(); Assert.IsNotNull(list); Assert.IsTrue(list.Count > 0); Assert.IsTrue(list[0] is Category); // How do we handle objects that does not have id property (such as Simple ?) // How do we handle objects with composite id's ? (such as Single) session.Delete( list[0] ); session.Flush(); session.Close(); }
public void Copy() { Category catWA = new Category(); catWA.Name = "HSQL workaround"; Category cat = new Category(); cat.Name = "foo"; Category subCatBar = new Category(); subCatBar.Name = "bar"; Category subCatBaz = new Category(); subCatBaz.Name = "baz"; cat.Subcategories.Add(subCatBar); cat.Subcategories.Add(subCatBaz); using (ISession s = OpenSession()) { s.Save(catWA); s.Save(cat); s.Flush(); } cat.Name = "new foo"; subCatBar.Name = "new bar"; cat.Subcategories.Remove(subCatBaz); Category newCat = new Category(); newCat.Name = "new"; cat.Subcategories.Add(newCat); Category newSubCat = new Category(); newSubCat.Name = "new sub"; newCat.Subcategories.Add(newSubCat); Category copiedCat; using (ISession s = OpenSession()) { copiedCat = s.Merge(cat); s.Flush(); } Assert.IsFalse(copiedCat == cat); Assert.IsTrue(cat.Subcategories.Contains(newCat)); using (ISession s = OpenSession()) { cat = (Category) s.CreateQuery("from Category cat where cat.Name='new foo'").UniqueResult(); newSubCat = (Category)s.CreateQuery("from Category cat left join fetch cat.Subcategories where cat.Name='new sub'").UniqueResult(); Assert.AreEqual("new sub", newSubCat.Name); } newSubCat.Subcategories.Add(cat); cat.Name="new new foo"; using (ISession s = OpenSession()) { newSubCat = s.Merge(newSubCat); Assert.IsTrue(newSubCat.Name.Equals("new sub")); Assert.AreEqual(1, newSubCat.Subcategories.Count); cat = (Category)newSubCat.Subcategories[0]; Assert.AreEqual("new new foo", cat.Name); newSubCat.Subcategories.Remove(cat); s.Delete(cat); s.Delete(subCatBaz); s.Delete(catWA); s.Flush(); } }
public void NamedSQLQuery() { if( dialect is Dialect.MySQLDialect ) { return; } ISession s = OpenSession(); Category c = new Category(); c.Name = "NAME"; Assignable assn = new Assignable(); assn.Id = "i.d."; IList l = new ArrayList(); l.Add( c ); assn.Categories = l; c.Assignable = assn; s.Save( assn ); s.Flush(); s.Close(); s = OpenSession(); IQuery q = s.GetNamedQuery( "namedsql" ); Assert.IsNotNull( q, "should have found 'namedsql'" ); IList list = q.List(); Assert.IsNotNull( list, "executing query returns list" ); object[] values = list[0] as object[]; Assert.IsNotNull( values[0], "index 0 should not be null" ); Assert.IsNotNull( values[1], "index 1 should not be null" ); Assert.AreEqual( typeof(Category), values[0].GetType(), "should be a Category" ); Assert.AreEqual( typeof(Assignable), values[1].GetType(), "should be Assignable" ); s.Delete( "from Category" ); s.Delete( "from Assignable" ); s.Flush(); s.Close(); }