PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NHibernate.Test/Legacy/FooBarTest.cs

https://github.com/cipatom/nhibernate-core
C# | 5700 lines | 4674 code | 797 blank | 229 comment | 161 complexity | 39836d5ba213088510b63b3bd3b728d3 MD5 | raw file
Possible License(s): Apache-2.0, CC-BY-SA-3.0, GPL-2.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Runtime.Serialization;
  9. using System.Runtime.Serialization.Formatters.Binary;
  10. using System.Text;
  11. using Iesi.Collections.Generic;
  12. using NHibernate.Connection;
  13. using NHibernate.Dialect;
  14. using NHibernate.DomainModel;
  15. using NHibernate.Criterion;
  16. using NHibernate.Proxy;
  17. using NHibernate.Type;
  18. using NHibernate.Util;
  19. using NUnit.Framework;
  20. namespace NHibernate.Test.Legacy
  21. {
  22. [TestFixture]
  23. public class FooBarTest : TestCase
  24. {
  25. // Equivalent of Java String.getBytes()
  26. private static byte[] GetBytes(string str)
  27. {
  28. return Encoding.Unicode.GetBytes(str);
  29. }
  30. protected override IList Mappings
  31. {
  32. get
  33. {
  34. return new string[]
  35. {
  36. "FooBar.hbm.xml",
  37. "Baz.hbm.xml",
  38. "Qux.hbm.xml",
  39. "Glarch.hbm.xml",
  40. "Fum.hbm.xml",
  41. "Fumm.hbm.xml",
  42. "Fo.hbm.xml",
  43. "One.hbm.xml",
  44. "Many.hbm.xml",
  45. "Immutable.hbm.xml",
  46. "Fee.hbm.xml",
  47. "Vetoer.hbm.xml",
  48. "Holder.hbm.xml",
  49. "Location.hbm.xml",
  50. "Stuff.hbm.xml",
  51. "Container.hbm.xml",
  52. "Simple.hbm.xml",
  53. "XY.hbm.xml"
  54. };
  55. }
  56. }
  57. [Test]
  58. public void CollectionVersioning()
  59. {
  60. using (ISession s = OpenSession())
  61. {
  62. One one = new One();
  63. one.Manies = new HashedSet<Many>();
  64. s.Save(one);
  65. s.Flush();
  66. Many many = new Many();
  67. many.One = one;
  68. one.Manies.Add(many);
  69. s.Save(many);
  70. s.Flush();
  71. // Versions are incremented compared to Hibernate because they start from 1
  72. // in NH.
  73. Assert.AreEqual(1, many.V);
  74. Assert.AreEqual(2, one.V);
  75. s.Delete(many);
  76. s.Delete(one);
  77. s.Flush();
  78. }
  79. }
  80. [Test]
  81. public void ForCertain()
  82. {
  83. Glarch g = new Glarch();
  84. Glarch g2 = new Glarch();
  85. IList strings = new ArrayList();
  86. strings.Add("foo");
  87. g2.Strings = strings;
  88. object gid, g2id;
  89. using (ISession s = OpenSession())
  90. {
  91. using (ITransaction t = s.BeginTransaction())
  92. {
  93. gid = s.Save(g);
  94. g2id = s.Save(g2);
  95. t.Commit();
  96. // Versions are initialized to 1 in NH.
  97. Assert.AreEqual(1, g.Version);
  98. Assert.AreEqual(1, g2.Version);
  99. }
  100. }
  101. using (ISession s = OpenSession())
  102. {
  103. using (ITransaction t = s.BeginTransaction())
  104. {
  105. g = (Glarch) s.Get(typeof(Glarch), gid);
  106. g2 = (Glarch) s.Get(typeof(Glarch), g2id);
  107. Assert.AreEqual(1, g2.Strings.Count);
  108. s.Delete(g);
  109. s.Delete(g2);
  110. t.Commit();
  111. }
  112. }
  113. }
  114. [Test]
  115. public void BagMultipleElements()
  116. {
  117. string bazCode;
  118. using (ISession s = OpenSession())
  119. {
  120. using (ITransaction t = s.BeginTransaction())
  121. {
  122. Baz baz = new Baz();
  123. baz.Bag = new ArrayList();
  124. baz.ByteBag = new ArrayList();
  125. s.Save(baz);
  126. baz.Bag.Add("foo");
  127. baz.Bag.Add("bar");
  128. baz.ByteBag.Add(GetBytes("foo"));
  129. baz.ByteBag.Add(GetBytes("bar"));
  130. t.Commit();
  131. bazCode = baz.Code;
  132. }
  133. }
  134. using (ISession s = OpenSession())
  135. {
  136. using (ITransaction t = s.BeginTransaction())
  137. {
  138. //put in cache
  139. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  140. Assert.AreEqual(2, baz.Bag.Count);
  141. Assert.AreEqual(2, baz.ByteBag.Count);
  142. t.Commit();
  143. }
  144. }
  145. using (ISession s = OpenSession())
  146. {
  147. using (ITransaction t = s.BeginTransaction())
  148. {
  149. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  150. Assert.AreEqual(2, baz.Bag.Count);
  151. Assert.AreEqual(2, baz.ByteBag.Count);
  152. baz.Bag.Remove("bar");
  153. baz.Bag.Add("foo");
  154. baz.ByteBag.Add(GetBytes("bar"));
  155. t.Commit();
  156. }
  157. }
  158. using (ISession s = OpenSession())
  159. {
  160. using (ITransaction t = s.BeginTransaction())
  161. {
  162. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  163. Assert.AreEqual(2, baz.Bag.Count);
  164. Assert.AreEqual(3, baz.ByteBag.Count);
  165. s.Delete(baz);
  166. t.Commit();
  167. }
  168. }
  169. }
  170. [Test]
  171. public void WierdSession()
  172. {
  173. object id;
  174. using (ISession s = OpenSession())
  175. {
  176. using (ITransaction t = s.BeginTransaction())
  177. {
  178. id = s.Save(new Foo());
  179. t.Commit();
  180. }
  181. }
  182. using (ISession s = OpenSession())
  183. {
  184. s.FlushMode = FlushMode.Never;
  185. using (ITransaction t = s.BeginTransaction())
  186. {
  187. Foo foo = (Foo) s.Get(typeof(Foo), id);
  188. t.Commit();
  189. }
  190. s.Disconnect();
  191. s.Reconnect();
  192. using (ITransaction t = s.BeginTransaction())
  193. {
  194. s.Flush();
  195. t.Commit();
  196. }
  197. }
  198. using (ISession s = OpenSession())
  199. {
  200. using (ITransaction t = s.BeginTransaction())
  201. {
  202. Foo foo = (Foo) s.Get(typeof(Foo), id);
  203. s.Delete(foo);
  204. t.Commit();
  205. }
  206. }
  207. }
  208. [Test]
  209. public void DereferenceLazyCollection()
  210. {
  211. string fooKey;
  212. string bazCode;
  213. using (ISession s = OpenSession())
  214. {
  215. Baz baz = new Baz();
  216. baz.FooSet = new HashedSet<FooProxy>();
  217. Foo foo = new Foo();
  218. baz.FooSet.Add(foo);
  219. s.Save(foo);
  220. s.Save(baz);
  221. foo.Bytes = GetBytes("foobar");
  222. s.Flush();
  223. fooKey = foo.Key;
  224. bazCode = baz.Code;
  225. }
  226. using (ISession s = OpenSession())
  227. {
  228. Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
  229. Assert.IsTrue(NHibernateUtil.IsInitialized(foo.Bytes));
  230. // H2.1 has 6 here, but we are using Unicode
  231. Assert.AreEqual(12, foo.Bytes.Length);
  232. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  233. Assert.AreEqual(1, baz.FooSet.Count);
  234. s.Flush();
  235. }
  236. sessions.EvictCollection("NHibernate.DomainModel.Baz.FooSet");
  237. using (ISession s = OpenSession())
  238. {
  239. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  240. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  241. baz.FooSet = null;
  242. s.Flush();
  243. }
  244. using (ISession s = OpenSession())
  245. {
  246. Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
  247. Assert.AreEqual(12, foo.Bytes.Length);
  248. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  249. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  250. Assert.AreEqual(0, baz.FooSet.Count);
  251. s.Delete(baz);
  252. s.Delete(foo);
  253. s.Flush();
  254. }
  255. }
  256. [Test]
  257. public void MoveLazyCollection()
  258. {
  259. string fooKey, bazCode, baz2Code;
  260. using (ISession s = OpenSession())
  261. {
  262. Baz baz = new Baz();
  263. Baz baz2 = new Baz();
  264. baz.FooSet = new HashedSet<FooProxy>();
  265. Foo foo = new Foo();
  266. baz.FooSet.Add(foo);
  267. s.Save(foo);
  268. s.Save(baz);
  269. s.Save(baz2);
  270. foo.Bytes = GetBytes("foobar");
  271. s.Flush();
  272. fooKey = foo.Key;
  273. bazCode = baz.Code;
  274. baz2Code = baz2.Code;
  275. }
  276. using (ISession s = OpenSession())
  277. {
  278. Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
  279. Assert.IsTrue(NHibernateUtil.IsInitialized(foo.Bytes));
  280. Assert.AreEqual(12, foo.Bytes.Length);
  281. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  282. Assert.AreEqual(1, baz.FooSet.Count);
  283. s.Flush();
  284. }
  285. sessions.EvictCollection("NHibernate.DomainModel.Baz.FooSet");
  286. using (ISession s = OpenSession())
  287. {
  288. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  289. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  290. Baz baz2 = (Baz) s.Get(typeof(Baz), baz2Code);
  291. baz2.FooSet = baz.FooSet;
  292. baz.FooSet = null;
  293. Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.FooSet));
  294. s.Flush();
  295. }
  296. using (ISession s = OpenSession())
  297. {
  298. Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
  299. Assert.AreEqual(12, foo.Bytes.Length);
  300. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  301. Baz baz2 = (Baz) s.Get(typeof(Baz), baz2Code);
  302. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  303. Assert.AreEqual(0, baz.FooSet.Count);
  304. Assert.IsTrue( NHibernateUtil.IsInitialized( baz2.FooSet ) ); //FooSet has batching enabled
  305. Assert.AreEqual(1, baz2.FooSet.Count);
  306. s.Delete(baz);
  307. s.Delete(baz2);
  308. s.Delete(foo);
  309. s.Flush();
  310. }
  311. }
  312. [Test]
  313. public void CriteriaCollection()
  314. {
  315. ISession s = OpenSession();
  316. Baz bb = (Baz) s.CreateCriteria(typeof(Baz)).UniqueResult();
  317. Baz baz = new Baz();
  318. s.Save(baz);
  319. s.Flush();
  320. s.Close();
  321. s = OpenSession();
  322. Baz b = (Baz) s.CreateCriteria(typeof(Baz)).UniqueResult();
  323. Assert.IsTrue(NHibernateUtil.IsInitialized(b.TopGlarchez));
  324. Assert.AreEqual(0, b.TopGlarchez.Count);
  325. s.Delete(b);
  326. s.Flush();
  327. s.CreateCriteria(typeof(Baz))
  328. .CreateCriteria("TopFoos")
  329. .Add(Expression.IsNotNull("id"))
  330. .List();
  331. s.CreateCriteria(typeof(Baz))
  332. .CreateCriteria("Foo")
  333. .CreateCriteria("Component.Glarch")
  334. .CreateCriteria("ProxySet")
  335. .Add(Expression.IsNotNull("id"))
  336. .List();
  337. s.Close();
  338. }
  339. private static bool IsEmpty(IEnumerable enumerable)
  340. {
  341. return !enumerable.GetEnumerator().MoveNext();
  342. }
  343. private static bool ContainsSingleObject(IEnumerable enumerable, object obj)
  344. {
  345. IEnumerator enumerator = enumerable.GetEnumerator();
  346. // Fail if no items
  347. if (!enumerator.MoveNext())
  348. {
  349. return false;
  350. }
  351. // Fail if item not equal
  352. if (!Equals(obj, enumerator.Current))
  353. {
  354. return false;
  355. }
  356. // Fail if more items
  357. if (enumerator.MoveNext())
  358. {
  359. return false;
  360. }
  361. // Succeed
  362. return true;
  363. }
  364. [Test]
  365. public void Query()
  366. {
  367. ISession s = OpenSession();
  368. ITransaction txn = s.BeginTransaction();
  369. Foo foo = new Foo();
  370. s.Save(foo);
  371. Foo foo2 = new Foo();
  372. s.Save(foo2);
  373. foo.TheFoo = foo2;
  374. IList list = s.CreateQuery("from Foo foo inner join fetch foo.TheFoo").List();
  375. Foo foof = (Foo) list[0];
  376. Assert.IsTrue(NHibernateUtil.IsInitialized(foof.TheFoo));
  377. list = s.CreateQuery("from Baz baz left outer join fetch baz.FooToGlarch").List();
  378. list = s.CreateQuery("select foo, bar from Foo foo left outer join foo.TheFoo bar where foo = ?")
  379. .SetEntity(0, foo).List();
  380. object[] row1 = (object[]) list[0];
  381. Assert.IsTrue(row1[0] == foo && row1[1] == foo2);
  382. s.CreateQuery("select foo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo = 'bar'").List();
  383. s.CreateQuery("select foo.TheFoo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo.TheFoo = 'bar'").List();
  384. s.CreateQuery("select foo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo.String = 'bar'").
  385. List();
  386. // if( !( dialect is Dialect.HSQLDialect ) )
  387. // {
  388. s.CreateQuery("select foo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo = foo.TheFoo.TheFoo").List();
  389. // }
  390. s.CreateQuery(
  391. "select foo.String from foo in class Foo where foo.TheFoo.TheFoo = 'bar' and foo.TheFoo.TheFoo.TheFoo = 'baz'").List
  392. ();
  393. s.CreateQuery(
  394. "select foo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo.String = 'a' and foo.TheFoo.String = 'b'").
  395. List();
  396. s.CreateQuery("from bar in class Bar, foo in elements(bar.Baz.FooArray)").List();
  397. if (Dialect is DB2Dialect)
  398. {
  399. s.CreateQuery("from foo in class Foo where lower( foo.TheFoo.String ) = 'foo'").List();
  400. s.CreateQuery("from foo in class Foo where lower( (foo.TheFoo.String || 'foo') || 'bar' ) = 'foo'").List();
  401. s.CreateQuery("from foo in class Foo where repeat( (foo.TheFoo.STring || 'foo') || 'bar', 2 ) = 'foo'").List();
  402. s.CreateQuery(
  403. "From foo in class Bar where foo.TheFoo.Integer is not null and repeat( (foo.TheFoo.String || 'foo') || 'bar', (5+5)/2 ) = 'foo'")
  404. .List();
  405. s.CreateQuery(
  406. "From foo in class Bar where foo.TheFoo.Integer is not null or repeat( (foo.TheFoo.String || 'foo') || 'bar', (5+5)/2 ) = 'foo'")
  407. .List();
  408. }
  409. if (Dialect is MsSql2000Dialect)
  410. {
  411. s.CreateQuery("select baz from Baz as baz join baz.FooArray foo group by baz order by sum(foo.Float)").Enumerable();
  412. }
  413. s.CreateQuery("from Foo as foo where foo.Component.Glarch.Name is not null").List();
  414. s.CreateQuery("from Foo as foo left outer join foo.Component.Glarch as glarch where glarch.Name = 'foo'").List();
  415. list = s.CreateQuery("from Foo").List();
  416. Assert.AreEqual(2, list.Count);
  417. Assert.IsTrue(list[0] is FooProxy);
  418. list = s.CreateQuery("from Foo foo left outer join foo.TheFoo").List();
  419. Assert.AreEqual(2, list.Count);
  420. Assert.IsTrue(((object[]) list[0])[0] is FooProxy);
  421. s.CreateQuery("From Foo, Bar").List();
  422. s.CreateQuery("from Baz baz left join baz.FooToGlarch, Bar bar join bar.TheFoo").List();
  423. s.CreateQuery("from Baz baz left join baz.FooToGlarch join baz.FooSet").List();
  424. s.CreateQuery("from Baz baz left join baz.FooToGlarch join fetch baz.FooSet foo left join fetch foo.TheFoo").List();
  425. list =
  426. s.CreateQuery(
  427. "from foo in class NHibernate.DomainModel.Foo where foo.String='osama bin laden' and foo.Boolean = true order by foo.String asc, foo.Component.Count desc")
  428. .List();
  429. Assert.AreEqual(0, list.Count, "empty query");
  430. IEnumerable enumerable =
  431. s.CreateQuery(
  432. "from foo in class NHibernate.DomainModel.Foo where foo.String='osama bin laden' order by foo.String asc, foo.Component.Count desc")
  433. .Enumerable();
  434. Assert.IsTrue(IsEmpty(enumerable), "empty enumerator");
  435. list = s.CreateQuery("select foo.TheFoo from foo in class NHibernate.DomainModel.Foo").List();
  436. Assert.AreEqual(1, list.Count, "query");
  437. Assert.AreEqual(foo.TheFoo, list[0], "returned object");
  438. foo.TheFoo.TheFoo = foo;
  439. foo.String = "fizard";
  440. if (Dialect.SupportsSubSelects && TestDialect.SupportsOperatorSome)
  441. {
  442. if (!(Dialect is FirebirdDialect))
  443. {
  444. if (IsClassicParser)
  445. {
  446. list =
  447. s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where ? = some foo.Component.ImportantDates.elements")
  448. .SetDateTime(0, DateTime.Today).List();
  449. }
  450. else
  451. {
  452. list =
  453. s.CreateQuery(
  454. "from foo in class NHibernate.DomainModel.Foo where ? = some elements(foo.Component.ImportantDates)").
  455. SetDateTime(0, DateTime.Today).List();
  456. }
  457. Assert.AreEqual(2, list.Count, "component query");
  458. }
  459. list =
  460. s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where size(foo.Component.ImportantDates) = 3").List();
  461. Assert.AreEqual(2, list.Count, "component query");
  462. list = s.CreateQuery("from foo in class Foo where 0 = size(foo.Component.ImportantDates)").List();
  463. Assert.AreEqual(0, list.Count, "component query");
  464. list = s.CreateQuery("from foo in class Foo where exists elements(foo.Component.ImportantDates)").List();
  465. Assert.AreEqual(2, list.Count, "component query");
  466. s.CreateQuery("from foo in class Foo where not exists (from bar in class Bar where bar.id = foo.id)").List();
  467. s.CreateQuery(
  468. "select foo.TheFoo from foo in class Foo where foo = some(select x from x in class Foo where x.Long > foo.TheFoo.Long)")
  469. .List();
  470. s.CreateQuery(
  471. "from foo in class Foo where foo = some(select x from x in class Foo where x.Long > foo.TheFoo.Long) and foo.TheFoo.String='baz'")
  472. .List();
  473. s.CreateQuery(
  474. "from foo in class Foo where foo.TheFoo.String='baz' and foo = some(select x from x in class Foo where x.Long>foo.TheFoo.Long)")
  475. .List();
  476. s.CreateQuery("from foo in class Foo where foo = some(select x from x in class Foo where x.Long > foo.TheFoo.Long)")
  477. .List();
  478. s.CreateQuery(
  479. "select foo.String, foo.Date, foo.TheFoo.String, foo.id from foo in class Foo, baz in class Baz where foo in elements(baz.FooArray) and foo.String like 'foo'")
  480. .Enumerable();
  481. }
  482. list = s.CreateQuery("from foo in class Foo where foo.Component.Count is null order by foo.Component.Count").List();
  483. Assert.AreEqual(0, list.Count, "component query");
  484. list = s.CreateQuery("from foo in class Foo where foo.Component.Name='foo'").List();
  485. Assert.AreEqual(2, list.Count, "component query");
  486. list =
  487. s.CreateQuery(
  488. "select distinct foo.Component.Name, foo.Component.Name from foo in class Foo where foo.Component.Name='foo'").List
  489. ();
  490. Assert.AreEqual(1, list.Count, "component query");
  491. list =
  492. s.CreateQuery("select distinct foo.Component.Name, foo.id from foo in class Foo where foo.Component.Name='foo'").
  493. List();
  494. Assert.AreEqual(2, list.Count, "component query");
  495. list = s.CreateQuery("select foo.TheFoo from foo in class Foo").List();
  496. Assert.AreEqual(2, list.Count, "query");
  497. list = s.CreateQuery("from foo in class Foo where foo.id=?").SetString(0, foo.Key).List();
  498. Assert.AreEqual(1, list.Count, "id query");
  499. list = s.CreateQuery("from foo in class Foo where foo.Key=?").SetString(0, foo.Key).List();
  500. Assert.AreEqual(1, list.Count, "named id query");
  501. Assert.AreSame(foo, list[0], "id query");
  502. list = s.CreateQuery("select foo.TheFoo from foo in class Foo where foo.String='fizard'").List();
  503. Assert.AreEqual(1, list.Count, "query");
  504. Assert.AreSame(foo.TheFoo, list[0], "returned object");
  505. list = s.CreateQuery("from foo in class Foo where foo.Component.Subcomponent.Name='bar'").List();
  506. Assert.AreEqual(2, list.Count, "components of components");
  507. list = s.CreateQuery("select foo.TheFoo from foo in class Foo where foo.TheFoo.id=?")
  508. .SetString(0, foo.TheFoo.Key).List();
  509. Assert.AreEqual(1, list.Count, "by id query");
  510. Assert.AreSame(foo.TheFoo, list[0], "by id returned object");
  511. s.CreateQuery("from foo in class Foo where foo.TheFoo = ?").SetEntity(0, foo.TheFoo).List();
  512. Assert.IsTrue(
  513. IsEmpty(s.CreateQuery("from bar in class Bar where bar.String='a string' or bar.String='a string'").Enumerable()
  514. ));
  515. if (IsClassicParser)
  516. {
  517. enumerable = s.CreateQuery(
  518. "select foo.Component.Name, foo.Component.ImportantDates.elements from foo in class Foo where foo.TheFoo.id=?"
  519. ).SetString(0, foo.TheFoo.Key).Enumerable();
  520. }
  521. else
  522. {
  523. enumerable =
  524. s.CreateQuery(
  525. "select foo.Component.Name, elements(foo.Component.ImportantDates) from foo in class Foo where foo.TheFoo.id=?").
  526. SetString(0, foo.TheFoo.Key).Enumerable();
  527. }
  528. int i = 0;
  529. foreach (object[] row in enumerable)
  530. {
  531. i++;
  532. Assert.IsTrue(row[0] is String);
  533. Assert.IsTrue(row[1] == null || row[1] is DateTime);
  534. }
  535. Assert.AreEqual(3, i); //WAS: 4
  536. if (IsClassicParser)
  537. {
  538. enumerable = s.CreateQuery(
  539. "select max(foo.Component.ImportantDates.elements) from foo in class Foo group by foo.id"
  540. ).Enumerable();
  541. }
  542. else
  543. {
  544. enumerable =
  545. s.CreateQuery("select max(elements(foo.Component.ImportantDates)) from foo in class Foo group by foo.id").
  546. Enumerable();
  547. }
  548. IEnumerator enumerator = enumerable.GetEnumerator();
  549. Assert.IsTrue(enumerator.MoveNext());
  550. Assert.IsTrue(enumerator.Current is DateTime);
  551. list = s.CreateQuery(
  552. "select foo.TheFoo.TheFoo.TheFoo from foo in class Foo, foo2 in class Foo where"
  553. + " foo = foo2.TheFoo and not not ( not foo.String='fizard' )"
  554. + " and foo2.String between 'a' and (foo.TheFoo.String)"
  555. + (Dialect is SQLiteDialect
  556. ? " and ( foo2.String in ( 'fiz', 'blah') or 1=1 )"
  557. : " and ( foo2.String in ( 'fiz', 'blah', foo.TheFoo.String, foo.String, foo2.String ) )")
  558. ).List();
  559. Assert.AreEqual(1, list.Count, "complex query");
  560. Assert.AreSame(foo, list[0], "returned object");
  561. foo.String = "from BoogieDown -tinsel town =!@#$^&*())";
  562. list = s.CreateQuery("from foo in class Foo where foo.String='from BoogieDown -tinsel town =!@#$^&*())'").List();
  563. Assert.AreEqual(1, list.Count, "single quotes");
  564. list = s.CreateQuery("from foo in class Foo where not foo.String='foo''bar'").List();
  565. Assert.AreEqual(2, list.Count, "single quotes");
  566. list = s.CreateQuery("from foo in class Foo where foo.Component.Glarch.Next is null").List();
  567. Assert.AreEqual(2, list.Count, "query association in component");
  568. Bar bar = new Bar();
  569. Baz baz = new Baz();
  570. baz.SetDefaults();
  571. bar.Baz = baz;
  572. baz.ManyToAny = new ArrayList();
  573. baz.ManyToAny.Add(bar);
  574. baz.ManyToAny.Add(foo);
  575. s.Save(bar);
  576. s.Save(baz);
  577. list =
  578. s.CreateQuery(" from bar in class Bar where bar.Baz.Count=667 and bar.Baz.Count!=123 and not bar.Baz.Name='1-E-1'").
  579. List();
  580. Assert.AreEqual(1, list.Count, "query many-to-one");
  581. list = s.CreateQuery(" from i in class Bar where i.Baz.Name='Bazza'").List();
  582. Assert.AreEqual(1, list.Count, "query many-to-one");
  583. if (DialectSupportsCountDistinct)
  584. {
  585. enumerable = s.CreateQuery("select count(distinct foo.TheFoo) from foo in class Foo").Enumerable();
  586. Assert.IsTrue(ContainsSingleObject(enumerable, (long) 2), "count"); // changed to Int64 (HQLFunction H3.2)
  587. }
  588. enumerable = s.CreateQuery("select count(foo.TheFoo.Boolean) from foo in class Foo").Enumerable();
  589. Assert.IsTrue(ContainsSingleObject(enumerable, (long) 2), "count"); // changed to Int64 (HQLFunction H3.2)
  590. enumerable = s.CreateQuery("select count(*), foo.Int from foo in class Foo group by foo.Int").Enumerable();
  591. enumerator = enumerable.GetEnumerator();
  592. Assert.IsTrue(enumerator.MoveNext());
  593. Assert.AreEqual(3L, (long) ((object[]) enumerator.Current)[0]);
  594. Assert.IsFalse(enumerator.MoveNext());
  595. enumerable = s.CreateQuery("select sum(foo.TheFoo.Int) from foo in class Foo").Enumerable();
  596. Assert.IsTrue(ContainsSingleObject(enumerable, (long) 4), "sum"); // changed to Int64 (HQLFunction H3.2)
  597. enumerable = s.CreateQuery("select count(foo) from foo in class Foo where foo.id=?")
  598. .SetString(0, foo.Key).Enumerable();
  599. Assert.IsTrue(ContainsSingleObject(enumerable, (long) 1), "id query count");
  600. list = s.CreateQuery("from foo in class Foo where foo.Boolean = ?").SetBoolean(0, true).List();
  601. list = s.CreateQuery("select new Foo(fo.X) from Fo fo").List();
  602. list = s.CreateQuery("select new Foo(fo.Integer) from Foo fo").List();
  603. list = s.CreateQuery("select new Foo(fo.X) from Foo fo")
  604. .SetCacheable(true)
  605. .List();
  606. Assert.IsTrue(list.Count == 3);
  607. list = s.CreateQuery("select new Foo(fo.X) from Foo fo")
  608. .SetCacheable(true)
  609. .List();
  610. Assert.IsTrue(list.Count == 3);
  611. enumerable = s.CreateQuery("select new Foo(fo.X) from Foo fo").Enumerable();
  612. enumerator = enumerable.GetEnumerator();
  613. Assert.IsTrue(enumerator.MoveNext(), "projection iterate (results)");
  614. Assert.IsTrue(typeof(Foo).IsAssignableFrom(enumerator.Current.GetType()),
  615. "projection iterate (return check)");
  616. // TODO: ScrollableResults not implemented
  617. //ScrollableResults sr = s.CreateQuery("select new Foo(fo.x) from Foo fo").Scroll();
  618. //Assert.IsTrue( "projection scroll (results)", sr.next() );
  619. //Assert.IsTrue( "projection scroll (return check)", typeof(Foo).isAssignableFrom( sr.get(0).getClass() ) );
  620. list = s.CreateQuery("select foo.Long, foo.Component.Name, foo, foo.TheFoo from foo in class Foo").List();
  621. Assert.IsTrue(list.Count > 0);
  622. foreach (object[] row in list)
  623. {
  624. Assert.IsTrue(row[0] is long);
  625. Assert.IsTrue(row[1] is string);
  626. Assert.IsTrue(row[2] is Foo);
  627. Assert.IsTrue(row[3] is Foo);
  628. }
  629. if (DialectSupportsCountDistinct)
  630. {
  631. list =
  632. s.CreateQuery("select avg(foo.Float), max(foo.Component.Name), count(distinct foo.id) from foo in class Foo").List();
  633. Assert.IsTrue(list.Count > 0);
  634. foreach (object[] row in list)
  635. {
  636. Assert.IsTrue(row[0] is double); // changed from float to double (HQLFunction H3.2)
  637. Assert.IsTrue(row[1] is string);
  638. Assert.IsTrue(row[2] is long); // changed from int to long (HQLFunction H3.2)
  639. }
  640. }
  641. list = s.CreateQuery("select foo.Long, foo.Component, foo, foo.TheFoo from foo in class Foo").List();
  642. Assert.IsTrue(list.Count > 0);
  643. foreach (object[] row in list)
  644. {
  645. Assert.IsTrue(row[0] is long);
  646. Assert.IsTrue(row[1] is FooComponent);
  647. Assert.IsTrue(row[2] is Foo);
  648. Assert.IsTrue(row[3] is Foo);
  649. }
  650. s.Save(new Holder("ice T"));
  651. s.Save(new Holder("ice cube"));
  652. Assert.AreEqual(15, s.CreateQuery("from o in class System.Object").List().Count);
  653. Assert.AreEqual(7, s.CreateQuery("from n in class INamed").List().Count);
  654. Assert.IsTrue(s.CreateQuery("from n in class INamed where n.Name is not null").List().Count == 4);
  655. foreach (INamed named in s.CreateQuery("from n in class INamed").Enumerable())
  656. {
  657. Assert.IsNotNull(named);
  658. }
  659. s.Save(new Holder("bar"));
  660. enumerable = s.CreateQuery("from n0 in class INamed, n1 in class INamed where n0.Name = n1.Name").Enumerable();
  661. int cnt = 0;
  662. foreach (object[] row in enumerable)
  663. {
  664. if (row[0] != row[1])
  665. {
  666. cnt++;
  667. }
  668. }
  669. //if ( !(dialect is Dialect.HSQLDialect) )
  670. //{
  671. Assert.IsTrue(cnt == 2);
  672. Assert.IsTrue(s.CreateQuery("from n0 in class INamed, n1 in class INamed where n0.Name = n1.Name").List().Count == 7);
  673. //}
  674. IQuery qu = s.CreateQuery("from n in class INamed where n.Name = :name");
  675. object temp = qu.ReturnTypes;
  676. temp = qu.NamedParameters;
  677. int c = 0;
  678. foreach (object obj in s.CreateQuery("from o in class System.Object").Enumerable())
  679. {
  680. c++;
  681. }
  682. Assert.IsTrue(c == 16);
  683. s.CreateQuery("select baz.Code, min(baz.Count) from baz in class Baz group by baz.Code").Enumerable();
  684. Assert.IsTrue(
  685. IsEmpty(
  686. s.CreateQuery(
  687. "selecT baz from baz in class Baz where baz.StringDateMap['foo'] is not null or baz.StringDateMap['bar'] = ?")
  688. .SetDateTime(0, DateTime.Today).Enumerable()));
  689. list = s.CreateQuery("select baz from baz in class Baz where baz.StringDateMap['now'] is not null").List();
  690. Assert.AreEqual(1, list.Count);
  691. list =
  692. s.CreateQuery("select baz from baz in class Baz where baz.StringDateMap[:now] is not null").SetString("now", "now").
  693. List();
  694. Assert.AreEqual(1, list.Count);
  695. list =
  696. s.CreateQuery(
  697. "select baz from baz in class Baz where baz.StringDateMap['now'] is not null and baz.StringDateMap['big bang'] < baz.StringDateMap['now']")
  698. .List();
  699. Assert.AreEqual(1, list.Count);
  700. list = s.CreateQuery("select index(date) from Baz baz join baz.StringDateMap date").List();
  701. Console.WriteLine(list);
  702. Assert.AreEqual(3, list.Count);
  703. s.CreateQuery(
  704. "from foo in class Foo where foo.Integer not between 1 and 5 and foo.String not in ('cde', 'abc') and foo.String is not null and foo.Integer<=3")
  705. .List();
  706. s.CreateQuery("from Baz baz inner join baz.CollectionComponent.Nested.Foos foo where foo.String is null").List();
  707. if (Dialect.SupportsSubSelects)
  708. {
  709. s.CreateQuery("from Baz baz inner join baz.FooSet where '1' in (from baz.FooSet foo where foo.String is not null)").
  710. List();
  711. s.CreateQuery(
  712. "from Baz baz where 'a' in elements(baz.CollectionComponent.Nested.Foos) and 1.0 in elements(baz.CollectionComponent.Nested.Floats)")
  713. .List();
  714. if (IsClassicParser)
  715. {
  716. s.CreateQuery(
  717. "from Baz baz where 'b' in baz.CollectionComponent.Nested.Foos.elements and 1.0 in baz.CollectionComponent.Nested.Floats.elements")
  718. .List();
  719. }
  720. else
  721. {
  722. s.CreateQuery(
  723. "from Baz baz where 'b' in elements(baz.CollectionComponent.Nested.Foos) and 1.0 in elements(baz.CollectionComponent.Nested.Floats)")
  724. .List();
  725. }
  726. }
  727. s.CreateQuery("from Foo foo join foo.TheFoo where foo.TheFoo in ('1','2','3')").List();
  728. //if ( !(dialect is Dialect.HSQLDialect) )
  729. s.CreateQuery("from Foo foo left join foo.TheFoo where foo.TheFoo in ('1','2','3')").List();
  730. s.CreateQuery("select foo.TheFoo from Foo foo where foo.TheFoo in ('1','2','3')").List();
  731. s.CreateQuery("select foo.TheFoo.String from Foo foo where foo.TheFoo in ('1','2','3')").List();
  732. s.CreateQuery("select foo.TheFoo.String from Foo foo where foo.TheFoo.String in ('1','2','3')").List();
  733. s.CreateQuery("select foo.TheFoo.Long from Foo foo where foo.TheFoo.String in ('1','2','3')").List();
  734. s.CreateQuery("select count(*) from Foo foo where foo.TheFoo.String in ('1','2','3') or foo.TheFoo.Long in (1,2,3)").
  735. List();
  736. s.CreateQuery("select count(*) from Foo foo where foo.TheFoo.String in ('1','2','3') group by foo.TheFoo.Long").List();
  737. s.CreateQuery("from Foo foo1 left join foo1.TheFoo foo2 left join foo2.TheFoo where foo1.String is not null").List();
  738. s.CreateQuery("from Foo foo1 left join foo1.TheFoo.TheFoo where foo1.String is not null").List();
  739. s.CreateQuery(
  740. "from Foo foo1 left join foo1.TheFoo foo2 left join foo1.TheFoo.TheFoo foo3 where foo1.String is not null").List();
  741. s.CreateQuery("select foo.Formula from Foo foo where foo.Formula > 0").List();
  742. int len = s.CreateQuery("from Foo as foo join foo.TheFoo as foo2 where foo2.id >'a' or foo2.id <'a'").List().Count;
  743. Assert.IsTrue(len == 2);
  744. s.Delete("from Holder");
  745. txn.Commit();
  746. s.Close();
  747. s = OpenSession();
  748. txn = s.BeginTransaction();
  749. baz = (Baz) s.CreateQuery("from Baz baz left outer join fetch baz.ManyToAny").UniqueResult();
  750. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.ManyToAny));
  751. Assert.IsTrue(baz.ManyToAny.Count == 2);
  752. BarProxy barp = (BarProxy) baz.ManyToAny[0];
  753. s.CreateQuery("from Baz baz join baz.ManyToAny").List();
  754. Assert.IsTrue(s.CreateQuery("select baz from Baz baz join baz.ManyToAny a where index(a) = 0").List().Count == 1);
  755. FooProxy foop = (FooProxy) s.Get(typeof(Foo), foo.Key);
  756. Assert.IsTrue(foop == baz.ManyToAny[1]);
  757. barp.Baz = baz;
  758. Assert.IsTrue(s.CreateQuery("select bar from Bar bar where bar.Baz.StringDateMap['now'] is not null").List().Count ==
  759. 1);
  760. Assert.IsTrue(
  761. s.CreateQuery(
  762. "select bar from Bar bar join bar.Baz b where b.StringDateMap['big bang'] < b.StringDateMap['now'] and b.StringDateMap['now'] is not null")
  763. .List().Count == 1);
  764. Assert.IsTrue(
  765. s.CreateQuery(
  766. "select bar from Bar bar where bar.Baz.StringDateMap['big bang'] < bar.Baz.StringDateMap['now'] and bar.Baz.StringDateMap['now'] is not null")
  767. .List().Count == 1);
  768. list = s.CreateQuery("select foo.String, foo.Component, foo.id from Bar foo").List();
  769. Assert.IsTrue(((FooComponent) ((object[]) list[0])[1]).Name == "foo");
  770. list = s.CreateQuery("select elements(baz.Components) from Baz baz").List();
  771. Assert.IsTrue(list.Count == 2);
  772. list = s.CreateQuery("select bc.Name from Baz baz join baz.Components bc").List();
  773. Assert.IsTrue(list.Count == 2);
  774. //list = s.CreateQuery("select bc from Baz baz join baz.components bc").List();
  775. s.CreateQuery("from Foo foo where foo.Integer < 10 order by foo.String").SetMaxResults(12).List();
  776. s.Delete(barp);
  777. s.Delete(baz);
  778. s.Delete(foop.TheFoo);
  779. s.Delete(foop);
  780. txn.Commit();
  781. s.Close();
  782. }
  783. [Test]
  784. public void CascadeDeleteDetached()
  785. {
  786. Baz baz;
  787. using (ISession s = OpenSession())
  788. {
  789. baz = new Baz();
  790. IList list = new ArrayList();
  791. list.Add(new Fee());
  792. baz.Fees = list;
  793. s.Save(baz);
  794. s.Flush();
  795. }
  796. using (ISession s = OpenSession())
  797. {
  798. baz = (Baz) s.Get(typeof(Baz), baz.Code);
  799. }
  800. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.Fees));
  801. using (ISession s = OpenSession())
  802. {
  803. s.Delete(baz);
  804. s.Flush();
  805. Assert.IsFalse(s.CreateQuery("from Fee").Enumerable().GetEnumerator().MoveNext());
  806. }
  807. using (ISession s = OpenSession())
  808. {
  809. baz = new Baz();
  810. IList list = new ArrayList();
  811. list.Add(new Fee());
  812. list.Add(new Fee());
  813. baz.Fees = list;
  814. s.Save(baz);
  815. s.Flush();
  816. }
  817. using (ISession s = OpenSession())
  818. {
  819. baz = (Baz) s.Get(typeof(Baz), baz.Code);
  820. NHibernateUtil.Initialize(baz.Fees);
  821. }
  822. Assert.AreEqual(2, baz.Fees.Count);
  823. using (ISession s = OpenSession())
  824. {
  825. s.Delete(baz);
  826. s.Flush();
  827. Assert.IsTrue(IsEmpty(s.CreateQuery("from Fee").Enumerable()));
  828. }
  829. }
  830. [Test]
  831. public void ForeignKeys()
  832. {
  833. Baz baz;
  834. using (ISession s = OpenSession())
  835. {
  836. baz = new Baz();
  837. Foo foo = new Foo();
  838. IList bag = new ArrayList();
  839. bag.Add(foo);
  840. baz.IdFooBag = bag;
  841. baz.Foo = foo;
  842. s.Save(baz);
  843. s.Flush();
  844. }
  845. using (ISession s = OpenSession())
  846. {
  847. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  848. s.Delete(baz);
  849. s.Flush();
  850. }
  851. }
  852. [Test]
  853. public void NonlazyCollections()
  854. {
  855. object glarchId;
  856. using (ISession s = OpenSession())
  857. {
  858. Glarch glarch1 = new Glarch();
  859. glarch1.ProxySet = new OrderedSet<GlarchProxy>();
  860. Glarch glarch2 = new Glarch();
  861. glarch1.ProxySet.Add(glarch1);
  862. s.Save(glarch2);
  863. glarchId = s.Save(glarch1);
  864. s.Flush();
  865. }
  866. Glarch loadedGlarch;
  867. using (ISession s = OpenSession())
  868. {
  869. loadedGlarch = (Glarch) s.Get(typeof(Glarch), glarchId);
  870. Assert.IsTrue(NHibernateUtil.IsInitialized(loadedGlarch.ProxySet));
  871. }
  872. // ProxySet is a non-lazy collection, so this should work outside
  873. // a session.
  874. Assert.AreEqual(1, loadedGlarch.ProxySet.Count);
  875. using (ISession s = OpenSession())
  876. {
  877. s.Delete("from Glarch");
  878. s.Flush();
  879. }
  880. }
  881. [Test]
  882. public void ReuseDeletedCollection()
  883. {
  884. Baz baz, baz2;
  885. using (ISession s = OpenSession())
  886. {
  887. baz = new Baz();
  888. baz.SetDefaults();
  889. s.Save(baz);
  890. s.Flush();
  891. s.Delete(baz);
  892. baz2 = new Baz();
  893. baz2.StringArray = new string[] {"x-y-z"};
  894. s.Save(baz2);
  895. s.Flush();
  896. }
  897. baz2.StringSet = baz.StringSet;
  898. baz2.StringArray = baz.StringArray;
  899. baz2.FooArray = baz.FooArray;
  900. using (ISession s = OpenSession())
  901. {
  902. s.Update(baz2);
  903. s.Flush();
  904. }
  905. using (ISession s = OpenSession())
  906. {
  907. baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
  908. Assert.AreEqual(3, baz2.StringArray.Length);
  909. Assert.AreEqual(3, baz2.StringSet.Count);
  910. s.Delete(baz2);
  911. s.Flush();
  912. }
  913. }
  914. [Test]
  915. public void PropertyRef()
  916. {
  917. object qid;
  918. object hid;
  919. using (ISession s = OpenSession())
  920. {
  921. Holder h = new Holder();
  922. h.Name = "foo";
  923. Holder h2 = new Holder();
  924. h2.Name = "bar";
  925. h.OtherHolder = h2;
  926. hid = s.Save(h);
  927. Qux q = new Qux();
  928. q.Holder = h2;
  929. qid = s.Save(q);
  930. s.Flush();
  931. }
  932. using (ISession s = OpenSession())
  933. {
  934. Holder h = (Holder) s.Load(typeof(Holder), hid);
  935. Assert.AreEqual(h.Name, "foo");
  936. Assert.AreEqual(h.OtherHolder.Name, "bar");
  937. object[] res =
  938. (object[]) s.CreateQuery("from Holder h join h.OtherHolder oh where h.OtherHolder.Name = 'bar'").List()[0];
  939. Assert.AreSame(h, res[0]);
  940. Qux q = (Qux) s.Get(typeof(Qux), qid);
  941. Assert.AreSame(q.Holder, h.OtherHolder);
  942. s.Delete(h);
  943. s.Delete(q);
  944. s.Flush();
  945. }
  946. }
  947. [Test]
  948. public void QueryCollectionOfValues()
  949. {
  950. object gid;
  951. using (ISession s = OpenSession())
  952. {
  953. Baz baz = new Baz();
  954. baz.SetDefaults();
  955. s.Save(baz);
  956. Glarch g = new Glarch();
  957. gid = s.Save(g);
  958. if (Dialect.SupportsSubSelects)
  959. {
  960. s.CreateFilter(baz.FooArray, "where size(this.Bytes) > 0").List();
  961. s.CreateFilter(baz.FooArray, "where 0 in elements(this.Bytes)").List();
  962. }
  963. s.Flush();
  964. }
  965. using (ISession s = OpenSession())
  966. {
  967. //s.CreateQuery("from Baz baz where baz.FooSet.String = 'foo'").List();
  968. //s.CreateQuery("from Baz baz where baz.FooArray.String = 'foo'").List();
  969. //s.CreateQuery("from Baz baz where baz.FooSet.foo.String = 'foo'").List();
  970. //s.CreateQuery("from Baz baz join baz.FooSet.Foo foo where foo.String = 'foo'").List();
  971. s.CreateQuery("from Baz baz join baz.FooSet foo join foo.TheFoo.TheFoo foo2 where foo2.String = 'foo'").List();
  972. s.CreateQuery("from Baz baz join baz.FooArray foo join foo.TheFoo.TheFoo foo2 where foo2.String = 'foo'").List();
  973. s.CreateQuery("from Baz baz join baz.StringDateMap date where index(date) = 'foo'").List();
  974. s.CreateQuery("from Baz baz join baz.TopGlarchez g where index(g) = 'A'").List();
  975. s.CreateQuery("select index(g) from Baz baz join baz.TopGlarchez g").List();
  976. Assert.AreEqual(3, s.CreateQuery("from Baz baz left join baz.StringSet").List().Count);
  977. Baz baz = (Baz) s.CreateQuery("from Baz baz join baz.StringSet str where str='foo'").List()[0];
  978. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.StringSet));
  979. baz = (Baz) s.CreateQuery("from Baz baz left join fetch baz.StringSet").List()[0];
  980. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.StringSet));
  981. Assert.AreEqual(1, s.CreateQuery("from Baz baz join baz.StringSet string where string='foo'").List().Count);
  982. Assert.AreEqual(1, s.CreateQuery("from Baz baz inner join baz.Components comp where comp.Name='foo'").List().Count);
  983. //IList bss = s.CreateQuery("select baz, ss from Baz baz inner join baz.StringSet ss").List();
  984. s.CreateQuery("from Glarch g inner join g.FooComponents comp where comp.Fee is not null").List();
  985. s.CreateQuery("from Glarch g inner join g.FooComponents comp join comp.Fee fee where fee.Count > 0").List();
  986. s.CreateQuery("from Glarch g inner join g.FooComponents comp where comp.Fee.Count is not null").List();
  987. s.Delete(baz);
  988. //s.delete("from Glarch g");
  989. s.Delete(s.Get(typeof(Glarch), gid));
  990. s.Flush();
  991. }
  992. }
  993. [Test]
  994. public void BatchLoad()
  995. {
  996. Baz baz, baz2, baz3;
  997. using (ISession s = OpenSession())
  998. {
  999. baz = new Baz();
  1000. var stringSet = new SortedSet<string> { "foo", "bar" };
  1001. var fooSet = new HashedSet<FooProxy>();
  1002. for (int i = 0; i < 3; i++)
  1003. {
  1004. Foo foo = new Foo();
  1005. s.Save(foo);
  1006. fooSet.Add(foo);
  1007. }
  1008. baz.FooSet = fooSet;
  1009. baz.StringSet = stringSet;
  1010. s.Save(baz);
  1011. baz2 = new Baz();
  1012. fooSet = new HashedSet<FooProxy>();
  1013. for (int i = 0; i < 2; i++)
  1014. {
  1015. Foo foo = new Foo();
  1016. s.Save(foo);
  1017. fooSet.Add(foo);
  1018. }
  1019. baz2.FooSet = fooSet;
  1020. s.Save(baz2);
  1021. baz3 = new Baz();
  1022. stringSet = new SortedSet<string>();
  1023. stringSet.Add("foo");
  1024. stringSet.Add("baz");
  1025. baz3.StringSet = stringSet;
  1026. s.Save(baz3);
  1027. s.Flush();
  1028. }
  1029. using (ISession s = OpenSession())
  1030. {
  1031. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  1032. baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
  1033. baz3 = (Baz) s.Load(typeof(Baz), baz3.Code);
  1034. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  1035. Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.FooSet));
  1036. Assert.IsFalse(NHibernateUtil.IsInitialized(baz3.FooSet));
  1037. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.StringSet));
  1038. Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.StringSet));
  1039. Assert.IsFalse(NHibernateUtil.IsInitialized(baz3.StringSet));
  1040. Assert.AreEqual(3, baz.FooSet.Count);
  1041. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.FooSet));
  1042. Assert.IsTrue(NHibernateUtil.IsInitialized(baz2.FooSet));
  1043. Assert.IsTrue(NHibernateUtil.IsInitialized(baz3.FooSet));
  1044. Assert.AreEqual(2, baz2.FooSet.Count);
  1045. Assert.IsTrue(baz3.StringSet.Contains("baz"));
  1046. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.StringSet));
  1047. Assert.IsTrue(NHibernateUtil.IsInitialized(baz2.StringSet));
  1048. Assert.IsTrue(NHibernateUtil.IsInitialized(baz3.StringSet));
  1049. Assert.AreEqual(2, baz.StringSet.Count);
  1050. Assert.AreEqual(0, baz2.StringSet.Count);
  1051. s.Delete(baz);
  1052. s.Delete(baz2);
  1053. s.Delete(baz3);
  1054. IEnumerable en = new JoinedEnumerable(
  1055. new IEnumerable[] {baz.FooSet, baz2.FooSet});
  1056. foreach (object obj in en)
  1057. {
  1058. s.Delete(obj);
  1059. }
  1060. s.Flush();
  1061. }
  1062. }
  1063. [Test]
  1064. public void FetchInitializedCollection()
  1065. {
  1066. ISession s = OpenSession();
  1067. Baz baz = new Baz();
  1068. IList fooBag = new ArrayList();
  1069. fooBag.Add(new Foo());
  1070. fooBag.Add(new Foo());
  1071. baz.FooBag = fooBag;
  1072. s.Save(baz);
  1073. s.Flush();
  1074. fooBag = baz.FooBag;
  1075. s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
  1076. Assert.IsTrue(NHibernateUtil.IsInitialized(fooBag));
  1077. s.Close();
  1078. s = OpenSession();
  1079. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  1080. Object bag = baz.FooBag;
  1081. Assert.IsFalse(NHibernateUtil.IsInitialized(bag));
  1082. s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
  1083. Assert.IsTrue(bag == baz.FooBag);
  1084. Assert.IsTrue(baz.FooBag.Count == 2);
  1085. s.Delete(baz);
  1086. s.Flush();
  1087. s.Close();
  1088. }
  1089. [Test]
  1090. public void LateCollectionAdd()
  1091. {
  1092. object id;
  1093. using (ISession s = OpenSession())
  1094. {
  1095. Baz baz = new Baz();
  1096. IList l = new ArrayList();
  1097. baz.StringList = l;
  1098. l.Add("foo");
  1099. id = s.Save(baz);
  1100. l.Add("bar");
  1101. s.Flush();
  1102. l.Add("baz");
  1103. s.Flush();
  1104. }
  1105. using (ISession s = OpenSession())
  1106. {
  1107. Baz baz = (Baz) s.Load(typeof(Baz), id);
  1108. Assert.AreEqual(3, baz.StringList.Count);
  1109. Assert.IsTrue(baz.StringList.Contains("foo"));
  1110. Assert.IsTrue(baz.StringList.Contains("bar"));
  1111. Assert.IsTrue(baz.StringList.Contains("baz"));
  1112. s.Delete(baz);
  1113. s.Flush();
  1114. }
  1115. }
  1116. [Test]
  1117. public void Update()
  1118. {
  1119. ISession s = OpenSession();
  1120. Foo foo = new Foo();
  1121. s.Save(foo);
  1122. s.Flush();
  1123. s.Close();
  1124. s = OpenSession();
  1125. FooProxy foo2 = (FooProxy) s.Load(typeof(Foo), foo.Key);
  1126. foo2.String = "dirty";
  1127. foo2.Boolean = false;
  1128. foo2.Bytes = new byte[] {1, 2, 3};
  1129. foo2.Date = DateTime.Today;
  1130. foo2.Short = 69;
  1131. s.Flush();
  1132. s.Close();
  1133. s = OpenSession();
  1134. Foo foo3 = new Foo();
  1135. s.Load(foo3, foo.Key);
  1136. Assert.IsTrue(foo2.EqualsFoo(foo3), "update");
  1137. s.Delete(foo3);
  1138. s.Flush();
  1139. s.Close();
  1140. }
  1141. [Test]
  1142. public void ListRemove()
  1143. {
  1144. using (ISession s = OpenSession())
  1145. {
  1146. Baz b = new Baz();
  1147. IList stringList = new ArrayList();
  1148. IList feeList = new ArrayList();
  1149. b.Fees = feeList;
  1150. b.StringList = stringList;
  1151. feeList.Add(new Fee());
  1152. feeList.Add(new Fee());
  1153. feeList.Add(new Fee());
  1154. feeList.Add(new Fee());
  1155. stringList.Add("foo");
  1156. stringList.Add("bar");
  1157. stringList.Add("baz");
  1158. stringList.Add("glarch");
  1159. s.Save(b);
  1160. s.Flush();
  1161. stringList.RemoveAt(1);
  1162. feeList.RemoveAt(1);
  1163. s.Flush();
  1164. s.Evict(b);
  1165. s.Refresh(b);
  1166. Assert.AreEqual(3, b.Fees.Count);
  1167. stringList = b.StringList;
  1168. Assert.AreEqual(3, stringList.Count);
  1169. Assert.AreEqual("baz", stringList[1]);
  1170. Assert.AreEqual("foo", stringList[0]);
  1171. s.Delete(b);
  1172. s.Delete("from Fee");
  1173. s.Flush();
  1174. }
  1175. }
  1176. [Test]
  1177. public void FetchInitializedCollectionDupe()
  1178. {
  1179. string bazCode;
  1180. using (ISession s = OpenSession())
  1181. {
  1182. Baz baz = new Baz();
  1183. IList fooBag = new ArrayList();
  1184. fooBag.Add(new Foo());
  1185. fooBag.Add(new Foo());
  1186. baz.FooBag = fooBag;
  1187. s.Save(baz);
  1188. s.Flush();
  1189. fooBag = baz.FooBag;
  1190. s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
  1191. Assert.IsTrue(NHibernateUtil.IsInitialized(fooBag));
  1192. Assert.AreSame(fooBag, baz.FooBag);
  1193. Assert.AreEqual(2, baz.FooBag.Count);
  1194. bazCode = baz.Code;
  1195. }
  1196. using (ISession s = OpenSession())
  1197. {
  1198. Baz baz = (Baz) s.Load(typeof(Baz), bazCode);
  1199. object bag = baz.FooBag;
  1200. Assert.IsFalse(NHibernateUtil.IsInitialized(bag));
  1201. s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
  1202. Assert.IsTrue(NHibernateUtil.IsInitialized(bag));
  1203. Assert.AreSame(bag, baz.FooBag);
  1204. Assert.AreEqual(2, baz.FooBag.Count);
  1205. s.Delete(baz);
  1206. s.Flush();
  1207. }
  1208. }
  1209. [Test]
  1210. public void Sortables()
  1211. {
  1212. ISession s = OpenSession();
  1213. ITransaction t = s.BeginTransaction();
  1214. Baz b = new Baz();
  1215. var ss = new HashedSet<Sortable>
  1216. {
  1217. new Sortable("foo"), new Sortable("bar"), new Sortable("baz")
  1218. };
  1219. b.Sortablez = ss;
  1220. s.Save(b);
  1221. s.Flush();
  1222. t.Commit();
  1223. s.Close();
  1224. s = OpenSession();
  1225. t = s.BeginTransaction();
  1226. IList result = s.CreateCriteria(typeof(Baz))
  1227. .AddOrder(Order.Asc("Name"))
  1228. .List();
  1229. b = (Baz) result[0];
  1230. Assert.IsTrue(b.Sortablez.Count == 3);
  1231. // compare the first item in the "Set" sortablez - can't reference
  1232. // the first item using b.sortablez[0] because it thinks 0 is the
  1233. // DictionaryEntry key - not the index.
  1234. foreach (Sortable sortable in b.Sortablez)
  1235. {
  1236. Assert.AreEqual(sortable.name, "bar");
  1237. break;
  1238. }
  1239. s.Flush();
  1240. t.Commit();
  1241. s.Close();
  1242. s = OpenSession();
  1243. t = s.BeginTransaction();
  1244. result = s.CreateQuery("from Baz baz left join fetch baz.Sortablez order by baz.Name asc")
  1245. .List();
  1246. b = (Baz) result[0];
  1247. Assert.IsTrue(b.Sortablez.Count == 3);
  1248. foreach (Sortable sortable in b.Sortablez)
  1249. {
  1250. Assert.AreEqual(sortable.name, "bar");
  1251. break;
  1252. }
  1253. s.Flush();
  1254. t.Commit();
  1255. s.Close();
  1256. s = OpenSession();
  1257. t = s.BeginTransaction();
  1258. result = s.CreateQuery("from Baz baz order by baz.Name asc")
  1259. .List();
  1260. b = (Baz) result[0];
  1261. Assert.IsTrue(b.Sortablez.Count == 3);
  1262. foreach (Sortable sortable in b.Sortablez)
  1263. {
  1264. Assert.AreEqual(sortable.name, "bar");
  1265. break;
  1266. }
  1267. s.Delete(b);
  1268. s.Flush();
  1269. t.Commit();
  1270. s.Close();
  1271. }
  1272. [Test]
  1273. public void FetchList()
  1274. {
  1275. ISession s = OpenSession();
  1276. Baz baz = new Baz();
  1277. s.Save(baz);
  1278. Foo foo = new Foo();
  1279. s.Save(foo);
  1280. Foo foo2 = new Foo();
  1281. s.Save(foo2);
  1282. s.Flush();
  1283. IList list = new ArrayList();
  1284. for (int i = 0; i < 5; i++)
  1285. {
  1286. Fee fee = new Fee();
  1287. list.Add(fee);
  1288. }
  1289. baz.Fees = list;
  1290. list = s.CreateQuery("from Foo foo, Baz baz left join fetch baz.Fees").List();
  1291. Assert.IsTrue(NHibernateUtil.IsInitialized(((Baz) ((object[]) list[0])[1]).Fees));
  1292. s.Delete(foo);
  1293. s.Delete(foo2);
  1294. s.Delete(baz);
  1295. s.Flush();
  1296. s.Close();
  1297. }
  1298. [Test]
  1299. public void BagOneToMany()
  1300. {
  1301. ISession s = OpenSession();
  1302. Baz baz = new Baz();
  1303. IList list = new ArrayList();
  1304. baz.Bazez = list;
  1305. list.Add(new Baz());
  1306. s.Save(baz);
  1307. s.Flush();
  1308. list.Add(new Baz());
  1309. s.Flush();
  1310. list.Insert(0, new Baz());
  1311. s.Flush();
  1312. object toDelete = list[1];
  1313. list.RemoveAt(1);
  1314. s.Delete(toDelete);
  1315. s.Flush();
  1316. s.Delete(baz);
  1317. s.Flush();
  1318. s.Close();
  1319. }
  1320. [Test]
  1321. public void QueryLockMode()
  1322. {
  1323. ISession s = OpenSession();
  1324. ITransaction tx = s.BeginTransaction();
  1325. Bar bar = new Bar();
  1326. Assert.IsNull(bar.Bytes);
  1327. s.Save(bar);
  1328. Assert.IsNotNull(bar.Bytes);
  1329. s.Flush();
  1330. Assert.IsNotNull(bar.Bytes);
  1331. bar.String = "changed";
  1332. Baz baz = new Baz();
  1333. baz.Foo = bar;
  1334. s.Save(baz);
  1335. Assert.IsNotNull(bar.Bytes);
  1336. IQuery q = s.CreateQuery("from Foo foo, Bar bar");
  1337. q.SetLockMode("bar", LockMode.Upgrade);
  1338. object[] result = (object[]) q.List()[0];
  1339. Assert.IsNotNull(bar.Bytes);
  1340. object b = result[0];
  1341. Assert.IsTrue(s.GetCurrentLockMode(b) == LockMode.Write && s.GetCurrentLockMode(result[1]) == LockMode.Write);
  1342. tx.Commit();
  1343. Assert.IsNotNull(bar.Bytes);
  1344. s.Disconnect();
  1345. s.Reconnect();
  1346. tx = s.BeginTransaction();
  1347. Assert.IsNotNull(bar.Bytes);
  1348. Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
  1349. Assert.IsNotNull(bar.Bytes);
  1350. s.CreateQuery("from Foo foo").List();
  1351. Assert.IsNotNull(bar.Bytes);
  1352. Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
  1353. q = s.CreateQuery("from Foo foo");
  1354. q.SetLockMode("foo", LockMode.Read);
  1355. q.List();
  1356. Assert.AreEqual(LockMode.Read, s.GetCurrentLockMode(b));
  1357. s.Evict(baz);
  1358. tx.Commit();
  1359. s.Disconnect();
  1360. s.Reconnect();
  1361. tx = s.BeginTransaction();
  1362. Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
  1363. s.Delete(s.Load(typeof(Baz), baz.Code));
  1364. Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
  1365. tx.Commit();
  1366. s.Close();
  1367. s = OpenSession();
  1368. tx = s.BeginTransaction();
  1369. q = s.CreateQuery("from Foo foo, Bar bar, Bar bar2");
  1370. q.SetLockMode("bar", LockMode.Upgrade);
  1371. q.SetLockMode("bar2", LockMode.Read);
  1372. result = (object[]) q.List()[0];
  1373. Assert.IsTrue(s.GetCurrentLockMode(result[0]) == LockMode.Upgrade &&
  1374. s.GetCurrentLockMode(result[1]) == LockMode.Upgrade);
  1375. s.Delete(result[0]);
  1376. tx.Commit();
  1377. s.Close();
  1378. }
  1379. [Test]
  1380. public void ManyToManyBag()
  1381. {
  1382. ISession s = OpenSession();
  1383. Baz baz = new Baz();
  1384. object id = s.Save(baz);
  1385. s.Flush();
  1386. s.Close();
  1387. s = OpenSession();
  1388. baz = (Baz) s.Load(typeof(Baz), id);
  1389. baz.FooBag.Add(new Foo());
  1390. s.Flush();
  1391. s.Close();
  1392. s = OpenSession();
  1393. baz = (Baz) s.Load(typeof(Baz), id);
  1394. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooBag));
  1395. Assert.AreEqual(1, baz.FooBag.Count);
  1396. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.FooBag[0]));
  1397. s.Delete(baz);
  1398. s.Flush();
  1399. s.Close();
  1400. }
  1401. [Test]
  1402. public void IdBag()
  1403. {
  1404. ISession s = OpenSession();
  1405. Baz baz = new Baz();
  1406. s.Save(baz);
  1407. IList l = new ArrayList();
  1408. IList l2 = new ArrayList();
  1409. baz.IdFooBag = l;
  1410. baz.ByteBag = l2;
  1411. l.Add(new Foo());
  1412. l.Add(new Bar());
  1413. byte[] bytes = GetBytes("ffo");
  1414. l2.Add(bytes);
  1415. l2.Add(GetBytes("foo"));
  1416. s.Flush();
  1417. l.Add(new Foo());
  1418. l.Add(new Bar());
  1419. l2.Add(GetBytes("bar"));
  1420. s.Flush();
  1421. object removedObject = l[3];
  1422. l.RemoveAt(3);
  1423. s.Delete(removedObject);
  1424. bytes[1] = Convert.ToByte('o');
  1425. s.Flush();
  1426. s.Close();
  1427. s = OpenSession();
  1428. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  1429. Assert.AreEqual(3, baz.IdFooBag.Count);
  1430. Assert.AreEqual(3, baz.ByteBag.Count);
  1431. bytes = GetBytes("foobar");
  1432. foreach (object obj in baz.IdFooBag)
  1433. {
  1434. s.Delete(obj);
  1435. }
  1436. baz.IdFooBag = null;
  1437. baz.ByteBag.Add(bytes);
  1438. baz.ByteBag.Add(bytes);
  1439. s.Flush();
  1440. s.Close();
  1441. s = OpenSession();
  1442. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  1443. Assert.AreEqual(0, baz.IdFooBag.Count);
  1444. Assert.AreEqual(5, baz.ByteBag.Count);
  1445. s.Delete(baz);
  1446. s.Flush();
  1447. s.Close();
  1448. }
  1449. [Test]
  1450. public void ForceOuterJoin()
  1451. {
  1452. if (sessions.Settings.IsOuterJoinFetchEnabled == false)
  1453. {
  1454. // don't bother to run the test if we can't test it
  1455. return;
  1456. }
  1457. ISession s = OpenSession();
  1458. Glarch g = new Glarch();
  1459. FooComponent fc = new FooComponent();
  1460. fc.Glarch = g;
  1461. FooProxy f = new Foo();
  1462. FooProxy f2 = new Foo();
  1463. f.Component = fc;
  1464. f.TheFoo = f2;
  1465. s.Save(f2);
  1466. object id = s.Save(f);
  1467. object gid = s.GetIdentifier(f.Component.Glarch);
  1468. s.Flush();
  1469. s.Close();
  1470. s = OpenSession();
  1471. f = (FooProxy) s.Load(typeof(Foo), id);
  1472. Assert.IsFalse(NHibernateUtil.IsInitialized(f));
  1473. Assert.IsTrue(NHibernateUtil.IsInitialized(f.Component.Glarch)); //outer-join="true"
  1474. Assert.IsFalse(NHibernateUtil.IsInitialized(f.TheFoo)); //outer-join="auto"
  1475. Assert.AreEqual(gid, s.GetIdentifier(f.Component.Glarch));

Large files files are truncated, but you can click here to view the full file