context: using System.Collections;
  using System.Collections.Generic;
  using NHibernate.Criterion;
  using NUnit.Framework;
  
  namespace NHibernate.Test.Unionsubclass
  {
  	[TestFixture]
  	public class UnionSubclassFixture : TestCase
  	{
  		protected override string MappingsAssembly
  		{
  			get { return "NHibernate.Test"; }
  		}
  
  		protected override IList Mappings
  		{
  			get { return new string[] { "Unionsubclass.Beings.hbm.xml" }; }
  		}
  
  		[Test]
  		public void UnionSubclassCollection()
  		{
  			using (ISession s = OpenSession())
  			{
  				using (ITransaction t = s.BeginTransaction())
  				{
  					Location mel = new Location("Earth");
  					s.Save(mel);
  
  					Human gavin = new Human();
  					gavin.Identity = "gavin";
  					gavin.Sex = 'M';
  					gavin.Location = mel;
  					mel.AddBeing(gavin);
  
  					gavin.Info.Add("bar");
  					gavin.Info.Add("y");
  
  					t.Commit();
  				}
  				s.Close();
  			}
  
  			using (ISession s = OpenSession())
  			{
  				using (ITransaction t = s.BeginTransaction())
  				{
  					Human gavin = (Human)s.CreateCriteria(typeof(Human)).UniqueResult();
  					Assert.AreEqual(gavin.Info.Count, 2);
  					s.Delete(gavin);
  					s.Delete(gavin.Location);
  					t.Commit();
  				}
  				s.Close();
  			}
  		}
  
  		[Test]
  		public void UnionSubclassFetchMode()
  		{
  			ISession s = OpenSession();
  			ITransaction t = s.BeginTransaction();
  			Location mel = new Location("Earth");
  			s.Save(mel);
  
  			Human gavin = new Human();
  			gavin.Identity = "gavin";
  			gavin.Sex = 'M';
  			gavin.Location = mel;
  			mel.AddBeing(gavin);
  			Human max = new Human();
  			max.Identity = "max";
  			max.Sex = 'M';
  			max.Location = mel;
  			mel.AddBeing(gavin);
  
  			s.Flush();
  			s.Clear();
  
  			IList list =
  				s.CreateCriteria(typeof(Human)).SetFetchMode("location", FetchMode.Join).SetFetchMode("location.beings",
  																																															 FetchMode.Join).List();
  
  			for (int i = 0; i < list.Count; i++)
  			{
  				Human h = (Human)list[i];
  				Assert.IsTrue(NHibernateUtil.IsInitialized(h.Location));
  				Assert.IsTrue(NHibernateUtil.IsInitialized(h.Location.Beings));
  				s.Delete(h);
  			}
  			s.Delete(s.Get<Location>(mel.Id));
  			t.Commit();
  			s.Close();
  		}
  
  		[Test]
  		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();
  		}
  
  		[Test]
  		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();
  
  
gt: 			thing = s.Get<Thing>(thing.Id);