コード例 #1
0
		public void MakeCollectionTransient()
		{
			ISession fixture = OpenSession();

			for (long i = 1L; i < 6L; i++)
			{
				Simple s = new Simple((int) i);
				s.Address = "dummy collection address " + i;
				s.Date = DateTime.Now;
				s.Name = "dummy collection name " + i;
				s.Pay = i * 1279L;
				fixture.Save(s, i);
			}

			fixture.Flush();

			IList list = fixture.CreateCriteria(typeof(Simple)).List();

			Assert.IsNotNull(list);
			Assert.IsTrue(list.Count == 5);
			Assert.IsTrue(fixture.Contains(list[2]));

			fixture.Clear();

			Assert.IsTrue(list.Count == 5);
			Assert.IsFalse(fixture.Contains(list[2]));

			fixture.Flush();

			Assert.IsTrue(list.Count == 5);

			fixture.Delete("from System.Object o");
			fixture.Flush();
			fixture.Close();
		}
コード例 #2
0
		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();
		}
コード例 #3
0
		private void Prepare(ISession s, Simple[] simples, object[] ids, int N)  
		{
			for ( int i=0; i<N; i++ ) 
			{
				s.Save( simples[i], ids[i] );
			}
			s.Flush();
		}
コード例 #4
0
		public void SimpleSelectTest() 
		{			
			// create the objects to search on
			long simple1Key = 15;
			Simple simple1 = new Simple();
			simple1.Address = "Street 12";
			simple1.Date = DateTime.Now;
			simple1.Name = "For Criteria Test";
			simple1.Count = 16;

			long notSimple1Key = 17;
			Simple notSimple1 = new Simple();
			notSimple1.Address = "Street 123";
			notSimple1.Date = DateTime.Now;
			notSimple1.Name = "Don't be found";
			notSimple1.Count = 18;

			using( ISession s1 = OpenSession() )
			using( ITransaction t1 = s1.BeginTransaction() )
			{
				s1.Save(notSimple1, notSimple1Key);
				s1.Save(simple1, simple1Key);
				t1.Commit();
			}

			using( ISession s2 = OpenSession() )
			using( ITransaction t2 = s2.BeginTransaction() )
			{
				IList results2 = s2.CreateCriteria(typeof(Simple))
					.Add(Expression.Expression.Eq("Address","Street 12"))
					.List();
			
				Assert.AreEqual(1, results2.Count);

				Simple simple2 = (Simple)results2[0];

				Assert.IsNotNull(simple2, "Unable to load object");
				Assert.AreEqual(simple1.Count, simple2.Count, "Load failed");
				Assert.AreEqual(simple1.Name, simple2.Name, "Load failed");
				Assert.AreEqual(simple1.Address, simple2.Address, "Load failed");
				Assert.AreEqual(simple1.Date.ToString(), simple2.Date.ToString(), "Load failed");

				s2.Delete("from Simple");

				t2.Commit();
			}
		}
コード例 #5
0
		public void Performance() 
		{
			for ( int n=2; n<4000; n*=2 ) 
			{
			
				Simple[] simples = new Simple[n];
				object[] ids = new object[n];
				for ( int i=0; i<n; i++ ) 
				{
					simples[i] = new Simple();
					simples[i].Init();
					simples[i].Count = i;
					ids[i] = (long) i;
				}
			
				ISession s = OpenSession();
				Prepare(s, simples, ids, n);
				s.Close();
			
				long find = 0;
				long flush = 0;
			
				for ( int i=0; i<100; i++ ) 
				{
					s = OpenSession();
					long time = DateTime.Now.Ticks;
					IList list = s.Find("from s in class Simple where not s.Name='osama bin laden' and s.Other is null");
					find += DateTime.Now.Ticks - time;
					Assert.IsTrue( list.Count == n );
					time = DateTime.Now.Ticks;
					s.Flush();
					flush += DateTime.Now.Ticks - time;
					s.Close();
				}
			
				Console.WriteLine( "Objects: " + n + " - find(): " + find + "ms / flush(): " + flush + "ms / Ratio: " + ( (float) flush )/find );
				Console.WriteLine( "Objects: " + n + " flush time per object: " + flush / 100.0 / n );
				Console.WriteLine( "Objects: " + n + " load time per object: " + find / 100.0 / n );
				s = OpenSession();
				Delete(s);
				s.Close();
			}
		}
コード例 #6
0
		public void TSNamed()
		{
			if ( dialect is NHibernate.Dialect.Oracle9Dialect )
			{
				return;
			}

			ISession session = OpenSession();

			Simple sim = new Simple();
			sim.Date = DateTime.Today;	// NB We don't use Now() due to the millisecond alignment problem with SQL Server
			session.Save( sim, 1L );
			IQuery q = session.CreateSQLQuery( "select {sim.*} from Simple {sim} where {sim}.date_ = :fred", "sim", typeof( Simple ) );
			q.SetTimestamp( "fred", sim.Date );
			Assert.AreEqual( 1, q.List().Count, "q.List.Count");
			session.Delete( sim );
			session.Flush();
			session.Close();
		}
コード例 #7
0
		public void SimpleDateCriteria() 
		{
			Simple s1 = new Simple();
			s1.Address = "blah";
			s1.Count = 1;
			s1.Date = new DateTime( 2004, 01, 01 );
			
			Simple s2 = new Simple();
			s2.Address = "blah";
			s2.Count = 2;
			s2.Date = new DateTime( 2006, 01, 01 );

			using( ISession s = OpenSession() )
			{
				s.Save( s1, 1L );
				s.Save( s2, 2L );
				s.Flush();
			}

			using( ISession s = OpenSession() )
			{
				IList results = s.CreateCriteria( typeof(Simple) )
					.Add( Expression.Expression.Gt( "Date", new DateTime( 2005, 01, 01 ) ) )
					.AddOrder( Expression.Order.Asc( "Date" ) )
					.List();

				Assert.AreEqual( 1, results.Count, "one gt from 2005" );
				Simple simple = (Simple)results[0];
				Assert.IsTrue( simple.Date > new DateTime( 2005, 01, 01), "should have returned dates after 2005" );
		
				results = s.CreateCriteria( typeof(Simple) )
					.Add( Expression.Expression.Lt( "Date", new DateTime( 2005, 01, 01 ) ) )
					.AddOrder( Expression.Order.Asc( "Date" ) )
					.List();
			
				Assert.AreEqual( 1, results.Count, "one lt than 2005" );
				simple = (Simple)results[0];
				Assert.IsTrue( simple.Date < new DateTime( 2005, 01, 01 ), "should be less than 2005" );

				s.Delete( "from Simple" );
				s.Flush();
			}
		}
