예제 #1
0
		public void CreateUpdateReadDelete()
		{
			Document doc;
			DateTime? initVersion;

			using (IStatelessSession ss = sessions.OpenStatelessSession())
			{
				ITransaction tx;
				using (tx = ss.BeginTransaction())
				{
					doc = new Document("blah blah blah", "Blahs");
					ss.Insert(doc);
					Assert.IsNotNull(doc.LastModified);
					initVersion = doc.LastModified;
					Assert.IsTrue(initVersion.HasValue);
					tx.Commit();
				}
				Thread.Sleep(1100); // Ensure version increment (some dialects lack fractional seconds).
				using (tx = ss.BeginTransaction())
				{
					doc.Text = "blah blah blah .... blah";
					ss.Update(doc);
					Assert.IsTrue(doc.LastModified.HasValue);
					Assert.AreNotEqual(initVersion, doc.LastModified);
					tx.Commit();
				}
				using (tx = ss.BeginTransaction())
				{
					doc.Text = "blah blah blah .... blah blay";
					ss.Update(doc);
					tx.Commit();
				}
				var doc2 = ss.Get<Document>("Blahs");
				Assert.AreEqual("Blahs", doc2.Name);
				Assert.AreEqual(doc.Text, doc2.Text);

				doc2 = (Document) ss.CreateQuery("from Document where text is not null").UniqueResult();
				Assert.AreEqual("Blahs", doc2.Name);
				Assert.AreEqual(doc.Text, doc2.Text);

				doc2 = (Document) ss.CreateSQLQuery("select * from Document").AddEntity(typeof (Document)).UniqueResult();
				Assert.AreEqual("Blahs", doc2.Name);
				Assert.AreEqual(doc.Text, doc2.Text);

				doc2 = ss.CreateCriteria<Document>().UniqueResult<Document>();
				Assert.AreEqual("Blahs", doc2.Name);
				Assert.AreEqual(doc.Text, doc2.Text);

				doc2 = (Document) ss.CreateCriteria(typeof (Document)).UniqueResult();
				Assert.AreEqual("Blahs", doc2.Name);
				Assert.AreEqual(doc.Text, doc2.Text);

				using (tx = ss.BeginTransaction())
				{
					ss.Delete(doc);
					tx.Commit();
				}
			}
		}
예제 #2
0
		public void HqlBulk()
		{
			IStatelessSession ss = sessions.OpenStatelessSession();
			ITransaction tx = ss.BeginTransaction();
			var doc = new Document("blah blah blah", "Blahs");
			ss.Insert(doc);
			var paper = new Paper {Color = "White"};
			ss.Insert(paper);
			tx.Commit();

			tx = ss.BeginTransaction();
			int count =
				ss.CreateQuery("update Document set Name = :newName where Name = :oldName").SetString("newName", "Foos").SetString(
					"oldName", "Blahs").ExecuteUpdate();
			Assert.AreEqual(1, count, "hql-delete on stateless session");
			count = ss.CreateQuery("update Paper set Color = :newColor").SetString("newColor", "Goldenrod").ExecuteUpdate();
			Assert.AreEqual(1, count, "hql-delete on stateless session");
			tx.Commit();

			tx = ss.BeginTransaction();
			count = ss.CreateQuery("delete Document").ExecuteUpdate();
			Assert.AreEqual(1, count, "hql-delete on stateless session");
			count = ss.CreateQuery("delete Paper").ExecuteUpdate();
			Assert.AreEqual(1, count, "hql-delete on stateless session");
			tx.Commit();
			ss.Close();
		}