public void Basic_CRUD_should_work()
		{
			Assembly assembly = Assembly.Load("NHibernate.DomainModel");
			var cfg = new Configuration();
			if (TestConfigurationHelper.hibernateConfigFile != null)
			{
				cfg.Configure(TestConfigurationHelper.hibernateConfigFile);
			}
			cfg.AddResource("NHibernate.DomainModel.ParentChild.hbm.xml", assembly);

			var formatter = new BinaryFormatter();
			var memoryStream = new MemoryStream();
			formatter.Serialize(memoryStream, cfg);
			memoryStream.Position = 0;
			cfg = formatter.Deserialize(memoryStream) as Configuration;
			Assert.That(cfg, Is.Not.Null);

			var export = new SchemaExport(cfg);
			export.Execute(true, true, false);
			ISessionFactory sf = cfg.BuildSessionFactory();
			using (ISession session = sf.OpenSession())
			{
				using (ITransaction tran = session.BeginTransaction())
				{
					var parent = new Parent();
					var child = new Child();
					parent.Child = child;
					parent.X = 9;
					parent.Count = 5;
					child.Parent = parent;
					child.Count = 3;
					child.X = 4;
					session.Save(parent);
					session.Save(child);
					tran.Commit();
				}
			}

			using (ISession session = sf.OpenSession())
			{
				var parent = session.Get<Parent>(1L);
				Assert.That(parent.Count, Is.EqualTo(5));
				Assert.That(parent.X, Is.EqualTo(9));
				Assert.That(parent.Child, Is.Not.Null);
				Assert.That(parent.Child.X, Is.EqualTo(4));
				Assert.That(parent.Child.Count, Is.EqualTo(3));
				Assert.That(parent.Child.Parent, Is.EqualTo(parent));
			}

			using (ISession session = sf.OpenSession())
			{
				using (ITransaction tran = session.BeginTransaction())
				{
					var p = session.Get<Parent>(1L);
					var c = session.Get<Child>(1L);
					session.Delete(c);
					session.Delete(p);
					tran.Commit();
				}
			}
			using (ISession session = sf.OpenSession())
			{
				var p = session.Get<Parent>(1L);
				Assert.That(p, Is.Null);
			}
			export.Drop(true, true);
		}
Пример #2
0
		public void ParentChild()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Parent p = new Parent();
			Child c = new Child();
			c.Parent = p;
			p.Child = c;
			s.Save(p);
			s.Save(c);
			t.Commit();
			s.Flush();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Child) s.Load(typeof(Child), c.Id);
			p = c.Parent;
			Assert.IsNotNull(p, "1-1 parent");
			c.Count = 32;
			p.Count = 66;
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Child) s.Load(typeof(Child), c.Id);
			p = c.Parent;
			Assert.AreEqual(66, p.Count, "1-1 update");
			Assert.AreEqual(32, c.Count, "1-1 update");
			Assert.AreEqual(1, s.CreateQuery("from c in class NHibernate.DomainModel.Child where c.Parent.Count=66").List().Count,
			                "1-1 query");
			Assert.AreEqual(2, ((object[]) s.CreateQuery("from Parent p join p.Child c where p.Count=66").List()[0]).Length,
			                "1-1 query");

			s.CreateQuery("select c, c.Parent from c in class NHibernate.DomainModel.Child order by c.Parent.Count").List();
			s.CreateQuery(
				"select c, c.Parent from c in class NHibernate.DomainModel.Child where c.Parent.Count=66 order by c.Parent.Count").
				List();
			s.CreateQuery(
				"select c, c.Parent, c.Parent.Count from c in class NHibernate.DomainModel.Child order by c.Parent.Count").
				Enumerable();
			Assert.AreEqual(1,
			                s.CreateQuery("FROM p in CLASS NHibernate.DomainModel.Parent WHERE p.Count=?").SetInt32(0, 66).List()
			                	.Count, "1-1 query");
			s.Delete(c);
			s.Delete(p);
			t.Commit();
			s.Close();
		}