コード例 #8
0
ファイル: SQLLoaderTest.cs プロジェクト: paulbatum/nhibernate
		public void TS()
		{
			if (Dialect is Oracle8iDialect)
			{
				return;
			}

			ISession session = OpenSession();
			ITransaction txn = session.BeginTransaction();

			Simple sim = new Simple();
			sim.Date = DateTime.Today; // NB We don't use Now() due to the millisecond alignment problem with SQL Server
			session.Save(sim, 1L);
			IQuery q = session.CreateSQLQuery("select {sim.*} from Simple {sim} where {sim}.date_ = ?")
				.AddEntity("sim", typeof(Simple));
			q.SetTimestamp(0, sim.Date);
			Assert.AreEqual(1, q.List().Count, "q.List.Count");
			session.Delete(sim);
			txn.Commit();
			session.Close();
		}
コード例 #9
0
		public void QueryCacheWithNullParameters()
		{
			Simple simple = new Simple();

			using (ISession s = OpenSession())
			{
				s.Save(simple, 1L);
				s.Flush();
			}

			using (ISession s = OpenSession())
			{
				s
					.CreateQuery("from Simple s where s = :s or s.Name = :name or s.Address = :address")
					.SetEntity("s", s.Load(typeof(Simple), 1L))
					.SetString("name", null)
					.SetString("address", null)
					.SetCacheable(true)
					.UniqueResult();

				// Run a second time, just to test the query cache
				object result = s
					.CreateQuery("from Simple s where s = :s or s.Name = :name or s.Address = :address")
					.SetEntity("s", s.Load(typeof(Simple), 1L))
					.SetString("name", null)
					.SetString("address", null)
					.SetCacheable(true)
					.UniqueResult();

				Assert.IsNotNull(result);
				Assert.AreEqual(1L, (long) s.GetIdentifier(result));
			}

			using (ISession s = OpenSession())
			{
				s.Delete("from Simple");
				s.Flush();
			}
		}
コード例 #10
0
		public void AdoNetOnly()
		{
			IConnectionProvider cp = ConnectionProviderFactory.NewConnectionProvider( Cfg.Environment.Properties );
		
			ISession s;
			for ( int n=2; n<4000; n*=2 ) 
			{
				s = OpenSession();
				s.Delete("from Simple");
				s.Flush();
				Simple[] simples = new Simple[n];
				object[] ids = new object[n];
				for	( int i=0; i<n; i++ ) 
				{
					simples[i] = new Simple();
					simples[i].Init();
					simples[i].Count = i;
					ids[i] = (long) i;
					s.Save(simples[i], ids[i]);
				}
				s.Flush();
				s.Close();
		
		
				//Now do timings
		
				IDbConnection c = cp.GetConnection();
				long time = DateTime.Now.Ticks;
				DirectAdoNet( c, simples, ids, n, "j1" );
				long adonet = DateTime.Now.Ticks - time;
				cp.CloseConnection(c);
		
				c = cp.GetConnection();
				time = DateTime.Now.Ticks;	
				DirectAdoNet( c, simples, ids, n, "j2" );
				adonet += DateTime.Now.Ticks - time;
				cp.CloseConnection(c);
					
				c = cp.GetConnection();
				time = DateTime.Now.Ticks;
				DirectAdoNet( c, simples, ids, n, "j2" );
				adonet += DateTime.Now.Ticks - time;
				cp.CloseConnection(c);
				System.Console.Out.WriteLine( "Objects: " + n + "\t Direct AdoNet: " + adonet );
		
			}
			cp.Dispose();
			s = OpenSession();
			s.Delete("from Simple");
			s.Flush();
			s.Close();
			System.GC.Collect();
		}
コード例 #11
0
		public void Many() 
		{
			IConnectionProvider cp = ConnectionProviderFactory.NewConnectionProvider( Cfg.Environment.Properties );
		
			long hiber=0;
			long adonet=0;
			ISession s;
			for ( int n=0; n<20; n++ ) 
			{
			
				s = OpenSession();
				s.Delete("from Simple");
				s.Flush();
				Simple[] simples = new Simple[n];
				object[] ids = new object[n];
				for ( int i=0; i<n; i++ ) 
				{
					simples[i] = new Simple();
					simples[i].Init();
					simples[i].Count = i;
					ids[i] = (long) i;
					s.Save(simples[i], ids[i]);
				}
				s.Flush();
				s.Close();
			
				//allow cache to settle
			
				s = OpenSession();
				NHibernate(s, simples, ids, n, "h0");
				s.Close();
			
				IDbConnection c = cp.GetConnection();
				DirectAdoNet( c, simples, ids, n, "j0" );
				cp.CloseConnection(c);
			
				s = OpenSession();
				NHibernate(s, simples, ids, n, "h0");
				s.Close();
			
				c = cp.GetConnection();
				DirectAdoNet( c, simples, ids, n, "j0" );
				cp.CloseConnection(c);
			
				//Now do timings
			
				int N=30;
			
				long time = DateTime.Now.Ticks;
				for (int i=0; i<N; i++) 
				{
					s = OpenSession();
					NHibernate(s, simples, ids, n, "h1");
					s.Close();
				}
				hiber += DateTime.Now.Ticks - time;
			
				time = DateTime.Now.Ticks;
				for (int i=0; i<N; i++) 
				{
					c = cp.GetConnection();
					DirectAdoNet( c, simples, ids, n, "j1" );
					cp.CloseConnection(c);
				}
				adonet += DateTime.Now.Ticks - time;
			
				time = DateTime.Now.Ticks;
				for (int i=0; i<N; i++) 
				{
					s = OpenSession();
					NHibernate(s, simples, ids, n, "h2");
					s.Close();
				}
				hiber += DateTime.Now.Ticks - time;
			
				time = DateTime.Now.Ticks;
				for (int i=0; i<N; i++) 
				{
					c = cp.GetConnection();
					DirectAdoNet( c, simples, ids, n, "j2" );
					cp.CloseConnection(c);
				}
				adonet += DateTime.Now.Ticks - time;
			
				time = DateTime.Now.Ticks;
				for (int i=0; i<N; i++) 
				{
					s = OpenSession();
					NHibernate(s, simples, ids, n, "h1");
					s.Close();
				}
				hiber += DateTime.Now.Ticks - time;
			
				time = DateTime.Now.Ticks;
				for (int i=0; i<N; i++) 
				{
					c = cp.GetConnection();
					DirectAdoNet( c, simples, ids, n, "j1" );
					cp.CloseConnection(c);
				}
				adonet += DateTime.Now.Ticks - time;			
			
			}

			System.Console.Out.Write( "Hibernate: " + hiber + "ms / Direct JDBC: " + adonet + "ms = Ratio: " + ( (float) hiber )/adonet );
			s = OpenSession();
			s.Delete("from Simple");
			s.Flush();
			s.Close();
			cp.Dispose();
			System.GC.Collect();

		}
