public void UnionSubclass()
        {
            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    Location mel  = new Location("Melbourne, Australia");
                    Location atl  = new Location("Atlanta, GA");
                    Location mars = new Location("Mars");
                    s.Save(mel);
                    s.Save(atl);
                    s.Save(mars);

                    Human gavin = new Human();
                    gavin.Identity = "gavin";
                    gavin.Sex      = 'M';
                    gavin.Location = mel;
                    mel.AddBeing(gavin);

                    Alien x23y4 = new Alien();
                    x23y4.Identity = "x23y4$$hu%3";
                    x23y4.Location = mars;
                    x23y4.Species  = "martian";
                    mars.AddBeing(x23y4);

                    Hive hive = new Hive();
                    hive.Location = mars;
                    hive.Members.Add(x23y4);
                    x23y4.Hive = hive;
                    s.Persist(hive);

                    Assert.AreEqual(2, s.CreateQuery("from Being").List().Count);
                    Assert.AreEqual(1, s.CreateQuery("from Being b where b.class = Alien").List().Count);
                    Assert.AreEqual(1, s.CreateQuery("from Alien").List().Count);
                    s.Clear();

                    IList <Being> beings = s.CreateQuery("from Being b left join fetch b.location").List <Being>();
                    foreach (Being b in beings)
                    {
                        Assert.IsTrue(NHibernateUtil.IsInitialized(b.Location));
                        Assert.IsNotNull(b.Location.Name);
                        Assert.IsNotNull(b.Identity);
                        Assert.IsNotNull(b.Species);
                    }
                    Assert.AreEqual(2, beings.Count);
                    s.Clear();

                    beings = s.CreateQuery("from Being").List <Being>();
                    foreach (Being b in beings)
                    {
                        Assert.IsFalse(NHibernateUtil.IsInitialized(b.Location));
                        Assert.IsNotNull(b.Location.Name);
                        Assert.IsNotNull(b.Identity);
                        Assert.IsNotNull(b.Species);
                    }
                    Assert.AreEqual(2, beings.Count);
                    s.Clear();

                    IList <Location> locations = s.CreateQuery("from Location").List <Location>();
                    int count = 0;
                    foreach (Location l in locations)
                    {
                        Assert.IsNotNull(l.Name);
                        foreach (Being o in l.Beings)
                        {
                            count++;
                            Assert.AreSame(o.Location, l);
                        }
                    }
                    Assert.AreEqual(2, count);
                    Assert.AreEqual(3, locations.Count);
                    s.Clear();

                    locations = s.CreateQuery("from Location loc left join fetch loc.beings").List <Location>();
                    count     = 0;
                    foreach (Location l in locations)
                    {
                        Assert.IsNotNull(l.Name);
                        foreach (Being o in l.Beings)
                        {
                            count++;
                            Assert.AreSame(o.Location, l);
                        }
                    }

                    Assert.AreEqual(2, count);
                    Assert.AreEqual(3, locations.Count);
                    s.Clear();

                    gavin = s.Get <Human>(gavin.Id);
                    atl   = s.Get <Location>(atl.Id);

                    atl.AddBeing(gavin);
                    Assert.AreEqual(1, s.CreateQuery("from Human h where h.location.name like '%GA'").List().Count);
                    s.Delete(gavin);
                    x23y4 = (Alien)s.CreateCriteria(typeof(Alien)).UniqueResult();
                    s.Delete(x23y4.Hive);
                    Assert.AreEqual(0, s.CreateQuery("from Being").List().Count);
                    t.Commit();
                    s.Close();
                }
            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    s.Delete("from Location");
                    t.Commit();
                }
        }
        public void UnionSubclassManyToOne()
        {
            ISession     s    = OpenSession();
            ITransaction t    = s.BeginTransaction();
            Location     mel  = new Location("Melbourne, Australia");
            Location     mars = new Location("Mars");

            s.Save(mel);
            s.Save(mars);

            Human gavin = new Human();

            gavin.Identity = "gavin";
            gavin.Sex      = 'M';
            gavin.Location = mel;
            mel.AddBeing(gavin);

            Alien x23y4 = new Alien();

            x23y4.Identity = "x23y4$$hu%3";
            x23y4.Location = mars;
            x23y4.Species  = "martian";
            mars.AddBeing(x23y4);

            Hive hive = new Hive();

            hive.Location = mars;
            hive.Members.Add(x23y4);
            x23y4.Hive = hive;
            s.Persist(hive);

            Thing thing = new Thing();

            thing.Description = "some thing";
            thing.Owner       = gavin;
            gavin.Things.Add(thing);
            s.Save(thing);
            s.Flush();

            s.Clear();

            thing = (Thing)s.CreateQuery("from Thing t left join fetch t.owner").UniqueResult();
            Assert.IsTrue(NHibernateUtil.IsInitialized(thing.Owner));
            Assert.AreEqual("gavin", thing.Owner.Identity);
            s.Clear();

            thing = (Thing)s.CreateQuery("select t from Thing t left join t.owner where t.owner.identity='gavin'").UniqueResult();
            Assert.IsFalse(NHibernateUtil.IsInitialized(thing.Owner));
            Assert.AreEqual("gavin", thing.Owner.Identity);
            s.Clear();

            gavin = (Human)s.CreateQuery("from Human h left join fetch h.things").UniqueResult();
            Assert.IsTrue(NHibernateUtil.IsInitialized(gavin.Things));
            Assert.AreEqual("some thing", ((Thing)gavin.Things[0]).Description);
            s.Clear();

            Assert.AreEqual(2, s.CreateQuery("from Being b left join fetch b.things").List().Count);
            s.Clear();

            gavin = (Human)s.CreateQuery("from Being b join fetch b.things").UniqueResult();
            Assert.IsTrue(NHibernateUtil.IsInitialized(gavin.Things));
            Assert.AreEqual("some thing", ((Thing)gavin.Things[0]).Description);
            s.Clear();

            gavin = (Human)s.CreateQuery("select h from Human h join h.things t where t.description='some thing'").UniqueResult();
            Assert.IsFalse(NHibernateUtil.IsInitialized(gavin.Things));
            Assert.AreEqual("some thing", ((Thing)gavin.Things[0]).Description);
            s.Clear();

            gavin = (Human)s.CreateQuery("select b from Being b join b.things t where t.description='some thing'").UniqueResult();
            Assert.IsFalse(NHibernateUtil.IsInitialized(gavin.Things));
            Assert.AreEqual("some thing", ((Thing)gavin.Things[0]).Description);
            s.Clear();

            thing = s.Get <Thing>(thing.Id);
            Assert.IsFalse(NHibernateUtil.IsInitialized(thing.Owner));
            Assert.AreEqual("gavin", thing.Owner.Identity);

            thing.Owner.Things.Remove(thing);
            thing.Owner = x23y4;
            x23y4.Things.Add(thing);

            s.Flush();
            s.Clear();

            thing = s.Get <Thing>(thing.Id);
            Assert.IsFalse(NHibernateUtil.IsInitialized(thing.Owner));
            Assert.AreEqual("x23y4$$hu%3", thing.Owner.Identity);

            s.Delete(thing);
            x23y4 = (Alien)s.CreateCriteria(typeof(Alien)).UniqueResult();
            s.Delete(x23y4.Hive);
            s.Delete(s.Get <Location>(mel.Id));
            s.Delete(s.Get <Location>(mars.Id));
            Assert.AreEqual(0, s.CreateQuery("from Being").List().Count);
            t.Commit();
            s.Close();
        }
        public void UnionSubclassOneToMany()
        {
            ISession     s    = OpenSession();
            ITransaction t    = s.BeginTransaction();
            Location     mel  = new Location("Melbourne, Australia");
            Location     mars = new Location("Mars");

            s.Save(mel);
            s.Save(mars);

            Human gavin = new Human();

            gavin.Identity = "gavin";
            gavin.Sex      = 'M';
            gavin.Location = mel;
            mel.AddBeing(gavin);

            Alien x23y4 = new Alien();

            x23y4.Identity = "x23y4$$hu%3";
            x23y4.Location = mars;
            x23y4.Species  = "martian";
            mars.AddBeing(x23y4);

            Alien yy3dk = new Alien();

            yy3dk.Identity = "yy3dk&*!!!";
            yy3dk.Location = mars;
            yy3dk.Species  = "martian";
            mars.AddBeing(yy3dk);

            Hive hive = new Hive();

            hive.Location = mars;
            hive.Members.Add(x23y4);
            x23y4.Hive = hive;
            hive.Members.Add(yy3dk);
            yy3dk.Hive = hive;
            s.Persist(hive);

            yy3dk.Hivemates.Add(x23y4);
            x23y4.Hivemates.Add(yy3dk);

            s.Flush();
            s.Clear();

            hive = (Hive)s.CreateQuery("from Hive h").UniqueResult();
            Assert.IsFalse(NHibernateUtil.IsInitialized(hive.Members));
            Assert.AreEqual(2, hive.Members.Count);

            s.Clear();

            hive = (Hive)s.CreateQuery("from Hive h left join fetch h.location left join fetch h.members").UniqueResult();
            Assert.IsTrue(NHibernateUtil.IsInitialized(hive.Members));
            Assert.AreEqual(2, hive.Members.Count);

            s.Clear();

            x23y4 = (Alien)s.CreateQuery("from Alien a left join fetch a.hivemates where a.identity like 'x%'").UniqueResult();
            Assert.IsTrue(NHibernateUtil.IsInitialized(x23y4.Hivemates));
            Assert.AreEqual(1, x23y4.Hivemates.Count);

            s.Clear();

            x23y4 = (Alien)s.CreateQuery("from Alien a where a.identity like 'x%'").UniqueResult();
            Assert.IsFalse(NHibernateUtil.IsInitialized(x23y4.Hivemates));
            Assert.AreEqual(1, x23y4.Hivemates.Count);

            s.Clear();

            x23y4 = (Alien)s.CreateCriteria(typeof(Alien)).AddOrder(Order.Asc("identity")).List()[0];
            s.Delete(x23y4.Hive);
            s.Delete(s.Get <Location>(mel.Id));
            s.Delete(s.Get <Location>(mars.Id));
            Assert.IsTrue(s.CreateQuery("from Being").List().Count == 0);
            t.Commit();
            s.Close();
        }
		public void UnionSubclassOneToMany()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Location mel = new Location("Melbourne, Australia");
			Location mars = new Location("Mars");
			s.Save(mel);
			s.Save(mars);

			Human gavin = new Human();
			gavin.Identity = "gavin";
			gavin.Sex = 'M';
			gavin.Location = mel;
			mel.AddBeing(gavin);

			Alien x23y4 = new Alien();
			x23y4.Identity = "x23y4$$hu%3";
			x23y4.Location = mars;
			x23y4.Species = "martian";
			mars.AddBeing(x23y4);

			Alien yy3dk = new Alien();
			yy3dk.Identity = "yy3dk&*!!!";
			yy3dk.Location = mars;
			yy3dk.Species = "martian";
			mars.AddBeing(yy3dk);

			Hive hive = new Hive();
			hive.Location = mars;
			hive.Members.Add(x23y4);
			x23y4.Hive = hive;
			hive.Members.Add(yy3dk);
			yy3dk.Hive = hive;
			s.Persist(hive);

			yy3dk.Hivemates.Add(x23y4);
			x23y4.Hivemates.Add(yy3dk);

			s.Flush();
			s.Clear();

			hive = (Hive)s.CreateQuery("from Hive h").UniqueResult();
			Assert.IsFalse(NHibernateUtil.IsInitialized(hive.Members));
			Assert.AreEqual(2, hive.Members.Count);

			s.Clear();

			hive = (Hive)s.CreateQuery("from Hive h left join fetch h.location left join fetch h.members").UniqueResult();
			Assert.IsTrue(NHibernateUtil.IsInitialized(hive.Members));
			Assert.AreEqual(2, hive.Members.Count);

			s.Clear();

			x23y4 = (Alien)s.CreateQuery("from Alien a left join fetch a.hivemates where a.identity like 'x%'").UniqueResult();
			Assert.IsTrue(NHibernateUtil.IsInitialized(x23y4.Hivemates));
			Assert.AreEqual(1, x23y4.Hivemates.Count);

			s.Clear();

			x23y4 = (Alien)s.CreateQuery("from Alien a where a.identity like 'x%'").UniqueResult();
			Assert.IsFalse(NHibernateUtil.IsInitialized(x23y4.Hivemates));
			Assert.AreEqual(1, x23y4.Hivemates.Count);

			s.Clear();

			x23y4 = (Alien)s.CreateCriteria(typeof(Alien)).AddOrder(Order.Asc("identity")).List()[0];
			s.Delete(x23y4.Hive);
			s.Delete(s.Get<Location>(mel.Id));
			s.Delete(s.Get<Location>(mars.Id));
			Assert.IsTrue(s.CreateQuery("from Being").List().Count == 0);
			t.Commit();
			s.Close();
		}
		public void UnionSubclass()
		{
			using (ISession s = OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				Location mel = new Location("Melbourne, Australia");
				Location atl = new Location("Atlanta, GA");
				Location mars = new Location("Mars");
				s.Save(mel);
				s.Save(atl);
				s.Save(mars);

				Human gavin = new Human();
				gavin.Identity = "gavin";
				gavin.Sex = 'M';
				gavin.Location = mel;
				mel.AddBeing(gavin);

				Alien x23y4 = new Alien();
				x23y4.Identity = "x23y4$$hu%3";
				x23y4.Location = mars;
				x23y4.Species = "martian";
				mars.AddBeing(x23y4);

				Hive hive = new Hive();
				hive.Location = mars;
				hive.Members.Add(x23y4);
				x23y4.Hive = hive;
				s.Persist(hive);

				Assert.AreEqual(2, s.CreateQuery("from Being").List().Count);
				Assert.AreEqual(1, s.CreateQuery("from Being b where b.class = Alien").List().Count);
				Assert.AreEqual(1, s.CreateQuery("from Alien").List().Count);
				s.Clear();

				IList<Being> beings = s.CreateQuery("from Being b left join fetch b.location").List<Being>();
				foreach (Being b in beings)
				{
					Assert.IsTrue(NHibernateUtil.IsInitialized(b.Location));
					Assert.IsNotNull(b.Location.Name);
					Assert.IsNotNull(b.Identity);
					Assert.IsNotNull(b.Species);
				}
				Assert.AreEqual(2, beings.Count);
				s.Clear();

				beings = s.CreateQuery("from Being").List<Being>();
				foreach (Being b in beings)
				{
					Assert.IsFalse(NHibernateUtil.IsInitialized(b.Location));
					Assert.IsNotNull(b.Location.Name);
					Assert.IsNotNull(b.Identity);
					Assert.IsNotNull(b.Species);
				}
				Assert.AreEqual(2, beings.Count);
				s.Clear();

				IList<Location> locations = s.CreateQuery("from Location").List<Location>();
				int count = 0;
				foreach (Location l in locations)
				{
					Assert.IsNotNull(l.Name);
					foreach (Being o in l.Beings)
					{
						count++;
						Assert.AreSame(o.Location, l);
					}
				}
				Assert.AreEqual(2, count);
				Assert.AreEqual(3, locations.Count);
				s.Clear();

				locations = s.CreateQuery("from Location loc left join fetch loc.beings").List<Location>();
				count = 0;
				foreach (Location l in locations)
				{
					Assert.IsNotNull(l.Name);
					foreach (Being o in l.Beings)
					{
						count++;
						Assert.AreSame(o.Location, l);
					}
				}

				Assert.AreEqual(2, count);
				Assert.AreEqual(3, locations.Count);
				s.Clear();

				gavin = s.Get<Human>(gavin.Id);
				atl = s.Get<Location>(atl.Id);

				atl.AddBeing(gavin);
				Assert.AreEqual(1, s.CreateQuery("from Human h where h.location.name like '%GA'").List().Count);
				s.Delete(gavin);
				x23y4 = (Alien) s.CreateCriteria(typeof (Alien)).UniqueResult();
				s.Delete(x23y4.Hive);
				Assert.AreEqual(0, s.CreateQuery("from Being").List().Count);
				t.Commit();
				s.Close();
			}
			using (ISession s = OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				s.Delete("from Location");
				t.Commit();
			}
		}
		public void UnionSubclassManyToOne()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Location mel = new Location("Melbourne, Australia");
			Location mars = new Location("Mars");
			s.Save(mel);
			s.Save(mars);

			Human gavin = new Human();
			gavin.Identity = "gavin";
			gavin.Sex = 'M';
			gavin.Location = mel;
			mel.AddBeing(gavin);

			Alien x23y4 = new Alien();
			x23y4.Identity = "x23y4$$hu%3";
			x23y4.Location = mars;
			x23y4.Species = "martian";
			mars.AddBeing(x23y4);

			Hive hive = new Hive();
			hive.Location = mars;
			hive.Members.Add(x23y4);
			x23y4.Hive = hive;
			s.Persist(hive);

			Thing thing = new Thing();
			thing.Description = "some thing";
			thing.Owner = gavin;
			gavin.Things.Add(thing);
			s.Save(thing);
			s.Flush();

			s.Clear();

			thing = (Thing)s.CreateQuery("from Thing t left join fetch t.owner").UniqueResult();
			Assert.IsTrue(NHibernateUtil.IsInitialized(thing.Owner));
			Assert.AreEqual("gavin", thing.Owner.Identity);
			s.Clear();

			thing = (Thing)s.CreateQuery("select t from Thing t left join t.owner where t.owner.identity='gavin'").UniqueResult();
			Assert.IsFalse(NHibernateUtil.IsInitialized(thing.Owner));
			Assert.AreEqual("gavin", thing.Owner.Identity);
			s.Clear();

			gavin = (Human)s.CreateQuery("from Human h left join fetch h.things").UniqueResult();
			Assert.IsTrue(NHibernateUtil.IsInitialized(gavin.Things));
			Assert.AreEqual("some thing", ((Thing)gavin.Things[0]).Description);
			s.Clear();

			Assert.AreEqual(2, s.CreateQuery("from Being b left join fetch b.things").List().Count);
			s.Clear();

			gavin = (Human)s.CreateQuery("from Being b join fetch b.things").UniqueResult();
			Assert.IsTrue(NHibernateUtil.IsInitialized(gavin.Things));
			Assert.AreEqual("some thing", ((Thing)gavin.Things[0]).Description);
			s.Clear();

			gavin = (Human)s.CreateQuery("select h from Human h join h.things t where t.description='some thing'").UniqueResult();
			Assert.IsFalse(NHibernateUtil.IsInitialized(gavin.Things));
			Assert.AreEqual("some thing", ((Thing)gavin.Things[0]).Description);
			s.Clear();

			gavin = (Human)s.CreateQuery("select b from Being b join b.things t where t.description='some thing'").UniqueResult();
			Assert.IsFalse(NHibernateUtil.IsInitialized(gavin.Things));
			Assert.AreEqual("some thing", ((Thing)gavin.Things[0]).Description);
			s.Clear();

			thing = s.Get<Thing>(thing.Id);
			Assert.IsFalse(NHibernateUtil.IsInitialized(thing.Owner));
			Assert.AreEqual("gavin", thing.Owner.Identity);

			thing.Owner.Things.Remove(thing);
			thing.Owner = x23y4;
			x23y4.Things.Add(thing);

			s.Flush();
			s.Clear();

			thing = s.Get<Thing>(thing.Id);
			Assert.IsFalse(NHibernateUtil.IsInitialized(thing.Owner));
			Assert.AreEqual("x23y4$$hu%3", thing.Owner.Identity);

			s.Delete(thing);
			x23y4 = (Alien)s.CreateCriteria(typeof(Alien)).UniqueResult();
			s.Delete(x23y4.Hive);
			s.Delete(s.Get<Location>(mel.Id));
			s.Delete(s.Get<Location>(mars.Id));
			Assert.AreEqual(0, s.CreateQuery("from Being").List().Count);
			t.Commit();
			s.Close();
		}