Пример #1
0
		public void BatchLoad()
		{
			Baz baz, baz2, baz3;

			using (ISession s = OpenSession())
			{
				baz = new Baz();
				SortedSet stringSet = new SortedSet();
				stringSet.Add("foo");
				stringSet.Add("bar");
				ISet fooSet = new HashedSet();

				for (int i = 0; i < 3; i++)
				{
					Foo foo = new Foo();
					s.Save(foo);
					fooSet.Add(foo);
				}

				baz.FooSet = fooSet;
				baz.StringSet = stringSet;
				s.Save(baz);

				baz2 = new Baz();
				fooSet = new HashedSet();
				for (int i = 0; i < 2; i++)
				{
					Foo foo = new Foo();
					s.Save(foo);
					fooSet.Add(foo);
				}
				baz2.FooSet = fooSet;
				s.Save(baz2);

				baz3 = new Baz();
				stringSet = new SortedSet();
				stringSet.Add("foo");
				stringSet.Add("baz");
				baz3.StringSet = stringSet;
				s.Save(baz3);
				s.Flush();
			}

			using (ISession s = OpenSession())
			{
				baz = (Baz) s.Load(typeof(Baz), baz.Code);
				baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
				baz3 = (Baz) s.Load(typeof(Baz), baz3.Code);

				Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
				Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.FooSet));
				Assert.IsFalse(NHibernateUtil.IsInitialized(baz3.FooSet));

				Assert.IsFalse(NHibernateUtil.IsInitialized(baz.StringSet));
				Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.StringSet));
				Assert.IsFalse(NHibernateUtil.IsInitialized(baz3.StringSet));

				Assert.AreEqual(3, baz.FooSet.Count);

				Assert.IsTrue(NHibernateUtil.IsInitialized(baz.FooSet));
				Assert.IsTrue(NHibernateUtil.IsInitialized(baz2.FooSet));
				Assert.IsTrue(NHibernateUtil.IsInitialized(baz3.FooSet));

				Assert.AreEqual(2, baz2.FooSet.Count);

				Assert.IsTrue(baz3.StringSet.Contains("baz"));

				Assert.IsTrue(NHibernateUtil.IsInitialized(baz.StringSet));
				Assert.IsTrue(NHibernateUtil.IsInitialized(baz2.StringSet));
				Assert.IsTrue(NHibernateUtil.IsInitialized(baz3.StringSet));

				Assert.AreEqual(2, baz.StringSet.Count);
				Assert.AreEqual(0, baz2.StringSet.Count);

				s.Delete(baz);
				s.Delete(baz2);
				s.Delete(baz3);

				IEnumerable en = new JoinedEnumerable(
					new IEnumerable[] {baz.FooSet, baz2.FooSet});

				foreach (object obj in en)
				{
					s.Delete(obj);
				}

				s.Flush();
			}
		}
Пример #2
0
		public void TransientOrphanDelete()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Baz baz = new Baz();
			ISet bars = new HashedSet();
			baz.CascadingBars = bars;
			bars.Add(new Bar());
			bars.Add(new Bar());
			bars.Add(new Bar());
			IList foos = new ArrayList();
			foos.Add(new Foo());
			foos.Add(new Foo());
			baz.FooBag = foos;
			s.Save(baz);

			IEnumerator enumer = new JoinedEnumerable(new IEnumerable[] {foos, bars}).GetEnumerator();
			while (enumer.MoveNext())
			{
				FooComponent cmp = ((Foo) enumer.Current).Component;
				s.Delete(cmp.Glarch);
				cmp.Glarch = null;
			}

			t.Commit();
			s.Close();

			enumer = bars.GetEnumerator();
			enumer.MoveNext();
			bars.Remove(enumer.Current);
			foos.RemoveAt(1);
			s = OpenSession();
			t = s.BeginTransaction();
			s.Update(baz);
			Assert.AreEqual(2, s.CreateQuery("from Bar bar").List().Count);
			Assert.AreEqual(3, s.CreateQuery("from Foo foo").List().Count);
			t.Commit();
			s.Close();

			foos.RemoveAt(0);
			s = OpenSession();
			t = s.BeginTransaction();
			s.Update(baz);
			enumer = bars.GetEnumerator();
			enumer.MoveNext();
			bars.Remove(enumer.Current);
			s.Delete(baz);
			s.Flush();
			Assert.AreEqual(0, s.CreateQuery("from Foo foo").List().Count);
			t.Commit();
			s.Close();
		}
		protected string GenerateSubquery(PersistentClass model, IMapping mapping)
		{
			Dialect.Dialect dialect = Factory.Dialect;
			Settings settings = Factory.Settings;

			if (!model.HasSubclasses)
			{
				return model.Table.GetQualifiedName(dialect, settings.DefaultCatalogName, settings.DefaultSchemaName);
			}

			HashedSet<Column> columns = new HashedSet<Column>();
			foreach (Table table in model.SubclassTableClosureIterator)
			{
				if (!table.IsAbstractUnionTable)
				{
					foreach (Column column in table.ColumnIterator)
						columns.Add(column);
				}
			}

			StringBuilder buf = new StringBuilder().Append("( ");
			IEnumerable<PersistentClass> siter =
				new JoinedEnumerable<PersistentClass>(new SingletonEnumerable<PersistentClass>(model),
				                                      new SafetyEnumerable<PersistentClass>(model.SubclassIterator));

			foreach (PersistentClass clazz in siter)
			{
				Table table = clazz.Table;
				if (!table.IsAbstractUnionTable)
				{
					buf.Append("select ");
					foreach (Column col in columns)
					{
						if (!table.ContainsColumn(col))
						{
							SqlType sqlType = col.GetSqlTypeCode(mapping);
							buf.Append(dialect.GetSelectClauseNullString(sqlType)).Append(" as ");
						}
						buf.Append(col.Name);
						buf.Append(StringHelper.CommaSpace);
					}
					buf.Append(clazz.SubclassId).Append(" as clazz_");
					buf.Append(" from ").Append(table.GetQualifiedName(dialect, settings.DefaultCatalogName, settings.DefaultSchemaName));
					buf.Append(" union ");
					if (dialect.SupportsUnionAll)
						buf.Append("all ");
				}
			}

			if (buf.Length > 2)
			{
				//chop the last union (all)
				buf.Length -= (dialect.SupportsUnionAll ? 11 : 7); //" union " : "all "
			}

			return buf.Append(" )").ToString();
		}