コード例 #12
0
		public void CachedQueryRegion()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Simple simple = new Simple();
			simple.Name = "Simple 1";
			s.Save(simple, 10L);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			IQuery q = s.CreateQuery("from Simple s where s.Name=?");
			q.SetCacheRegion("foo");
			q.SetCacheable(true);
			q.SetString(0, "Simple 1");
			Assert.AreEqual(1, q.List().Count);
			Assert.AreEqual(1, q.List().Count);
			Assert.AreEqual(1, q.List().Count);

			q = s.CreateQuery("from Simple s where s.Name=:name");
			q.SetCacheRegion("foo");
			q.SetCacheable(true);
			q.SetString("name", "Simple 1");
			Assert.AreEqual(1, q.List().Count);
			simple = (Simple) q.List()[0];

			q.SetString("name", "Simple 2");
			Assert.AreEqual(0, q.List().Count);
			Assert.AreEqual(0, q.List().Count);
			simple.Name = "Simple 2";
			Assert.AreEqual(1, q.List().Count);
			Assert.AreEqual(1, q.List().Count);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			s.Update(simple, 10L);
			s.Delete(simple);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			q = s.CreateQuery("from Simple s where s.Name=?");
			q.SetCacheRegion("foo");
			q.SetCacheable(true);
			q.SetString(0, "Simple 1");
			Assert.AreEqual(0, q.List().Count);
			Assert.AreEqual(0, q.List().Count);
			t.Commit();
			s.Close();
		}
コード例 #13
0
ファイル: FooBarTest.cs プロジェクト: remcoros/nhibernate
		public void EnumerableDisposable()
		{
			// this test used to be called Iterators()

			ISession s = OpenSession();
			for (long i = 0L; i < 10L; i++)
			{
				Simple simple = new Simple();
				simple.Count = (int) i;
				s.Save(simple, i);
				Assert.IsNotNull(simple, "simple is not null");
			}
			s.Flush();
			s.Close();

			s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Simple simp = (Simple) s.Load(typeof(Simple), 8L);

			// the reader under the enum has to still be a SqlDataReader (subst db name here) and 
			// can't be a NDataReader - the best way to get this result is to query on just a property
			// of an object.  If the query is "from Simple as s" then it will be converted to a NDataReader
			// on the MoveNext so it can get the object from the id - thus needing another open DataReader so
			// it must convert to an NDataReader.
			IEnumerable enumer = s.CreateQuery("select s.Count from Simple as s").Enumerable();
			//int count = 0;
			foreach (object obj in enumer)
			{
				if ((int) obj == 7)
				{
					break;
				}
			}

			// if Enumerable doesn't implement Dispose() then the test fails on this line
			t.Commit();
			s.Close();

			s = OpenSession();
			Assert.AreEqual(10,
			                s.Delete("from Simple"),
			                "delete by query");

			s.Flush();
			s.Close();

			s = OpenSession();
			enumer = s.CreateQuery("from Simple").Enumerable();
			Assert.IsFalse(enumer.GetEnumerator().MoveNext(), "no items in enumerator");
			s.Flush();
			s.Close();
		}
コード例 #14
0
		public void SQLFunctionAsAlias()
		{
			string functionName = LocateAppropriateDialectFunctionNameForAliasTest();
			if (functionName == null)
			{
				log.Info("Dialect does not list any no-arg functions");
				return;
			}

			log.Info("Using function named [" + functionName + "] for 'function as alias' test");
			string query = "select " + functionName + " from Simple as " + functionName + " where " + functionName + ".id = 10";

			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Simple simple = new Simple();
			simple.Name = "Simple 1";
			s.Save(simple, 10L);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			IList result = s.CreateQuery(query).List();
			Assert.IsTrue(result[0] is Simple,
			              "Unexpected result type [" + result[0].GetType().Name + "]");
			s.Delete(result[0]);
			t.Commit();
			s.Close();
		}
コード例 #15
0
		public void NothingToUpdate()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Simple simple = new Simple();
			simple.Name = "Simple 1";
			s.Save(simple, (long) 10);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			s.Update(simple, (long) 10);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			s.Update(simple, (long) 10);
			s.Delete(simple);
			t.Commit();
			s.Close();
		}
コード例 #16
0
		private void NHibernate(ISession s, Simple[] simples, object[] ids, int N, string runname)
		{
			ITransaction trans = s.BeginTransaction();
			s.Find("from Simple s");
			trans.Commit();
		}
コード例 #17
0
		public void LoadAfterNotExists()
		{
			ISession fixture = OpenSession();

			// First, prime the fixture session to think the entity does not exist
			try
			{
				fixture.Load(typeof(Simple), -1L);
			}
			catch (ObjectNotFoundException)
			{
				// this is expected
			}

			// Next, lets create that entity under the covers
			ISession anotherSession = null;
			try
			{
				anotherSession = OpenSession();

				Simple oneSimple = new Simple(1);
				oneSimple.Name = "hidden entity";
				oneSimple.Address = "SessionCacheTest.LoadAfterNotExists";
				oneSimple.Date = DateTime.Now;
				oneSimple.Pay = 1000000f;

				anotherSession.Save(oneSimple, -1L);
				anotherSession.Flush();
			}
			finally
			{
				QuietlyClose(anotherSession);
			}

			// Verify that the original session is still unable to see the new entry...
			try
			{
				fixture.Load(typeof(Simple), -1L);
			}
			catch (ObjectNotFoundException)
			{
			}

			// Now, lets clear the original session at which point it should be able to see the new entity
			fixture.Clear();

			string failedMessage = "Unable to load entity with id = -1.";
			try
			{
				Simple dummy = fixture.Load(typeof(Simple), -1L) as Simple;
				Assert.IsNotNull(dummy, failedMessage);
				fixture.Delete(dummy);
				fixture.Flush();
			}
			catch (ObjectNotFoundException)
			{
				Assert.Fail(failedMessage);
			}
			finally
			{
				QuietlyClose(fixture);
			}
		}
コード例 #18
0
		private void DirectAdoNet(IDbConnection c, Simple[] simples, object[] ids, int N, string runname)
		{
			IList result = new ArrayList();
			IDbCommand select = c.CreateCommand();
			IDbTransaction trans = c.BeginTransaction();
			select.CommandText = "SELECT id_, name, address, count_, date_, pay, other FROM Simple";
			select.Transaction = trans;
			IDataReader reader = select.ExecuteReader();
			while ( reader.Read() ) 
			{
				/*new Long( rs.getLong(1) );
				rs.getString(2);
				rs.getString(3);
				rs.getInt(4);
				rs.getDate(5);
				rs.getFloat(6);
				rs.getLong(7);*/
				Simple s = new Simple();
				if ( ! reader.IsDBNull(0) )
				{
					reader.GetInt64(0);
				}
				if ( ! reader.IsDBNull(1) )
				{
					s.Name = reader.GetString(1); 
				}
				if ( ! reader.IsDBNull(2) )
				{
					s.Address = reader.GetString(2); 
				}
				if ( ! reader.IsDBNull(3) )
				{
					s.Count = reader.GetInt32(3); 
				}
				if ( ! reader.IsDBNull(4) )
				{
					s.Date = reader.GetDateTime(4); 
				}
				if ( ! reader.IsDBNull(5) )
				{
					s.Pay =  reader.GetFloat(5); 
				}
				if ( ! reader.IsDBNull(6) )
				{
					reader.GetInt64(6); 
				} 
				s.Other = null;
				result.Add(s);
			}
			reader.Close();
			select.Dispose();
			trans.Commit();
			trans.Dispose();
		}
コード例 #19
0
		public void SetProperties()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Simple simple = new Simple();
			simple.Name = "Simple 1";
			s.Save(simple, (long) 10);
			IQuery q = s.CreateQuery("from s in class Simple where s.Name=:Name and s.Count=:Count");
			q.SetProperties(simple);
			Assert.AreEqual(simple, q.List()[0]);
			s.Delete(simple);
			t.Commit();
			s.Close();
		}
コード例 #20
0
		public void CollectionQuery()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();

			Simple s1 = new Simple();
			s1.Name = "s";
			s1.Count = 0;
			Simple s2 = new Simple();
			s2.Count = 2;
			Simple s3 = new Simple();
			s3.Count = 3;
			s.Save(s1, (long) 1);
			s.Save(s2, (long) 2);
			s.Save(s3, (long) 3);
			Container c = new Container();
			IList l = new ArrayList();
			l.Add(s1);
			l.Add(s3);
			l.Add(s2);
			c.OneToMany = l;
			l = new ArrayList();
			l.Add(s1);
			l.Add(null);
			l.Add(s2);
			c.ManyToMany = l;
			s.Save(c);

			Assert.AreEqual(1,
			                s.CreateQuery("select c from c in class ContainerX, s in class Simple where c.OneToMany[2] = s").List
			                	().Count);
			Assert.AreEqual(1,
			                s.CreateQuery("select c from c in class ContainerX, s in class Simple where c.ManyToMany[2] = s").
			                	List().Count);
			Assert.AreEqual(1,
			                s.CreateQuery("select c from c in class ContainerX, s in class Simple where s = c.OneToMany[2]").List
			                	().Count);
			Assert.AreEqual(1,
			                s.CreateQuery("select c from c in class ContainerX, s in class Simple where s = c.ManyToMany[2]").
			                	List().Count);
			Assert.AreEqual(1, s.CreateQuery("select c from c in class ContainerX where c.OneToMany[0].Name = 's'").List().Count);
			Assert.AreEqual(1, s.CreateQuery("select c from c in class ContainerX where c.ManyToMany[0].Name = 's'").List().Count);
			Assert.AreEqual(1,
			                s.CreateQuery("select c from c in class ContainerX where 's' = c.OneToMany[2 - 2].Name").List().Count);
			Assert.AreEqual(1,
			                s.CreateQuery("select c from c in class ContainerX where 's' = c.ManyToMany[(3+1)/4-1].Name").List().
			                	Count);
			if (Dialect.SupportsSubSelects)
			{
				Assert.AreEqual(1,
				                s.CreateQuery(
				                	"select c from c in class ContainerX where c.ManyToMany[ c.ManyToMany.maxIndex ].Count = 2").List()
				                	.Count);
				Assert.AreEqual(1,
				                s.CreateQuery(
				                	"select c from c in class ContainerX where c.ManyToMany[ maxindex(c.ManyToMany) ].Count = 2").List()
				                	.Count);
			}
			Assert.AreEqual(1,
			                s.CreateQuery(
			                	"select c from c in class ContainerX where c.OneToMany[ c.ManyToMany[0].Count ].Name = 's'").List().
			                	Count);
			Assert.AreEqual(1,
			                s.CreateQuery(
			                	"select c from c in class ContainerX where c.ManyToMany[ c.OneToMany[0].Count ].Name = 's'").List().
			                	Count);

			s.Delete(c);
			s.Delete(s1);
			s.Delete(s2);
			s.Delete(s3);

			t.Commit();
			s.Close();
		}
コード例 #21
0
		public void DialectSQLFunctions()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();

			IEnumerator iter = s.CreateQuery("select max(s.Count) from s in class Simple").Enumerable()
				.GetEnumerator();

			if (Dialect is MySQLDialect
			    // Added two dialects below for NH
				|| Dialect is MsSql2000Dialect
				|| Dialect is PostgreSQLDialect)
			{
				Assert.IsTrue(iter.MoveNext());
				Assert.IsNull(iter.Current);
			}

			Simple simple = new Simple();
			simple.Name = "Simple Dialect Function Test";
			simple.Address = "Simple Address";
			simple.Pay = 45.8f;
			simple.Count = 2;
			s.Save(simple, 10L);

			// Test to make sure allocating an specified object operates correctly.
			Assert.AreEqual(1,
			                s.CreateQuery("select new S(s.Count, s.Address) from s in class Simple").List()
			                	.Count);

			// Quick check the base dialect functions operate correctly
			Assert.AreEqual(1,
			                s.CreateQuery("select max(s.Count) from s in class Simple").List().Count);
			Assert.AreEqual(1,
			                s.CreateQuery("select count(*) from s in class Simple").List().Count);

			if (Dialect is Oracle8iDialect)
			{
				// Check Oracle Dialect mix of dialect functions - no args (no parenthesis and single arg functions
				IList rset = s.CreateQuery("select s.Name, sysdate, trunc(s.Pay), round(s.Pay) from s in class Simple").List();
				object[] row = (object[]) rset[0];
				Assert.IsNotNull(row[0], "Name string should have been returned");
				Assert.IsNotNull(row[1], "Todays Date should have been returned");
				Assert.AreEqual(45f, row[2], "trunc(45.8) result was incorrect");
				Assert.AreEqual(46f, row[3], "round(45.8) result was incorrect");

				simple.Pay = -45.8f;
				s.Update(simple);

				// Test type conversions while using nested functions (Float to Int).
				rset = s.CreateQuery("select abs(round(s.Pay)) from s in class Simple").List();
				Assert.AreEqual(46f, rset[0], "abs(round(-45.8)) result was incorrect");

				rset = s.CreateQuery("select left('abc', 2), right('abc', 2) from s in class Simple").List();
				row = (object[]) rset[0];
				Assert.AreEqual("ab", row[0], "Left function is broken.");
				Assert.AreEqual("bc", row[1], "Right function is broken.");

				// Test a larger depth 3 function example - Not a useful combo other than for testing
				Assert.AreEqual(1,
												s.CreateQuery("select trunc(round(length('A'))) from s in class Simple").List().Count);
				// NOTE: In Oracle this will fail as the translator will expect two columns in return
				//Assert.AreEqual(1,
				//                s.CreateQuery("select trunc(round(sysdate)) from s in class Simple").List().Count);

				// Test the oracle standard NVL funtion as a test of multi-param functions...
				// NOTE: commented out for NH, since Pay is a value type and will never be null
				//simple.Pay = null;
				//s.Update( simple );
				//Assert.AreEqual( 0,
				//	s.CreateQuery("select MOD( NVL(s.Pay, 5000), 2 ) from Simple as s where s.id = 10").List()[0] );
			}

			// NOTE: Commented out for NHibernate, no HSQL dialect.
			//if ( (getDialect() is HSQLDialect) ) 
			//{
			//	// Test the hsql standard MOD funtion as a test of multi-param functions...
			//	Integer value = (Integer) s.find("select MOD(s.count, 2) from Simple as s where s.id = 10" ).get(0);
			//	assertTrue( 0 == value.intValue() );
			//}

			s.Delete(simple);
			t.Commit();
			s.Close();
		}
コード例 #22
0
		public void ManyToMany()
		{
			// if( dialect is Dialect.HSQLDialect) return;

			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Container c = new Container();
			c.ManyToMany = new ArrayList();
			c.Bag = new ArrayList();
			Simple s1 = new Simple();
			Simple s2 = new Simple();
			s1.Count = 123;
			s2.Count = 654;
			Contained c1 = new Contained();
			c1.Bag = new ArrayList();
			c1.Bag.Add(c);
			c.Bag.Add(c1);
			c.ManyToMany.Add(s1);
			c.ManyToMany.Add(s2);
			object cid = s.Save(c);
			s.Save(s1, (long) 12);
			s.Save(s2, (long) -1);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.Load(typeof(Container), cid);
			Assert.AreEqual(1, c.Bag.Count);
			Assert.AreEqual(2, c.ManyToMany.Count);
			foreach (object obj in c.Bag)
			{
				c1 = (Contained) obj;
				break;
			}
			c.Bag.Remove(c1);
			c1.Bag.Remove(c);
			Assert.IsNotNull(c.ManyToMany[0]);
			c.ManyToMany.RemoveAt(0);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.Load(typeof(Container), cid);
			Assert.AreEqual(0, c.Bag.Count);
			Assert.AreEqual(1, c.ManyToMany.Count);
			c1 = (Contained) s.Load(typeof(Contained), c1.Id);
			Assert.AreEqual(0, c1.Bag.Count);
			Assert.AreEqual(1, s.Delete("from c in class ContainerX"));
			Assert.AreEqual(1, s.Delete("from c in class Contained"));
			Assert.AreEqual(2, s.Delete("from s in class Simple"));
			t.Commit();
			s.Close();
		}
コード例 #23
0
		public void CachedQueryOnInsert()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Simple simple = new Simple();
			simple.Name = "Simple 1";
			s.Save(simple, 10L);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			IQuery q = s.CreateQuery("from Simple s");
			IList list = q.SetCacheable(true).List();
			Assert.AreEqual(1, list.Count);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			q = s.CreateQuery("from Simple s");
			list = q.SetCacheable(true).List();
			Assert.AreEqual(1, list.Count);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			Simple simple2 = new Simple();
			simple2.Count = 133;
			s.Save(simple2, 12L);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			q = s.CreateQuery("from Simple s");
			list = q.SetCacheable(true).List();
			Assert.AreEqual(2, list.Count);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			q = s.CreateQuery("from Simple s");
			list = q.SetCacheable(true).List();
			Assert.AreEqual(2, list.Count);
			foreach (object obj in list)
			{
				s.Delete(obj);
			}
			t.Commit();
			s.Close();
		}
コード例 #24
0
		public void Container()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Container c = new Container();
			Simple x = new Simple();
			x.Count = 123;
			Simple y = new Simple();
			y.Count = 456;
			s.Save(x, (long) 1);
			s.Save(y, (long) 0);
			IList o2m = new ArrayList();
			o2m.Add(x);
			o2m.Add(null);
			o2m.Add(y);
			IList m2m = new ArrayList();
			m2m.Add(x);
			m2m.Add(null);
			m2m.Add(y);
			c.OneToMany = o2m;
			c.ManyToMany = m2m;
			IList comps = new ArrayList();
			Container.ContainerInnerClass ccic = new Container.ContainerInnerClass();
			ccic.Name = "foo";
			ccic.Simple = x;
			comps.Add(ccic);
			comps.Add(null);
			ccic = new Container.ContainerInnerClass();
			ccic.Name = "bar";
			ccic.Simple = y;
			comps.Add(ccic);

			ISet compos = new HashedSet();
			compos.Add(ccic);
			c.Composites = compos;
			c.Components = comps;
			One one = new One();
			Many many = new Many();
			ISet manies = new HashedSet();
			manies.Add(many);
			one.Manies = manies;
			many.One = one;
			ccic.Many = many;
			ccic.One = one;
			s.Save(one);
			s.Save(many);
			s.Save(c);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.Load(typeof(Container), c.Id);

			ccic = (Container.ContainerInnerClass) c.Components[2];
			Assert.AreEqual(ccic.One, ccic.Many.One);
			Assert.AreEqual(3, c.Components.Count);
			Assert.AreEqual(1, c.Composites.Count);
			Assert.AreEqual(3, c.OneToMany.Count);
			Assert.AreEqual(3, c.ManyToMany.Count);

			for (int i = 0; i < 3; i++)
			{
				Assert.AreEqual(c.ManyToMany[i], c.OneToMany[i]);
			}
			object o1 = c.OneToMany[0];
			object o2 = c.OneToMany[2];
			c.OneToMany.RemoveAt(2);
			c.OneToMany[0] = o2;
			c.OneToMany[1] = o1;
			o1 = c.Components[2];
			c.Components.RemoveAt(2);
			c.Components[0] = o1;
			c.ManyToMany[0] = c.ManyToMany[2];
			c.Composites.Add(o1);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.Load(typeof(Container), c.Id);
			Assert.AreEqual(1, c.Components.Count); //WAS: 2 - h2.0.3 comment
			Assert.AreEqual(2, c.Composites.Count);
			Assert.AreEqual(2, c.OneToMany.Count);
			Assert.AreEqual(3, c.ManyToMany.Count);
			Assert.IsNotNull(c.OneToMany[0]);
			Assert.IsNotNull(c.OneToMany[1]);

			((Container.ContainerInnerClass) c.Components[0]).Name = "a different name";
			IEnumerator enumer = c.Composites.GetEnumerator();
			enumer.MoveNext();
			((Container.ContainerInnerClass) enumer.Current).Name = "once again";
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.Load(typeof(Container), c.Id);
			Assert.AreEqual(1, c.Components.Count); //WAS: 2 -> h2.0.3 comment
			Assert.AreEqual(2, c.Composites.Count);
			Assert.AreEqual("a different name", ((Container.ContainerInnerClass) c.Components[0]).Name);
			enumer = c.Composites.GetEnumerator();
			bool found = false;
			while (enumer.MoveNext())
			{
				if (((Container.ContainerInnerClass) enumer.Current).Name.Equals("once again"))
				{
					found = true;
				}
			}

			Assert.IsTrue(found);
			c.OneToMany.Clear();
			c.ManyToMany.Clear();
			c.Composites.Clear();
			c.Components.Clear();
			s.Delete("from s in class Simple");
			s.Delete("from m in class Many");
			s.Delete("from o in class One");
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.Load(typeof(Container), c.Id);
			Assert.AreEqual(0, c.Components.Count);
			Assert.AreEqual(0, c.Composites.Count);
			Assert.AreEqual(0, c.OneToMany.Count);
			Assert.AreEqual(0, c.ManyToMany.Count);
			s.Delete(c);
			t.Commit();
			s.Close();
		}
コード例 #25
0
		public void SQLFunctions()
		{
			using (ISession s = OpenSession())
			{
				ITransaction t = s.BeginTransaction();
				Simple simple = new Simple();
				simple.Name = "Simple 1";
				s.Save(simple, (long) 10);

				if (Dialect is DB2Dialect)
				{
					s.CreateQuery("from s in class Simple where repeat('foo', 3) = 'foofoofoo'").List();
					s.CreateQuery("from s in class Simple where repeat(s.Name, 3) = 'foofoofoo'").List();
					s.CreateQuery("from s in class Simple where repeat( lower(s.Name), 3 + (1-1) / 2) = 'foofoofoo'").List();
				}

				Assert.AreEqual(1, s.CreateQuery("from s in class Simple where upper(s.Name) = 'SIMPLE 1'").List().Count);
				Assert.AreEqual(1,
				                s.CreateQuery(
				                	"from s in class Simple where not( upper(s.Name)='yada' or 1=2 or 'foo'='bar' or not('foo'='foo') or 'foo' like 'bar')")
				                	.List().Count);

				if (!(Dialect is MySQLDialect) && !(Dialect is MsSql2000Dialect))
				{
					// Dialect.MckoiDialect and Dialect.InterbaseDialect also included
					// My Sql has a funny concatenation operator
					Assert.AreEqual(1,
					                s.CreateQuery("from s in class Simple where lower(s.Name || ' foo')='simple 1 foo'").List().Count);
				}

				if ((Dialect is MsSql2000Dialect))
				{
					Assert.AreEqual(1,
					                s.CreateQuery("from s in class Simple where lower( s.Name + ' foo' ) = 'simple 1 foo'").List().
					                	Count);
				}

				/*
				 * TODO: uncomment if MckoiDialect is ever implemented 
				if( (dialect is Dialect.MckoiDialect) ) 
				{
					Assert.AreEqual( 1, s.CreateQuery("from s in class Simple where lower( concat(s.Name, ' foo') ) = 'simple 1 foo'").List().Count );
				}
				*/

				Simple other = new Simple();
				other.Name = "Simple 2";
				other.Count = 12;
				simple.Other = other;
				s.Save(other, (long) 20);
				Assert.AreEqual(1, s.CreateQuery("from s in class Simple where upper( s.Other.Name )='SIMPLE 2'").List().Count);
				Assert.AreEqual(0, s.CreateQuery("from s in class Simple where not (upper(s.Other.Name)='SIMPLE 2')").List().Count);
				Assert.AreEqual(1,
				                s.CreateQuery(
				                	"select distinct s from s in class Simple where ( ( s.Other.Count + 3) = (15*2)/2 and s.Count = 69) or ( (s.Other.Count + 2) / 7 ) = 2")
				                	.List().Count);
				Assert.AreEqual(1,
				                s.CreateQuery(
				                	"select s from s in class Simple where ( ( s.Other.Count + 3) = (15*2)/2 and s.Count = 69) or ( (s.Other.Count + 2) / 7 ) = 2 order by s.Other.Count")
				                	.List().Count);

				Simple min = new Simple();
				min.Count = -1;

				s.Save(min, (long) 30);

				if (Dialect.SupportsSubSelects)
				{
					Assert.AreEqual(2,
					                s.CreateQuery(
					                	"from s in class Simple where s.Count > ( select min(sim.Count) from sim in class NHibernate.DomainModel.Simple )")
					                	.List().Count);
					t.Commit();
					t = s.BeginTransaction();
					Assert.AreEqual(2,
					                s.CreateQuery(
					                	"from s in class Simple where s = some( select sim from sim in class NHibernate.DomainModel.Simple where sim.Count>=0) and s.Count >= 0")
					                	.List().Count);
					Assert.AreEqual(1,
					                s.CreateQuery(
					                	"from s in class Simple where s = some( select sim from sim in class NHibernate.DomainModel.Simple where sim.Other.Count=s.Other.Count ) and s.Other.Count > 0")
					                	.List().Count);
				}

				IEnumerator enumer =
					s.CreateQuery("select sum(s.Count) from s in class Simple group by s.Count having sum(s.Count) > 10 ").Enumerable()
						.GetEnumerator();
				Assert.IsTrue(enumer.MoveNext());
				Assert.AreEqual(12, (Int64) enumer.Current); // changed cast from Int32 to Int64 (H3.2)
				Assert.IsFalse(enumer.MoveNext());

				if (Dialect.SupportsSubSelects)
				{
					enumer =
						s.CreateQuery("select s.Count from s in class Simple group by s.Count having s.Count = 12").Enumerable().
							GetEnumerator();
					Assert.IsTrue(enumer.MoveNext());
				}

				enumer =
					s.CreateQuery(
						"select s.id, s.Count, count(t), max(t.Date) from s in class Simple, t in class Simple where s.Count = t.Count group by s.id, s.Count order by s.Count")
						.Enumerable().GetEnumerator();

				IQuery q = s.CreateQuery("from s in class Simple");
				q.SetMaxResults(10);
				Assert.AreEqual(3, q.List().Count);

				q = s.CreateQuery("from s in class Simple");
				q.SetMaxResults(1);
				Assert.AreEqual(1, q.List().Count);

				q = s.CreateQuery("from s in class Simple");
				Assert.AreEqual(3, q.List().Count);

				q = s.CreateQuery("from s in class Simple where s.Name = ?");
				q.SetString(0, "Simple 1");
				Assert.AreEqual(1, q.List().Count);

				q = s.CreateQuery("from s in class Simple where s.Name = ? and upper(s.Name) = ?");
				q.SetString(1, "SIMPLE 1");
				q.SetString(0, "Simple 1");
				q.SetFirstResult(0);
				Assert.IsTrue(q.Enumerable().GetEnumerator().MoveNext());

				q =
					s.CreateQuery(
						"from s in class Simple where s.Name = :foo and upper(s.Name) = :bar or s.Count=:count or s.Count=:count + 1");
				q.SetParameter("bar", "SIMPLE 1");
				q.SetString("foo", "Simple 1");
				q.SetInt32("count", 69);
				q.SetFirstResult(0);
				Assert.IsTrue(q.Enumerable().GetEnumerator().MoveNext());

				q = s.CreateQuery("select s.id from s in class Simple");
				q.SetFirstResult(1);
				q.SetMaxResults(2);
				IEnumerable enumerable = q.Enumerable();
				int i = 0;
				foreach (object obj in enumerable)
				{
					Assert.IsTrue(obj is Int64);
					i++;
				}

				Assert.AreEqual(2, i);

				q = s.CreateQuery("select all s, s.Other from s in class Simple where s = :s");
				q.SetParameter("s", simple);
				Assert.AreEqual(1, q.List().Count);

				q = s.CreateQuery("from s in class Simple where s.Name in (:name_list) and s.Count > :count");
				IList list = new ArrayList(2);
				list.Add("Simple 1");
				list.Add("foo");
				q.SetParameterList("name_list", list);
				q.SetParameter("count", (int) -1);
				Assert.AreEqual(1, q.List().Count);

				s.Delete(other);
				s.Delete(simple);
				s.Delete(min);
				t.Commit();
			}
		}
コード例 #26
0
		public void Locking()
		{
			ISession s = OpenSession();
			ITransaction tx = s.BeginTransaction();
			Simple s1 = new Simple();
			s1.Count = 1;
			Simple s2 = new Simple();
			s2.Count = 2;
			Simple s3 = new Simple();
			s3.Count = 3;
			Simple s4 = new Simple();
			s4.Count = 4;

			s.Save(s1, (long) 1);
			s.Save(s2, (long) 2);
			s.Save(s3, (long) 3);
			s.Save(s4, (long) 4);
			Assert.AreEqual(LockMode.Write, s.GetCurrentLockMode(s1));
			tx.Commit();
			s.Close();

			s = OpenSession();
			tx = s.BeginTransaction();
			s1 = (Simple) s.Load(typeof(Simple), (long) 1, LockMode.None);
			Assert.AreEqual(LockMode.Read, s.GetCurrentLockMode(s1));
			s2 = (Simple) s.Load(typeof(Simple), (long) 2, LockMode.Read);
			Assert.AreEqual(LockMode.Read, s.GetCurrentLockMode(s2));
			s3 = (Simple) s.Load(typeof(Simple), (long) 3, LockMode.Upgrade);
			Assert.AreEqual(LockMode.Upgrade, s.GetCurrentLockMode(s3));
			s4 = (Simple) s.Load(typeof(Simple), (long) 4, LockMode.UpgradeNoWait);
			Assert.AreEqual(LockMode.UpgradeNoWait, s.GetCurrentLockMode(s4));

			s1 = (Simple) s.Load(typeof(Simple), (long) 1, LockMode.Upgrade); //upgrade
			Assert.AreEqual(LockMode.Upgrade, s.GetCurrentLockMode(s1));
			s2 = (Simple) s.Load(typeof(Simple), (long) 2, LockMode.None);
			Assert.AreEqual(LockMode.Read, s.GetCurrentLockMode(s2));
			s3 = (Simple) s.Load(typeof(Simple), (long) 3, LockMode.Read);
			Assert.AreEqual(LockMode.Upgrade, s.GetCurrentLockMode(s3));
			s4 = (Simple) s.Load(typeof(Simple), (long) 4, LockMode.Upgrade);
			Assert.AreEqual(LockMode.UpgradeNoWait, s.GetCurrentLockMode(s4));

			s.Lock(s2, LockMode.Upgrade); //upgrade
			Assert.AreEqual(LockMode.Upgrade, s.GetCurrentLockMode(s2));
			s.Lock(s3, LockMode.Upgrade);
			Assert.AreEqual(LockMode.Upgrade, s.GetCurrentLockMode(s3));
			s.Lock(s1, LockMode.UpgradeNoWait);
			s.Lock(s4, LockMode.None);
			Assert.AreEqual(LockMode.UpgradeNoWait, s.GetCurrentLockMode(s4));

			tx.Commit();
			tx = s.BeginTransaction();

			Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(s3));
			Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(s1));
			Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(s2));
			Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(s4));

			s.Lock(s1, LockMode.Read); //upgrade
			Assert.AreEqual(LockMode.Read, s.GetCurrentLockMode(s1));
			s.Lock(s2, LockMode.Upgrade); //upgrade
			Assert.AreEqual(LockMode.Upgrade, s.GetCurrentLockMode(s2));
			s.Lock(s3, LockMode.UpgradeNoWait); //upgrade
			Assert.AreEqual(LockMode.UpgradeNoWait, s.GetCurrentLockMode(s3));
			s.Lock(s4, LockMode.None);
			Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(s4));

			s4.Name = "s4";
			s.Flush();
			Assert.AreEqual(LockMode.Write, s.GetCurrentLockMode(s4));
			tx.Commit();

			tx = s.BeginTransaction();
			Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(s3));
			Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(s1));
			Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(s2));
			Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(s4));

			s.Delete(s1);
			s.Delete(s2);
			s.Delete(s3);
			s.Delete(s4);
			tx.Commit();
			s.Close();
		}
コード例 #27
0
ファイル: FumTest.cs プロジェクト: NeoDarque/nhibernate-core
		public void UnflushedSessionSerialization()
		{
			///////////////////////////////////////////////////////////////////////////
			// Test insertions across serializations

			ISession s2;

			// NOTE: H2.1 has getSessions().openSession() here (and below),
			// instead of just the usual openSession()
			using (ISession s = sessions.OpenSession())
			{
				s.FlushMode = FlushMode.Never;

				Simple simple = new Simple();
				simple.Address = "123 Main St. Anytown USA";
				simple.Count = 1;
				simple.Date = new DateTime(2005, 1, 1);
				simple.Name = "My UnflushedSessionSerialization Simple";
				simple.Pay = 5000.0f;

				s.Save(simple, 10L);

				// Now, try to serialize session without flushing...
				s.Disconnect();

				s2 = SpoofSerialization(s);
			}

			Simple check, other;

			using (ISession s = s2)
			{
				s.Reconnect();

				Simple simple = (Simple) s.Load(typeof(Simple), 10L);
				other = new Simple();
				other.Init();
				s.Save(other, 11L);

				simple.Other = other;
				s.Flush();

				check = simple;
			}

			///////////////////////////////////////////////////////////////////////////
			// Test updates across serializations

			using (ISession s = sessions.OpenSession())
			{
				s.FlushMode = FlushMode.Never;
				Simple simple = (Simple) s.Get(typeof(Simple), 10L);
				Assert.AreEqual(check.Name, simple.Name, "Not same parent instances");
				Assert.AreEqual(check.Other.Name, other.Name, "Not same child instances");

				simple.Name = "My updated name";

				s.Disconnect();
				s2 = SpoofSerialization(s);

				check = simple;
			}

			using (ISession s = s2)
			{
				s.Reconnect();
				s.Flush();
			}

			///////////////////////////////////////////////////////////////////////////
			// Test deletions across serializations
			using (ISession s = sessions.OpenSession())
			{
				s.FlushMode = FlushMode.Never;
				Simple simple = (Simple) s.Get(typeof(Simple), 10L);
				Assert.AreEqual(check.Name, simple.Name, "Not same parent instances");
				Assert.AreEqual(check.Other.Name, other.Name, "Not same child instances");

				// Now, lets delete across serialization...
				s.Delete(simple);

				s.Disconnect();
				s2 = SpoofSerialization(s);
			}

			using (ISession s = s2)
			{
				s.Reconnect();
				s.Flush();
			}

			using (ISession s = OpenSession())
			{
				s.Delete("from Simple");
				s.Flush();
			}
		}
コード例 #28
0
		public void SetPropertiesOnQuery()
		{
			DateTime now = DateTime.Now;

			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();

			// create a new
			long key = 10;
			Simple simple = new Simple();
			simple.Name = "Simple 1";
			simple.Address = "Street 12";
			simple.Date = now;
			simple.Count = 99;

			s.Save(simple, key);

			t.Commit();

			t = s.BeginTransaction();

			IQuery q = s.CreateQuery("from s in class Simple where s.Name=:Name and s.Count=:Count");
			q.SetProperties(simple);

			Simple loadedSimple = (Simple) q.List()[0];
			Assert.AreEqual(99, loadedSimple.Count);
			Assert.AreEqual("Simple 1", loadedSimple.Name);
			Assert.AreEqual("Street 12", loadedSimple.Address);
			Assert.AreEqual(now.ToString(), loadedSimple.Date.ToString());

			s.Delete(simple);

			t.Commit();
			s.Close();
		}
コード例 #29
0
		public void TestCRUD()
		{
			long key = 10;
			long otherKey = 9;

			using (ISession s1 = OpenSession())
			using (ITransaction t1 = s1.BeginTransaction())
			{
				// create a new
				Simple simple1 = new Simple();
				Simple otherSimple1 = new Simple();

				simple1.Name = "Simple 1";
				simple1.Address = "Street 12";
				simple1.Date = testDateTime;
				simple1.Count = 99;

				otherSimple1.Name = "Other Simple 1";
				otherSimple1.Address = "Other Street 12";
				otherSimple1.Date = testDateTime;
				otherSimple1.Count = 98;

				simple1.Other = otherSimple1;

				s1.Save(otherSimple1, otherKey);
				s1.Save(simple1, key);

				t1.Commit();
			}

			// try to Load the object to make sure the save worked
			ISession s2 = OpenSession();
			ITransaction t2 = s2.BeginTransaction();

			Simple simple2 = (Simple) s2.Load(typeof(Simple), key);
			Simple otherSimple2 = (Simple) s2.Load(typeof(Simple), otherKey);

			// verify each property was saved as expected
			Assert.IsNotNull(simple2, "Unable to load object");
			Assert.IsNotNull(otherSimple2);
			Assert.AreSame(simple2.Other, otherSimple2);

			// update
			simple2.Count = 999;
			simple2.Name = "Simple 1 (Update)";
			simple2.Address = "Street 123";
			simple2.Date = updateDateTime;

			s2.Update(simple2, key);

			t2.Commit();
			s2.Close();

			// lets verify that the update worked 
			ISession s3 = OpenSession();
			ITransaction t3 = s3.BeginTransaction();

//			Simple simple3 = (Simple)s3.Load(typeof(Simple), key);
			Simple simple3 = (Simple) s3.CreateQuery("from Simple as s where s.id = ? and '?'='?'").SetInt64(0, key).List()[0];
			Simple otherSimple3;

			Assert.AreEqual(simple2.Count, simple3.Count);
			Assert.AreEqual(simple2.Name, simple3.Name);
			Assert.AreEqual(simple2.Address, simple3.Address);
			Assert.AreEqual(simple2.Date, simple3.Date);

			// note that the Other will not be the same object because
			// they were loaded in 2 different sessions
			otherSimple3 = simple3.Other;

			// the update worked - lets clear out the table
			s3.Delete(simple3);
			s3.Delete(otherSimple3);

			t3.Commit();
			s3.Close();

			// verify there is no other Simple objects in the db
			ISession s4 = OpenSession();
			Assert.AreEqual(0, s4.CreateCriteria(typeof(Simple)).List().Count);
			s4.Close();
		}
コード例 #30
0
		public void NHibernateOnly()
		{
			ISession s;
			for ( int n=2; n<4000; n*=2 ) {
			
				s = OpenSession();
				s.Delete("from Simple");
				s.Flush();
				Simple[] simples = new Simple[n];
				object[] ids = new object[n];
				for ( int i=0; i<n; i++ ) 
				{
					simples[i] = new Simple();
					simples[i].Init();
					simples[i].Count = i;
					ids[i] = (long) i;
					s.Save(simples[i], ids[i]);
				}
				s.Flush();
				s.Close();
			
				//Now do timings
			
				s = OpenSession();
				long time = DateTime.Now.Ticks;
				NHibernate(s, simples, ids, n, "h1");
				long hiber = DateTime.Now.Ticks - time;
				s.Close();
			
				s = OpenSession();
				time = DateTime.Now.Ticks;
				NHibernate(s, simples, ids, n, "h2");
				hiber += DateTime.Now.Ticks - time;
				s.Close();
			
				s = OpenSession();
				time = DateTime.Now.Ticks;
				NHibernate(s, simples, ids, n, "h2");
				hiber += DateTime.Now.Ticks - time;
				s.Close();
			
				System.Console.Out.WriteLine( "Objects: " + n + "\t - Hibernate: \t" + hiber );
			
			}
			s = OpenSession();
			s.Delete("from Simple");
			s.Flush();
			s.Close();
			System.GC.Collect();
		}