PageRenderTime 77ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/nhibernate/nhibernate-core
C# | 5521 lines | 4537 code | 753 blank | 231 comment | 150 complexity | 24e4570caf1bd86b7b6aed2d8d307541 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.Common;
  6. using System.Diagnostics;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Runtime.Serialization;
  10. using System.Runtime.Serialization.Formatters.Binary;
  11. using System.Text;
  12. using Iesi.Collections.Generic;
  13. using NHibernate.Connection;
  14. using NHibernate.Dialect;
  15. using NHibernate.DomainModel;
  16. using NHibernate.Criterion;
  17. using NHibernate.Proxy;
  18. using NHibernate.Type;
  19. using NHibernate.Util;
  20. using NUnit.Framework;
  21. namespace NHibernate.Test.Legacy
  22. {
  23. [TestFixture]
  24. public class FooBarTest : TestCase
  25. {
  26. // Equivalent of Java String.getBytes()
  27. private static byte[] GetBytes(string str)
  28. {
  29. return Encoding.Unicode.GetBytes(str);
  30. }
  31. protected override string[] Mappings
  32. {
  33. get
  34. {
  35. return new string[]
  36. {
  37. "FooBar.hbm.xml",
  38. "Baz.hbm.xml",
  39. "Qux.hbm.xml",
  40. "Glarch.hbm.xml",
  41. "Fum.hbm.xml",
  42. "Fumm.hbm.xml",
  43. "Fo.hbm.xml",
  44. "One.hbm.xml",
  45. "Many.hbm.xml",
  46. "Immutable.hbm.xml",
  47. "Fee.hbm.xml",
  48. "Vetoer.hbm.xml",
  49. "Holder.hbm.xml",
  50. "Location.hbm.xml",
  51. "Stuff.hbm.xml",
  52. "Container.hbm.xml",
  53. "Simple.hbm.xml",
  54. "XY.hbm.xml"
  55. };
  56. }
  57. }
  58. [Test]
  59. public void CollectionVersioning()
  60. {
  61. using (ISession s = OpenSession())
  62. {
  63. One one = new One();
  64. one.Manies = new HashSet<Many>();
  65. s.Save(one);
  66. s.Flush();
  67. Many many = new Many();
  68. many.One = one;
  69. one.Manies.Add(many);
  70. s.Save(many);
  71. s.Flush();
  72. // Versions are incremented compared to Hibernate because they start from 1
  73. // in NH.
  74. Assert.AreEqual(1, many.V);
  75. Assert.AreEqual(2, one.V);
  76. s.Delete(many);
  77. s.Delete(one);
  78. s.Flush();
  79. }
  80. }
  81. [Test]
  82. public void ForCertain()
  83. {
  84. Glarch g = new Glarch();
  85. Glarch g2 = new Glarch();
  86. IList<string> strings = new List<string>();
  87. strings.Add("foo");
  88. g2.Strings = strings;
  89. object gid, g2id;
  90. using (ISession s = OpenSession())
  91. {
  92. using (ITransaction t = s.BeginTransaction())
  93. {
  94. gid = s.Save(g);
  95. g2id = s.Save(g2);
  96. t.Commit();
  97. // Versions are initialized to 1 in NH.
  98. Assert.AreEqual(1, g.Version);
  99. Assert.AreEqual(1, g2.Version);
  100. }
  101. }
  102. using (ISession s = OpenSession())
  103. {
  104. using (ITransaction t = s.BeginTransaction())
  105. {
  106. g = (Glarch) s.Get(typeof(Glarch), gid);
  107. g2 = (Glarch) s.Get(typeof(Glarch), g2id);
  108. Assert.AreEqual(1, g2.Strings.Count);
  109. s.Delete(g);
  110. s.Delete(g2);
  111. t.Commit();
  112. }
  113. }
  114. }
  115. [Test]
  116. public void BagMultipleElements()
  117. {
  118. string bazCode;
  119. using (ISession s = OpenSession())
  120. {
  121. using (ITransaction t = s.BeginTransaction())
  122. {
  123. Baz baz = new Baz();
  124. baz.Bag = new List<string>();
  125. baz.ByteBag = new List<byte[]>();
  126. s.Save(baz);
  127. baz.Bag.Add("foo");
  128. baz.Bag.Add("bar");
  129. baz.ByteBag.Add(GetBytes("foo"));
  130. baz.ByteBag.Add(GetBytes("bar"));
  131. t.Commit();
  132. bazCode = baz.Code;
  133. }
  134. }
  135. using (ISession s = OpenSession())
  136. {
  137. using (ITransaction t = s.BeginTransaction())
  138. {
  139. //put in cache
  140. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  141. Assert.AreEqual(2, baz.Bag.Count);
  142. Assert.AreEqual(2, baz.ByteBag.Count);
  143. t.Commit();
  144. }
  145. }
  146. using (ISession s = OpenSession())
  147. {
  148. using (ITransaction t = s.BeginTransaction())
  149. {
  150. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  151. Assert.AreEqual(2, baz.Bag.Count);
  152. Assert.AreEqual(2, baz.ByteBag.Count);
  153. baz.Bag.Remove("bar");
  154. baz.Bag.Add("foo");
  155. baz.ByteBag.Add(GetBytes("bar"));
  156. t.Commit();
  157. }
  158. }
  159. using (ISession s = OpenSession())
  160. {
  161. using (ITransaction t = s.BeginTransaction())
  162. {
  163. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  164. Assert.AreEqual(2, baz.Bag.Count);
  165. Assert.AreEqual(3, baz.ByteBag.Count);
  166. s.Delete(baz);
  167. t.Commit();
  168. }
  169. }
  170. }
  171. [Test]
  172. public void WierdSession()
  173. {
  174. object id;
  175. using (ISession s = OpenSession())
  176. {
  177. using (ITransaction t = s.BeginTransaction())
  178. {
  179. id = s.Save(new Foo());
  180. t.Commit();
  181. }
  182. }
  183. using (ISession s = OpenSession())
  184. {
  185. s.FlushMode = FlushMode.Manual;
  186. using (ITransaction t = s.BeginTransaction())
  187. {
  188. Foo foo = (Foo) s.Get(typeof(Foo), id);
  189. t.Commit();
  190. }
  191. s.Disconnect();
  192. s.Reconnect();
  193. using (ITransaction t = s.BeginTransaction())
  194. {
  195. s.Flush();
  196. t.Commit();
  197. }
  198. }
  199. using (ISession s = OpenSession())
  200. {
  201. using (ITransaction t = s.BeginTransaction())
  202. {
  203. Foo foo = (Foo) s.Get(typeof(Foo), id);
  204. s.Delete(foo);
  205. t.Commit();
  206. }
  207. }
  208. }
  209. [Test]
  210. public void DereferenceLazyCollection()
  211. {
  212. string fooKey;
  213. string bazCode;
  214. using (ISession s = OpenSession())
  215. {
  216. Baz baz = new Baz();
  217. baz.FooSet = new HashSet<FooProxy>();
  218. Foo foo = new Foo();
  219. baz.FooSet.Add(foo);
  220. s.Save(foo);
  221. s.Save(baz);
  222. foo.Bytes = GetBytes("foobar");
  223. s.Flush();
  224. fooKey = foo.Key;
  225. bazCode = baz.Code;
  226. }
  227. using (ISession s = OpenSession())
  228. {
  229. Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
  230. Assert.IsTrue(NHibernateUtil.IsInitialized(foo.Bytes));
  231. // H2.1 has 6 here, but we are using Unicode
  232. Assert.AreEqual(12, foo.Bytes.Length);
  233. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  234. Assert.AreEqual(1, baz.FooSet.Count);
  235. s.Flush();
  236. }
  237. Sfi.EvictCollection("NHibernate.DomainModel.Baz.FooSet");
  238. using (ISession s = OpenSession())
  239. {
  240. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  241. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  242. baz.FooSet = null;
  243. s.Flush();
  244. }
  245. using (ISession s = OpenSession())
  246. {
  247. Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
  248. Assert.AreEqual(12, foo.Bytes.Length);
  249. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  250. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  251. Assert.AreEqual(0, baz.FooSet.Count);
  252. s.Delete(baz);
  253. s.Delete(foo);
  254. s.Flush();
  255. }
  256. }
  257. [Test]
  258. public void MoveLazyCollection()
  259. {
  260. string fooKey, bazCode, baz2Code;
  261. using (ISession s = OpenSession())
  262. {
  263. Baz baz = new Baz();
  264. Baz baz2 = new Baz();
  265. baz.FooSet = new HashSet<FooProxy>();
  266. Foo foo = new Foo();
  267. baz.FooSet.Add(foo);
  268. s.Save(foo);
  269. s.Save(baz);
  270. s.Save(baz2);
  271. foo.Bytes = GetBytes("foobar");
  272. s.Flush();
  273. fooKey = foo.Key;
  274. bazCode = baz.Code;
  275. baz2Code = baz2.Code;
  276. }
  277. using (ISession s = OpenSession())
  278. {
  279. Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
  280. Assert.IsTrue(NHibernateUtil.IsInitialized(foo.Bytes));
  281. Assert.AreEqual(12, foo.Bytes.Length);
  282. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  283. Assert.AreEqual(1, baz.FooSet.Count);
  284. s.Flush();
  285. }
  286. Sfi.EvictCollection("NHibernate.DomainModel.Baz.FooSet");
  287. using (ISession s = OpenSession())
  288. {
  289. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  290. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  291. Baz baz2 = (Baz) s.Get(typeof(Baz), baz2Code);
  292. baz2.FooSet = baz.FooSet;
  293. baz.FooSet = null;
  294. Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.FooSet));
  295. s.Flush();
  296. }
  297. using (ISession s = OpenSession())
  298. {
  299. Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
  300. Assert.AreEqual(12, foo.Bytes.Length);
  301. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  302. Baz baz2 = (Baz) s.Get(typeof(Baz), baz2Code);
  303. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  304. Assert.AreEqual(0, baz.FooSet.Count);
  305. Assert.IsTrue( NHibernateUtil.IsInitialized( baz2.FooSet ) ); //FooSet has batching enabled
  306. Assert.AreEqual(1, baz2.FooSet.Count);
  307. s.Delete(baz);
  308. s.Delete(baz2);
  309. s.Delete(foo);
  310. s.Flush();
  311. }
  312. }
  313. [Test]
  314. public void CriteriaCollection()
  315. {
  316. ISession s = OpenSession();
  317. Baz bb = (Baz) s.CreateCriteria(typeof(Baz)).UniqueResult();
  318. Baz baz = new Baz();
  319. s.Save(baz);
  320. s.Flush();
  321. s.Close();
  322. s = OpenSession();
  323. Baz b = (Baz) s.CreateCriteria(typeof(Baz)).UniqueResult();
  324. Assert.IsTrue(NHibernateUtil.IsInitialized(b.TopGlarchez));
  325. Assert.AreEqual(0, b.TopGlarchez.Count);
  326. s.Delete(b);
  327. s.Flush();
  328. s.CreateCriteria(typeof(Baz))
  329. .CreateCriteria("TopFoos")
  330. .Add(Expression.IsNotNull("id"))
  331. .List();
  332. s.CreateCriteria(typeof(Baz))
  333. .CreateCriteria("Foo")
  334. .CreateCriteria("Component.Glarch")
  335. .CreateCriteria("ProxySet")
  336. .Add(Expression.IsNotNull("id"))
  337. .List();
  338. s.Close();
  339. }
  340. private static bool IsEmpty(IEnumerable enumerable)
  341. {
  342. return !enumerable.GetEnumerator().MoveNext();
  343. }
  344. private static bool ContainsSingleObject(IEnumerable enumerable, object obj)
  345. {
  346. IEnumerator enumerator = enumerable.GetEnumerator();
  347. // Fail if no items
  348. if (!enumerator.MoveNext())
  349. {
  350. return false;
  351. }
  352. // Fail if item not equal
  353. if (!Equals(obj, enumerator.Current))
  354. {
  355. return false;
  356. }
  357. // Fail if more items
  358. if (enumerator.MoveNext())
  359. {
  360. return false;
  361. }
  362. // Succeed
  363. return true;
  364. }
  365. [Test]
  366. public void Query()
  367. {
  368. ISession s = OpenSession();
  369. ITransaction txn = s.BeginTransaction();
  370. Foo foo = new Foo();
  371. s.Save(foo);
  372. Foo foo2 = new Foo();
  373. s.Save(foo2);
  374. foo.TheFoo = foo2;
  375. IList list = s.CreateQuery("from Foo foo inner join fetch foo.TheFoo").List();
  376. Foo foof = (Foo) list[0];
  377. Assert.IsTrue(NHibernateUtil.IsInitialized(foof.TheFoo));
  378. list = s.CreateQuery("from Baz baz left outer join fetch baz.FooToGlarch").List();
  379. list = s.CreateQuery("select foo, bar from Foo foo left outer join foo.TheFoo bar where foo = ?")
  380. .SetEntity(0, foo).List();
  381. object[] row1 = (object[]) list[0];
  382. Assert.IsTrue(row1[0] == foo && row1[1] == foo2);
  383. s.CreateQuery("select foo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo = 'bar'").List();
  384. s.CreateQuery("select foo.TheFoo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo.TheFoo = 'bar'").List();
  385. s.CreateQuery("select foo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo.String = 'bar'").
  386. List();
  387. // if( !( dialect is Dialect.HSQLDialect ) )
  388. // {
  389. s.CreateQuery("select foo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo = foo.TheFoo.TheFoo").List();
  390. // }
  391. s.CreateQuery(
  392. "select foo.String from foo in class Foo where foo.TheFoo.TheFoo = 'bar' and foo.TheFoo.TheFoo.TheFoo = 'baz'").List
  393. ();
  394. s.CreateQuery(
  395. "select foo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo.String = 'a' and foo.TheFoo.String = 'b'").
  396. List();
  397. s.CreateQuery("from bar in class Bar, foo in elements(bar.Baz.FooArray)").List();
  398. if (Dialect is DB2Dialect)
  399. {
  400. s.CreateQuery("from foo in class Foo where lower( foo.TheFoo.String ) = 'foo'").List();
  401. s.CreateQuery("from foo in class Foo where lower( (foo.TheFoo.String || 'foo') || 'bar' ) = 'foo'").List();
  402. s.CreateQuery("from foo in class Foo where repeat( (foo.TheFoo.STring || 'foo') || 'bar', 2 ) = 'foo'").List();
  403. s.CreateQuery(
  404. "From foo in class Bar where foo.TheFoo.Integer is not null and repeat( (foo.TheFoo.String || 'foo') || 'bar', (5+5)/2 ) = 'foo'")
  405. .List();
  406. s.CreateQuery(
  407. "From foo in class Bar where foo.TheFoo.Integer is not null or repeat( (foo.TheFoo.String || 'foo') || 'bar', (5+5)/2 ) = 'foo'")
  408. .List();
  409. }
  410. if (Dialect is MsSql2000Dialect)
  411. {
  412. s.CreateQuery("select baz from Baz as baz join baz.FooArray foo group by baz order by sum(foo.Float)").Enumerable();
  413. }
  414. s.CreateQuery("from Foo as foo where foo.Component.Glarch.Name is not null").List();
  415. s.CreateQuery("from Foo as foo left outer join foo.Component.Glarch as glarch where glarch.Name = 'foo'").List();
  416. list = s.CreateQuery("from Foo").List();
  417. Assert.AreEqual(2, list.Count);
  418. Assert.IsTrue(list[0] is FooProxy);
  419. list = s.CreateQuery("from Foo foo left outer join foo.TheFoo").List();
  420. Assert.AreEqual(2, list.Count);
  421. Assert.IsTrue(((object[]) list[0])[0] is FooProxy);
  422. s.CreateQuery("From Foo, Bar").List();
  423. s.CreateQuery("from Baz baz left join baz.FooToGlarch, Bar bar join bar.TheFoo").List();
  424. s.CreateQuery("from Baz baz left join baz.FooToGlarch join baz.FooSet").List();
  425. s.CreateQuery("from Baz baz left join baz.FooToGlarch join fetch baz.FooSet foo left join fetch foo.TheFoo").List();
  426. list =
  427. s.CreateQuery(
  428. "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")
  429. .List();
  430. Assert.AreEqual(0, list.Count, "empty query");
  431. IEnumerable enumerable =
  432. s.CreateQuery(
  433. "from foo in class NHibernate.DomainModel.Foo where foo.String='osama bin laden' order by foo.String asc, foo.Component.Count desc")
  434. .Enumerable();
  435. Assert.IsTrue(IsEmpty(enumerable), "empty enumerator");
  436. list = s.CreateQuery("select foo.TheFoo from foo in class NHibernate.DomainModel.Foo").List();
  437. Assert.AreEqual(1, list.Count, "query");
  438. Assert.AreEqual(foo.TheFoo, list[0], "returned object");
  439. foo.TheFoo.TheFoo = foo;
  440. foo.String = "fizard";
  441. if (Dialect.SupportsSubSelects && TestDialect.SupportsOperatorSome)
  442. {
  443. if (!(Dialect is FirebirdDialect))
  444. {
  445. list = s.CreateQuery(
  446. "from foo in class NHibernate.DomainModel.Foo where ? = some elements(foo.Component.ImportantDates)").
  447. SetDateTime(0, DateTime.Today).List();
  448. Assert.AreEqual(2, list.Count, "component query");
  449. }
  450. if (Dialect.SupportsScalarSubSelects)
  451. {
  452. list =
  453. s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where size(foo.Component.ImportantDates) = 3").List();
  454. Assert.AreEqual(2, list.Count, "component query");
  455. list = s.CreateQuery("from foo in class Foo where 0 = size(foo.Component.ImportantDates)").List();
  456. Assert.AreEqual(0, list.Count, "component query");
  457. }
  458. list = s.CreateQuery("from foo in class Foo where exists elements(foo.Component.ImportantDates)").List();
  459. Assert.AreEqual(2, list.Count, "component query");
  460. s.CreateQuery("from foo in class Foo where not exists (from bar in class Bar where bar.id = foo.id)").List();
  461. s.CreateQuery(
  462. "select foo.TheFoo from foo in class Foo where foo = some(select x from x in class Foo where x.Long > foo.TheFoo.Long)")
  463. .List();
  464. s.CreateQuery(
  465. "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'")
  466. .List();
  467. s.CreateQuery(
  468. "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)")
  469. .List();
  470. s.CreateQuery("from foo in class Foo where foo = some(select x from x in class Foo where x.Long > foo.TheFoo.Long)")
  471. .List();
  472. s.CreateQuery(
  473. "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'")
  474. .Enumerable();
  475. }
  476. list = s.CreateQuery("from foo in class Foo where foo.Component.Count is null order by foo.Component.Count").List();
  477. Assert.AreEqual(0, list.Count, "component query");
  478. list = s.CreateQuery("from foo in class Foo where foo.Component.Name='foo'").List();
  479. Assert.AreEqual(2, list.Count, "component query");
  480. list =
  481. s.CreateQuery(
  482. "select distinct foo.Component.Name, foo.Component.Name from foo in class Foo where foo.Component.Name='foo'").List
  483. ();
  484. Assert.AreEqual(1, list.Count, "component query");
  485. list =
  486. s.CreateQuery("select distinct foo.Component.Name, foo.id from foo in class Foo where foo.Component.Name='foo'").
  487. List();
  488. Assert.AreEqual(2, list.Count, "component query");
  489. list = s.CreateQuery("select foo.TheFoo from foo in class Foo").List();
  490. Assert.AreEqual(2, list.Count, "query");
  491. list = s.CreateQuery("from foo in class Foo where foo.id=?").SetString(0, foo.Key).List();
  492. Assert.AreEqual(1, list.Count, "id query");
  493. list = s.CreateQuery("from foo in class Foo where foo.Key=?").SetString(0, foo.Key).List();
  494. Assert.AreEqual(1, list.Count, "named id query");
  495. Assert.AreSame(foo, list[0], "id query");
  496. list = s.CreateQuery("select foo.TheFoo from foo in class Foo where foo.String='fizard'").List();
  497. Assert.AreEqual(1, list.Count, "query");
  498. Assert.AreSame(foo.TheFoo, list[0], "returned object");
  499. list = s.CreateQuery("from foo in class Foo where foo.Component.Subcomponent.Name='bar'").List();
  500. Assert.AreEqual(2, list.Count, "components of components");
  501. list = s.CreateQuery("select foo.TheFoo from foo in class Foo where foo.TheFoo.id=?")
  502. .SetString(0, foo.TheFoo.Key).List();
  503. Assert.AreEqual(1, list.Count, "by id query");
  504. Assert.AreSame(foo.TheFoo, list[0], "by id returned object");
  505. s.CreateQuery("from foo in class Foo where foo.TheFoo = ?").SetEntity(0, foo.TheFoo).List();
  506. Assert.IsTrue(
  507. IsEmpty(s.CreateQuery("from bar in class Bar where bar.String='a string' or bar.String='a string'").Enumerable()
  508. ));
  509. enumerable = s.CreateQuery(
  510. "select foo.Component.Name, elements(foo.Component.ImportantDates) from foo in class Foo where foo.TheFoo.id=?").
  511. SetString(0, foo.TheFoo.Key).Enumerable();
  512. int i = 0;
  513. foreach (object[] row in enumerable)
  514. {
  515. i++;
  516. Assert.IsTrue(row[0] is String);
  517. Assert.IsTrue(row[1] == null || row[1] is DateTime);
  518. }
  519. Assert.AreEqual(3, i); //WAS: 4
  520. enumerable = s.CreateQuery("select max(elements(foo.Component.ImportantDates)) from foo in class Foo group by foo.id").
  521. Enumerable();
  522. IEnumerator enumerator = enumerable.GetEnumerator();
  523. Assert.IsTrue(enumerator.MoveNext());
  524. Assert.IsTrue(enumerator.Current is DateTime);
  525. list = s.CreateQuery(
  526. "select foo.TheFoo.TheFoo.TheFoo from foo in class Foo, foo2 in class Foo where"
  527. + " foo = foo2.TheFoo and not not ( not foo.String='fizard' )"
  528. + " and foo2.String between 'a' and (foo.TheFoo.String)"
  529. + (Dialect is SQLiteDialect
  530. ? " and ( foo2.String in ( 'fiz', 'blah') or 1=1 )"
  531. : " and ( foo2.String in ( 'fiz', 'blah', foo.TheFoo.String, foo.String, foo2.String ) )")
  532. ).List();
  533. Assert.AreEqual(1, list.Count, "complex query");
  534. Assert.AreSame(foo, list[0], "returned object");
  535. foo.String = "from BoogieDown -tinsel town =!@#$^&*())";
  536. list = s.CreateQuery("from foo in class Foo where foo.String='from BoogieDown -tinsel town =!@#$^&*())'").List();
  537. Assert.AreEqual(1, list.Count, "single quotes");
  538. list = s.CreateQuery("from foo in class Foo where not foo.String='foo''bar'").List();
  539. Assert.AreEqual(2, list.Count, "single quotes");
  540. list = s.CreateQuery("from foo in class Foo where foo.Component.Glarch.Next is null").List();
  541. Assert.AreEqual(2, list.Count, "query association in component");
  542. Bar bar = new Bar();
  543. Baz baz = new Baz();
  544. baz.SetDefaults();
  545. bar.Baz = baz;
  546. baz.ManyToAny = new List<object>();
  547. baz.ManyToAny.Add(bar);
  548. baz.ManyToAny.Add(foo);
  549. s.Save(bar);
  550. s.Save(baz);
  551. list =
  552. 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'").
  553. List();
  554. Assert.AreEqual(1, list.Count, "query many-to-one");
  555. list = s.CreateQuery(" from i in class Bar where i.Baz.Name='Bazza'").List();
  556. Assert.AreEqual(1, list.Count, "query many-to-one");
  557. if (TestDialect.SupportsCountDistinct)
  558. {
  559. enumerable = s.CreateQuery("select count(distinct foo.TheFoo) from foo in class Foo").Enumerable();
  560. Assert.IsTrue(ContainsSingleObject(enumerable, (long) 2), "count"); // changed to Int64 (HQLFunction H3.2)
  561. }
  562. enumerable = s.CreateQuery("select count(foo.TheFoo.Boolean) from foo in class Foo").Enumerable();
  563. Assert.IsTrue(ContainsSingleObject(enumerable, (long) 2), "count"); // changed to Int64 (HQLFunction H3.2)
  564. enumerable = s.CreateQuery("select count(*), foo.Int from foo in class Foo group by foo.Int").Enumerable();
  565. enumerator = enumerable.GetEnumerator();
  566. Assert.IsTrue(enumerator.MoveNext());
  567. Assert.AreEqual(3L, (long) ((object[]) enumerator.Current)[0]);
  568. Assert.IsFalse(enumerator.MoveNext());
  569. enumerable = s.CreateQuery("select sum(foo.TheFoo.Int) from foo in class Foo").Enumerable();
  570. Assert.IsTrue(ContainsSingleObject(enumerable, (long) 4), "sum"); // changed to Int64 (HQLFunction H3.2)
  571. enumerable = s.CreateQuery("select count(foo) from foo in class Foo where foo.id=?")
  572. .SetString(0, foo.Key).Enumerable();
  573. Assert.IsTrue(ContainsSingleObject(enumerable, (long) 1), "id query count");
  574. list = s.CreateQuery("from foo in class Foo where foo.Boolean = ?").SetBoolean(0, true).List();
  575. list = s.CreateQuery("select new Foo(fo.X) from Fo fo").List();
  576. list = s.CreateQuery("select new Foo(fo.Integer) from Foo fo").List();
  577. list = s.CreateQuery("select new Foo(fo.X) from Foo fo")
  578. .SetCacheable(true)
  579. .List();
  580. Assert.IsTrue(list.Count == 3);
  581. list = s.CreateQuery("select new Foo(fo.X) from Foo fo")
  582. .SetCacheable(true)
  583. .List();
  584. Assert.IsTrue(list.Count == 3);
  585. enumerable = s.CreateQuery("select new Foo(fo.X) from Foo fo").Enumerable();
  586. enumerator = enumerable.GetEnumerator();
  587. Assert.IsTrue(enumerator.MoveNext(), "projection iterate (results)");
  588. Assert.IsTrue(typeof(Foo).IsAssignableFrom(enumerator.Current.GetType()),
  589. "projection iterate (return check)");
  590. // TODO: ScrollableResults not implemented
  591. //ScrollableResults sr = s.CreateQuery("select new Foo(fo.x) from Foo fo").Scroll();
  592. //Assert.IsTrue( "projection scroll (results)", sr.next() );
  593. //Assert.IsTrue( "projection scroll (return check)", typeof(Foo).isAssignableFrom( sr.get(0).getClass() ) );
  594. list = s.CreateQuery("select foo.Long, foo.Component.Name, foo, foo.TheFoo from foo in class Foo").List();
  595. Assert.IsTrue(list.Count > 0);
  596. foreach (object[] row in list)
  597. {
  598. Assert.IsTrue(row[0] is long);
  599. Assert.IsTrue(row[1] is string);
  600. Assert.IsTrue(row[2] is Foo);
  601. Assert.IsTrue(row[3] is Foo);
  602. }
  603. if (TestDialect.SupportsCountDistinct)
  604. {
  605. list =
  606. s.CreateQuery("select avg(foo.Float), max(foo.Component.Name), count(distinct foo.id) from foo in class Foo").List();
  607. Assert.IsTrue(list.Count > 0);
  608. foreach (object[] row in list)
  609. {
  610. Assert.IsTrue(row[0] is double); // changed from float to double (HQLFunction H3.2)
  611. Assert.IsTrue(row[1] is string);
  612. Assert.IsTrue(row[2] is long); // changed from int to long (HQLFunction H3.2)
  613. }
  614. }
  615. list = s.CreateQuery("select foo.Long, foo.Component, foo, foo.TheFoo from foo in class Foo").List();
  616. Assert.IsTrue(list.Count > 0);
  617. foreach (object[] row in list)
  618. {
  619. Assert.IsTrue(row[0] is long);
  620. Assert.IsTrue(row[1] is FooComponent);
  621. Assert.IsTrue(row[2] is Foo);
  622. Assert.IsTrue(row[3] is Foo);
  623. }
  624. s.Save(new Holder("ice T"));
  625. s.Save(new Holder("ice cube"));
  626. Assert.AreEqual(15, s.CreateQuery("from o in class System.Object").List().Count);
  627. Assert.AreEqual(7, s.CreateQuery("from n in class INamed").List().Count);
  628. Assert.IsTrue(s.CreateQuery("from n in class INamed where n.Name is not null").List().Count == 4);
  629. foreach (INamed named in s.CreateQuery("from n in class INamed").Enumerable())
  630. {
  631. Assert.IsNotNull(named);
  632. }
  633. s.Save(new Holder("bar"));
  634. enumerable = s.CreateQuery("from n0 in class INamed, n1 in class INamed where n0.Name = n1.Name").Enumerable();
  635. int cnt = 0;
  636. foreach (object[] row in enumerable)
  637. {
  638. if (row[0] != row[1])
  639. {
  640. cnt++;
  641. }
  642. }
  643. //if ( !(dialect is Dialect.HSQLDialect) )
  644. //{
  645. Assert.IsTrue(cnt == 2);
  646. Assert.IsTrue(s.CreateQuery("from n0 in class INamed, n1 in class INamed where n0.Name = n1.Name").List().Count == 7);
  647. //}
  648. IQuery qu = s.CreateQuery("from n in class INamed where n.Name = :name");
  649. object temp = qu.ReturnTypes;
  650. temp = qu.NamedParameters;
  651. int c = 0;
  652. foreach (object obj in s.CreateQuery("from o in class System.Object").Enumerable())
  653. {
  654. c++;
  655. }
  656. Assert.IsTrue(c == 16);
  657. s.CreateQuery("select baz.Code, min(baz.Count) from baz in class Baz group by baz.Code").Enumerable();
  658. Assert.IsTrue(
  659. IsEmpty(
  660. s.CreateQuery(
  661. "selecT baz from baz in class Baz where baz.StringDateMap['foo'] is not null or baz.StringDateMap['bar'] = ?")
  662. .SetDateTime(0, DateTime.Today).Enumerable()));
  663. list = s.CreateQuery("select baz from baz in class Baz where baz.StringDateMap['now'] is not null").List();
  664. Assert.AreEqual(1, list.Count);
  665. list =
  666. s.CreateQuery("select baz from baz in class Baz where baz.StringDateMap[:now] is not null").SetString("now", "now").
  667. List();
  668. Assert.AreEqual(1, list.Count);
  669. list =
  670. s.CreateQuery(
  671. "select baz from baz in class Baz where baz.StringDateMap['now'] is not null and baz.StringDateMap['big bang'] < baz.StringDateMap['now']")
  672. .List();
  673. Assert.AreEqual(1, list.Count);
  674. list = s.CreateQuery("select index(date) from Baz baz join baz.StringDateMap date").List();
  675. Console.WriteLine(list);
  676. Assert.AreEqual(3, list.Count);
  677. s.CreateQuery(
  678. "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")
  679. .List();
  680. s.CreateQuery("from Baz baz inner join baz.CollectionComponent.Nested.Foos foo where foo.String is null").List();
  681. if (Dialect.SupportsSubSelects)
  682. {
  683. s.CreateQuery("from Baz baz inner join baz.FooSet where '1' in (from baz.FooSet foo where foo.String is not null)").
  684. List();
  685. s.CreateQuery(
  686. "from Baz baz where 'a' in elements(baz.CollectionComponent.Nested.Foos) and 1.0 in elements(baz.CollectionComponent.Nested.Floats)")
  687. .List();
  688. s.CreateQuery(
  689. "from Baz baz where 'b' in elements(baz.CollectionComponent.Nested.Foos) and 1.0 in elements(baz.CollectionComponent.Nested.Floats)")
  690. .List();
  691. }
  692. s.CreateQuery("from Foo foo join foo.TheFoo where foo.TheFoo in ('1','2','3')").List();
  693. //if ( !(dialect is Dialect.HSQLDialect) )
  694. s.CreateQuery("from Foo foo left join foo.TheFoo where foo.TheFoo in ('1','2','3')").List();
  695. s.CreateQuery("select foo.TheFoo from Foo foo where foo.TheFoo in ('1','2','3')").List();
  696. s.CreateQuery("select foo.TheFoo.String from Foo foo where foo.TheFoo in ('1','2','3')").List();
  697. s.CreateQuery("select foo.TheFoo.String from Foo foo where foo.TheFoo.String in ('1','2','3')").List();
  698. s.CreateQuery("select foo.TheFoo.Long from Foo foo where foo.TheFoo.String in ('1','2','3')").List();
  699. s.CreateQuery("select count(*) from Foo foo where foo.TheFoo.String in ('1','2','3') or foo.TheFoo.Long in (1,2,3)").
  700. List();
  701. s.CreateQuery("select count(*) from Foo foo where foo.TheFoo.String in ('1','2','3') group by foo.TheFoo.Long").List();
  702. s.CreateQuery("from Foo foo1 left join foo1.TheFoo foo2 left join foo2.TheFoo where foo1.String is not null").List();
  703. s.CreateQuery("from Foo foo1 left join foo1.TheFoo.TheFoo where foo1.String is not null").List();
  704. s.CreateQuery(
  705. "from Foo foo1 left join foo1.TheFoo foo2 left join foo1.TheFoo.TheFoo foo3 where foo1.String is not null").List();
  706. s.CreateQuery("select foo.Formula from Foo foo where foo.Formula > 0").List();
  707. int len = s.CreateQuery("from Foo as foo join foo.TheFoo as foo2 where foo2.id >'a' or foo2.id <'a'").List().Count;
  708. Assert.IsTrue(len == 2);
  709. s.Delete("from Holder");
  710. txn.Commit();
  711. s.Close();
  712. s = OpenSession();
  713. txn = s.BeginTransaction();
  714. baz = (Baz) s.CreateQuery("from Baz baz left outer join fetch baz.ManyToAny").UniqueResult();
  715. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.ManyToAny));
  716. Assert.IsTrue(baz.ManyToAny.Count == 2);
  717. BarProxy barp = (BarProxy) baz.ManyToAny[0];
  718. s.CreateQuery("from Baz baz join baz.ManyToAny").List();
  719. Assert.IsTrue(s.CreateQuery("select baz from Baz baz join baz.ManyToAny a where index(a) = 0").List().Count == 1);
  720. FooProxy foop = (FooProxy) s.Get(typeof(Foo), foo.Key);
  721. Assert.IsTrue(foop == baz.ManyToAny[1]);
  722. barp.Baz = baz;
  723. Assert.IsTrue(s.CreateQuery("select bar from Bar bar where bar.Baz.StringDateMap['now'] is not null").List().Count ==
  724. 1);
  725. Assert.IsTrue(
  726. s.CreateQuery(
  727. "select bar from Bar bar join bar.Baz b where b.StringDateMap['big bang'] < b.StringDateMap['now'] and b.StringDateMap['now'] is not null")
  728. .List().Count == 1);
  729. Assert.IsTrue(
  730. s.CreateQuery(
  731. "select bar from Bar bar where bar.Baz.StringDateMap['big bang'] < bar.Baz.StringDateMap['now'] and bar.Baz.StringDateMap['now'] is not null")
  732. .List().Count == 1);
  733. list = s.CreateQuery("select foo.String, foo.Component, foo.id from Bar foo").List();
  734. Assert.IsTrue(((FooComponent) ((object[]) list[0])[1]).Name == "foo");
  735. list = s.CreateQuery("select elements(baz.Components) from Baz baz").List();
  736. Assert.IsTrue(list.Count == 2);
  737. list = s.CreateQuery("select bc.Name from Baz baz join baz.Components bc").List();
  738. Assert.IsTrue(list.Count == 2);
  739. //list = s.CreateQuery("select bc from Baz baz join baz.components bc").List();
  740. s.CreateQuery("from Foo foo where foo.Integer < 10 order by foo.String").SetMaxResults(12).List();
  741. s.Delete(barp);
  742. s.Delete(baz);
  743. s.Delete(foop.TheFoo);
  744. s.Delete(foop);
  745. txn.Commit();
  746. s.Close();
  747. }
  748. [Test]
  749. public void CascadeDeleteDetached()
  750. {
  751. Baz baz;
  752. using (ISession s = OpenSession())
  753. {
  754. baz = new Baz();
  755. IList<Fee> list = new List<Fee>();
  756. list.Add(new Fee());
  757. baz.Fees = list;
  758. s.Save(baz);
  759. s.Flush();
  760. }
  761. using (ISession s = OpenSession())
  762. {
  763. baz = (Baz) s.Get(typeof(Baz), baz.Code);
  764. }
  765. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.Fees));
  766. using (ISession s = OpenSession())
  767. {
  768. s.Delete(baz);
  769. s.Flush();
  770. Assert.IsFalse(s.CreateQuery("from Fee").Enumerable().GetEnumerator().MoveNext());
  771. }
  772. using (ISession s = OpenSession())
  773. {
  774. baz = new Baz();
  775. IList<Fee> list = new List<Fee>();
  776. list.Add(new Fee());
  777. list.Add(new Fee());
  778. baz.Fees = list;
  779. s.Save(baz);
  780. s.Flush();
  781. }
  782. using (ISession s = OpenSession())
  783. {
  784. baz = (Baz) s.Get(typeof(Baz), baz.Code);
  785. NHibernateUtil.Initialize(baz.Fees);
  786. }
  787. Assert.AreEqual(2, baz.Fees.Count);
  788. using (ISession s = OpenSession())
  789. {
  790. s.Delete(baz);
  791. s.Flush();
  792. Assert.IsTrue(IsEmpty(s.CreateQuery("from Fee").Enumerable()));
  793. }
  794. }
  795. [Test]
  796. public void ForeignKeys()
  797. {
  798. Baz baz;
  799. using (ISession s = OpenSession())
  800. {
  801. baz = new Baz();
  802. Foo foo = new Foo();
  803. IList<Foo> bag = new List<Foo>();
  804. bag.Add(foo);
  805. baz.IdFooBag = bag;
  806. baz.Foo = foo;
  807. s.Save(baz);
  808. s.Flush();
  809. }
  810. using (ISession s = OpenSession())
  811. {
  812. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  813. s.Delete(baz);
  814. s.Flush();
  815. }
  816. }
  817. [Test]
  818. public void NonlazyCollections()
  819. {
  820. object glarchId;
  821. using (ISession s = OpenSession())
  822. {
  823. Glarch glarch1 = new Glarch();
  824. glarch1.ProxySet = new LinkedHashSet<GlarchProxy>();
  825. Glarch glarch2 = new Glarch();
  826. glarch1.ProxySet.Add(glarch1);
  827. s.Save(glarch2);
  828. glarchId = s.Save(glarch1);
  829. s.Flush();
  830. }
  831. Glarch loadedGlarch;
  832. using (ISession s = OpenSession())
  833. {
  834. loadedGlarch = (Glarch) s.Get(typeof(Glarch), glarchId);
  835. Assert.IsTrue(NHibernateUtil.IsInitialized(loadedGlarch.ProxySet));
  836. }
  837. // ProxySet is a non-lazy collection, so this should work outside
  838. // a session.
  839. Assert.AreEqual(1, loadedGlarch.ProxySet.Count);
  840. using (ISession s = OpenSession())
  841. {
  842. s.Delete("from Glarch");
  843. s.Flush();
  844. }
  845. }
  846. [Test]
  847. public void ReuseDeletedCollection()
  848. {
  849. Baz baz, baz2;
  850. using (ISession s = OpenSession())
  851. {
  852. baz = new Baz();
  853. baz.SetDefaults();
  854. s.Save(baz);
  855. s.Flush();
  856. s.Delete(baz);
  857. baz2 = new Baz();
  858. baz2.StringArray = new string[] {"x-y-z"};
  859. s.Save(baz2);
  860. s.Flush();
  861. }
  862. baz2.StringSet = baz.StringSet;
  863. baz2.StringArray = baz.StringArray;
  864. baz2.FooArray = baz.FooArray;
  865. using (ISession s = OpenSession())
  866. {
  867. s.Update(baz2);
  868. s.Flush();
  869. }
  870. using (ISession s = OpenSession())
  871. {
  872. baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
  873. Assert.AreEqual(3, baz2.StringArray.Length);
  874. Assert.AreEqual(3, baz2.StringSet.Count);
  875. s.Delete(baz2);
  876. s.Flush();
  877. }
  878. }
  879. [Test]
  880. public void PropertyRef()
  881. {
  882. object qid;
  883. object hid;
  884. using (ISession s = OpenSession())
  885. {
  886. Holder h = new Holder();
  887. h.Name = "foo";
  888. Holder h2 = new Holder();
  889. h2.Name = "bar";
  890. h.OtherHolder = h2;
  891. hid = s.Save(h);
  892. Qux q = new Qux();
  893. q.Holder = h2;
  894. qid = s.Save(q);
  895. s.Flush();
  896. }
  897. using (ISession s = OpenSession())
  898. {
  899. Holder h = (Holder) s.Load(typeof(Holder), hid);
  900. Assert.AreEqual(h.Name, "foo");
  901. Assert.AreEqual(h.OtherHolder.Name, "bar");
  902. object[] res =
  903. (object[]) s.CreateQuery("from Holder h join h.OtherHolder oh where h.OtherHolder.Name = 'bar'").List()[0];
  904. Assert.AreSame(h, res[0]);
  905. Qux q = (Qux) s.Get(typeof(Qux), qid);
  906. Assert.AreSame(q.Holder, h.OtherHolder);
  907. s.Delete(h);
  908. s.Delete(q);
  909. s.Flush();
  910. }
  911. }
  912. [Test]
  913. public void QueryCollectionOfValues()
  914. {
  915. object gid;
  916. using (ISession s = OpenSession())
  917. {
  918. Baz baz = new Baz();
  919. baz.SetDefaults();
  920. s.Save(baz);
  921. Glarch g = new Glarch();
  922. gid = s.Save(g);
  923. if (Dialect.SupportsSubSelects)
  924. {
  925. if (Dialect.SupportsScalarSubSelects)
  926. s.CreateFilter(baz.FooArray, "where size(this.Bytes) > 0").List();
  927. s.CreateFilter(baz.FooArray, "where 0 in elements(this.Bytes)").List();
  928. }
  929. s.Flush();
  930. }
  931. using (ISession s = OpenSession())
  932. {
  933. //s.CreateQuery("from Baz baz where baz.FooSet.String = 'foo'").List();
  934. //s.CreateQuery("from Baz baz where baz.FooArray.String = 'foo'").List();
  935. //s.CreateQuery("from Baz baz where baz.FooSet.foo.String = 'foo'").List();
  936. //s.CreateQuery("from Baz baz join baz.FooSet.Foo foo where foo.String = 'foo'").List();
  937. s.CreateQuery("from Baz baz join baz.FooSet foo join foo.TheFoo.TheFoo foo2 where foo2.String = 'foo'").List();
  938. s.CreateQuery("from Baz baz join baz.FooArray foo join foo.TheFoo.TheFoo foo2 where foo2.String = 'foo'").List();
  939. s.CreateQuery("from Baz baz join baz.StringDateMap date where index(date) = 'foo'").List();
  940. s.CreateQuery("from Baz baz join baz.TopGlarchez g where index(g) = 'A'").List();
  941. s.CreateQuery("select index(g) from Baz baz join baz.TopGlarchez g").List();
  942. Assert.AreEqual(3, s.CreateQuery("from Baz baz left join baz.StringSet").List().Count);
  943. Baz baz = (Baz) s.CreateQuery("from Baz baz join baz.StringSet str where str='foo'").List()[0];
  944. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.StringSet));
  945. baz = (Baz) s.CreateQuery("from Baz baz left join fetch baz.StringSet").List()[0];
  946. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.StringSet));
  947. Assert.AreEqual(1, s.CreateQuery("from Baz baz join baz.StringSet string where string='foo'").List().Count);
  948. Assert.AreEqual(1, s.CreateQuery("from Baz baz inner join baz.Components comp where comp.Name='foo'").List().Count);
  949. //IList bss = s.CreateQuery("select baz, ss from Baz baz inner join baz.StringSet ss").List();
  950. s.CreateQuery("from Glarch g inner join g.FooComponents comp where comp.Fee is not null").List();
  951. s.CreateQuery("from Glarch g inner join g.FooComponents comp join comp.Fee fee where fee.Count > 0").List();
  952. s.CreateQuery("from Glarch g inner join g.FooComponents comp where comp.Fee.Count is not null").List();
  953. s.Delete(baz);
  954. //s.delete("from Glarch g");
  955. s.Delete(s.Get(typeof(Glarch), gid));
  956. s.Flush();
  957. }
  958. }
  959. [Test]
  960. public void BatchLoad()
  961. {
  962. Baz baz, baz2, baz3;
  963. using (ISession s = OpenSession())
  964. {
  965. baz = new Baz();
  966. var stringSet = new SortedSet<string> { "foo", "bar" };
  967. var fooSet = new HashSet<FooProxy>();
  968. for (int i = 0; i < 3; i++)
  969. {
  970. Foo foo = new Foo();
  971. s.Save(foo);
  972. fooSet.Add(foo);
  973. }
  974. baz.FooSet = fooSet;
  975. baz.StringSet = stringSet;
  976. s.Save(baz);
  977. baz2 = new Baz();
  978. fooSet = new HashSet<FooProxy>();
  979. for (int i = 0; i < 2; i++)
  980. {
  981. Foo foo = new Foo();
  982. s.Save(foo);
  983. fooSet.Add(foo);
  984. }
  985. baz2.FooSet = fooSet;
  986. s.Save(baz2);
  987. baz3 = new Baz();
  988. stringSet = new SortedSet<string>();
  989. stringSet.Add("foo");
  990. stringSet.Add("baz");
  991. baz3.StringSet = stringSet;
  992. s.Save(baz3);
  993. s.Flush();
  994. }
  995. using (ISession s = OpenSession())
  996. {
  997. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  998. baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
  999. baz3 = (Baz) s.Load(typeof(Baz), baz3.Code);
  1000. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  1001. Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.FooSet));
  1002. Assert.IsFalse(NHibernateUtil.IsInitialized(baz3.FooSet));
  1003. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.StringSet));
  1004. Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.StringSet));
  1005. Assert.IsFalse(NHibernateUtil.IsInitialized(baz3.StringSet));
  1006. Assert.AreEqual(3, baz.FooSet.Count);
  1007. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.FooSet));
  1008. Assert.IsTrue(NHibernateUtil.IsInitialized(baz2.FooSet));
  1009. Assert.IsTrue(NHibernateUtil.IsInitialized(baz3.FooSet));
  1010. Assert.AreEqual(2, baz2.FooSet.Count);
  1011. Assert.IsTrue(baz3.StringSet.Contains("baz"));
  1012. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.StringSet));
  1013. Assert.IsTrue(NHibernateUtil.IsInitialized(baz2.StringSet));
  1014. Assert.IsTrue(NHibernateUtil.IsInitialized(baz3.StringSet));
  1015. Assert.AreEqual(2, baz.StringSet.Count);
  1016. Assert.AreEqual(0, baz2.StringSet.Count);
  1017. s.Delete(baz);
  1018. s.Delete(baz2);
  1019. s.Delete(baz3);
  1020. IEnumerable en = new JoinedEnumerable(
  1021. new IEnumerable[] {baz.FooSet, baz2.FooSet});
  1022. foreach (object obj in en)
  1023. {
  1024. s.Delete(obj);
  1025. }
  1026. s.Flush();
  1027. }
  1028. }
  1029. [Test]
  1030. public void FetchInitializedCollection()
  1031. {
  1032. ISession s = OpenSession();
  1033. Baz baz = new Baz();
  1034. IList<Foo> fooBag = new List<Foo>();
  1035. fooBag.Add(new Foo());
  1036. fooBag.Add(new Foo());
  1037. baz.FooBag = fooBag;
  1038. s.Save(baz);
  1039. s.Flush();
  1040. fooBag = baz.FooBag;
  1041. s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
  1042. Assert.IsTrue(NHibernateUtil.IsInitialized(fooBag));
  1043. s.Close();
  1044. s = OpenSession();
  1045. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  1046. Object bag = baz.FooBag;
  1047. Assert.IsFalse(NHibernateUtil.IsInitialized(bag));
  1048. s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
  1049. Assert.IsTrue(bag == baz.FooBag);
  1050. Assert.IsTrue(baz.FooBag.Count == 2);
  1051. s.Delete(baz);
  1052. s.Flush();
  1053. s.Close();
  1054. }
  1055. [Test]
  1056. public void LateCollectionAdd()
  1057. {
  1058. object id;
  1059. using (ISession s = OpenSession())
  1060. {
  1061. Baz baz = new Baz();
  1062. IList<string> l = new List<string>();
  1063. baz.StringList = l;
  1064. l.Add("foo");
  1065. id = s.Save(baz);
  1066. l.Add("bar");
  1067. s.Flush();
  1068. l.Add("baz");
  1069. s.Flush();
  1070. }
  1071. using (ISession s = OpenSession())
  1072. {
  1073. Baz baz = (Baz) s.Load(typeof(Baz), id);
  1074. Assert.AreEqual(3, baz.StringList.Count);
  1075. Assert.IsTrue(baz.StringList.Contains("foo"));
  1076. Assert.IsTrue(baz.StringList.Contains("bar"));
  1077. Assert.IsTrue(baz.StringList.Contains("baz"));
  1078. s.Delete(baz);
  1079. s.Flush();
  1080. }
  1081. }
  1082. [Test]
  1083. public void Update()
  1084. {
  1085. ISession s = OpenSession();
  1086. Foo foo = new Foo();
  1087. s.Save(foo);
  1088. s.Flush();
  1089. s.Close();
  1090. s = OpenSession();
  1091. FooProxy foo2 = (FooProxy) s.Load(typeof(Foo), foo.Key);
  1092. foo2.String = "dirty";
  1093. foo2.Boolean = false;
  1094. foo2.Bytes = new byte[] {1, 2, 3};
  1095. foo2.Date = DateTime.Today;
  1096. foo2.Short = 69;
  1097. s.Flush();
  1098. s.Close();
  1099. s = OpenSession();
  1100. Foo foo3 = new Foo();
  1101. s.Load(foo3, foo.Key);
  1102. Assert.IsTrue(foo2.EqualsFoo(foo3), "update");
  1103. s.Delete(foo3);
  1104. s.Flush();
  1105. s.Close();
  1106. }
  1107. [Test]
  1108. public void ListRemove()
  1109. {
  1110. using (ISession s = OpenSession())
  1111. {
  1112. Baz b = new Baz();
  1113. IList<string> stringList = new List<string>();
  1114. IList<Fee> feeList = new List<Fee>();
  1115. b.Fees = feeList;
  1116. b.StringList = stringList;
  1117. feeList.Add(new Fee());
  1118. feeList.Add(new Fee());
  1119. feeList.Add(new Fee());
  1120. feeList.Add(new Fee());
  1121. stringList.Add("foo");
  1122. stringList.Add("bar");
  1123. stringList.Add("baz");
  1124. stringList.Add("glarch");
  1125. s.Save(b);
  1126. s.Flush();
  1127. stringList.RemoveAt(1);
  1128. feeList.RemoveAt(1);
  1129. s.Flush();
  1130. s.Evict(b);
  1131. s.Refresh(b);
  1132. Assert.AreEqual(3, b.Fees.Count);
  1133. stringList = b.StringList;
  1134. Assert.AreEqual(3, stringList.Count);
  1135. Assert.AreEqual("baz", stringList[1]);
  1136. Assert.AreEqual("foo", stringList[0]);
  1137. s.Delete(b);
  1138. s.Delete("from Fee");
  1139. s.Flush();
  1140. }
  1141. }
  1142. [Test]
  1143. public void FetchInitializedCollectionDupe()
  1144. {
  1145. string bazCode;
  1146. using (ISession s = OpenSession())
  1147. {
  1148. Baz baz = new Baz();
  1149. IList<Foo> fooBag = new List<Foo>();
  1150. fooBag.Add(new Foo());
  1151. fooBag.Add(new Foo());
  1152. baz.FooBag = fooBag;
  1153. s.Save(baz);
  1154. s.Flush();
  1155. fooBag = baz.FooBag;
  1156. s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
  1157. Assert.IsTrue(NHibernateUtil.IsInitialized(fooBag));
  1158. Assert.AreSame(fooBag, baz.FooBag);
  1159. Assert.AreEqual(2, baz.FooBag.Count);
  1160. bazCode = baz.Code;
  1161. }
  1162. using (ISession s = OpenSession())
  1163. {
  1164. Baz baz = (Baz) s.Load(typeof(Baz), bazCode);
  1165. object bag = baz.FooBag;
  1166. Assert.IsFalse(NHibernateUtil.IsInitialized(bag));
  1167. s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
  1168. Assert.IsTrue(NHibernateUtil.IsInitialized(bag));
  1169. Assert.AreSame(bag, baz.FooBag);
  1170. Assert.AreEqual(2, baz.FooBag.Count);
  1171. s.Delete(baz);
  1172. s.Flush();
  1173. }
  1174. }
  1175. [Test]
  1176. public void Sortables()
  1177. {
  1178. ISession s = OpenSession();
  1179. ITransaction t = s.BeginTransaction();
  1180. Baz b = new Baz();
  1181. var ss = new HashSet<Sortable>
  1182. {
  1183. new Sortable("foo"), new Sortable("bar"), new Sortable("baz")
  1184. };
  1185. b.Sortablez = ss;
  1186. s.Save(b);
  1187. s.Flush();
  1188. t.Commit();
  1189. s.Close();
  1190. s = OpenSession();
  1191. t = s.BeginTransaction();
  1192. IList result = s.CreateCriteria(typeof(Baz))
  1193. .AddOrder(Order.Asc("Name"))
  1194. .List();
  1195. b = (Baz) result[0];
  1196. Assert.IsTrue(b.Sortablez.Count == 3);
  1197. // compare the first item in the "Set" sortablez - can't reference
  1198. // the first item using b.sortablez[0] because it thinks 0 is the
  1199. // DictionaryEntry key - not the index.
  1200. foreach (Sortable sortable in b.Sortablez)
  1201. {
  1202. Assert.AreEqual(sortable.name, "bar");
  1203. break;
  1204. }
  1205. s.Flush();
  1206. t.Commit();
  1207. s.Close();
  1208. s = OpenSession();
  1209. t = s.BeginTransaction();
  1210. result = s.CreateQuery("from Baz baz left join fetch baz.Sortablez order by baz.Name asc")
  1211. .List();
  1212. b = (Baz) result[0];
  1213. Assert.IsTrue(b.Sortablez.Count == 3);
  1214. foreach (Sortable sortable in b.Sortablez)
  1215. {
  1216. Assert.AreEqual(sortable.name, "bar");
  1217. break;
  1218. }
  1219. s.Flush();
  1220. t.Commit();
  1221. s.Close();
  1222. s = OpenSession();
  1223. t = s.BeginTransaction();
  1224. result = s.CreateQuery("from Baz baz order by baz.Name asc")
  1225. .List();
  1226. b = (Baz) result[0];
  1227. Assert.IsTrue(b.Sortablez.Count == 3);
  1228. foreach (Sortable sortable in b.Sortablez)
  1229. {
  1230. Assert.AreEqual(sortable.name, "bar");
  1231. break;
  1232. }
  1233. s.Delete(b);
  1234. s.Flush();
  1235. t.Commit();
  1236. s.Close();
  1237. }
  1238. [Test]
  1239. public void FetchList()
  1240. {
  1241. ISession s = OpenSession();
  1242. Baz baz = new Baz();
  1243. s.Save(baz);
  1244. Foo foo = new Foo();
  1245. s.Save(foo);
  1246. Foo foo2 = new Foo();
  1247. s.Save(foo2);
  1248. s.Flush();
  1249. IList<Fee> list = new List<Fee>();
  1250. for (int i = 0; i < 5; i++)
  1251. {
  1252. Fee fee = new Fee();
  1253. list.Add(fee);
  1254. }
  1255. baz.Fees = list;
  1256. var result = s.CreateQuery("from Foo foo, Baz baz left join fetch baz.Fees").List();
  1257. Assert.IsTrue(NHibernateUtil.IsInitialized(((Baz)((object[])result[0])[1]).Fees));
  1258. s.Delete(foo);
  1259. s.Delete(foo2);
  1260. s.Delete(baz);
  1261. s.Flush();
  1262. s.Close();
  1263. }
  1264. [Test]
  1265. public void BagOneToMany()
  1266. {
  1267. ISession s = OpenSession();
  1268. Baz baz = new Baz();
  1269. IList<Baz> list = new List<Baz>();
  1270. baz.Bazez = list;
  1271. list.Add(new Baz());
  1272. s.Save(baz);
  1273. s.Flush();
  1274. list.Add(new Baz());
  1275. s.Flush();
  1276. list.Insert(0, new Baz());
  1277. s.Flush();
  1278. object toDelete = list[1];
  1279. list.RemoveAt(1);
  1280. s.Delete(toDelete);
  1281. s.Flush();
  1282. s.Delete(baz);
  1283. s.Flush();
  1284. s.Close();
  1285. }
  1286. [Test]
  1287. public void QueryLockMode()
  1288. {
  1289. ISession s = OpenSession();
  1290. ITransaction tx = s.BeginTransaction();
  1291. Bar bar = new Bar();
  1292. Assert.IsNull(bar.Bytes);
  1293. s.Save(bar);
  1294. Assert.IsNotNull(bar.Bytes);
  1295. s.Flush();
  1296. Assert.IsNotNull(bar.Bytes);
  1297. bar.String = "changed";
  1298. Baz baz = new Baz();
  1299. baz.Foo = bar;
  1300. s.Save(baz);
  1301. Assert.IsNotNull(bar.Bytes);
  1302. IQuery q = s.CreateQuery("from Foo foo, Bar bar");
  1303. q.SetLockMode("bar", LockMode.Upgrade);
  1304. object[] result = (object[]) q.List()[0];
  1305. Assert.IsNotNull(bar.Bytes);
  1306. object b = result[0];
  1307. Assert.IsTrue(s.GetCurrentLockMode(b) == LockMode.Write && s.GetCurrentLockMode(result[1]) == LockMode.Write);
  1308. tx.Commit();
  1309. Assert.IsNotNull(bar.Bytes);
  1310. s.Disconnect();
  1311. s.Reconnect();
  1312. tx = s.BeginTransaction();
  1313. Assert.IsNotNull(bar.Bytes);
  1314. Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
  1315. Assert.IsNotNull(bar.Bytes);
  1316. s.CreateQuery("from Foo foo").List();
  1317. Assert.IsNotNull(bar.Bytes);
  1318. Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
  1319. q = s.CreateQuery("from Foo foo");
  1320. q.SetLockMode("foo", LockMode.Read);
  1321. q.List();
  1322. Assert.AreEqual(LockMode.Read, s.GetCurrentLockMode(b));
  1323. s.Evict(baz);
  1324. tx.Commit();
  1325. s.Disconnect();
  1326. s.Reconnect();
  1327. tx = s.BeginTransaction();
  1328. Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
  1329. s.Delete(s.Load(typeof(Baz), baz.Code));
  1330. Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
  1331. tx.Commit();
  1332. s.Close();
  1333. s = OpenSession();
  1334. tx = s.BeginTransaction();
  1335. q = s.CreateQuery("from Foo foo, Bar bar, Bar bar2");
  1336. q.SetLockMode("bar", LockMode.Upgrade);
  1337. q.SetLockMode("bar2", LockMode.Read);
  1338. result = (object[]) q.List()[0];
  1339. Assert.IsTrue(s.GetCurrentLockMode(result[0]) == LockMode.Upgrade &&
  1340. s.GetCurrentLockMode(result[1]) == LockMode.Upgrade);
  1341. s.Delete(result[0]);
  1342. tx.Commit();
  1343. s.Close();
  1344. }
  1345. [Test]
  1346. public void ManyToManyBag()
  1347. {
  1348. ISession s = OpenSession();
  1349. Baz baz = new Baz();
  1350. object id = s.Save(baz);
  1351. s.Flush();
  1352. s.Close();
  1353. s = OpenSession();
  1354. baz = (Baz) s.Load(typeof(Baz), id);
  1355. baz.FooBag.Add(new Foo());
  1356. s.Flush();
  1357. s.Close();
  1358. s = OpenSession();
  1359. baz = (Baz) s.Load(typeof(Baz), id);
  1360. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooBag));
  1361. Assert.AreEqual(1, baz.FooBag.Count);
  1362. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.FooBag[0]));
  1363. s.Delete(baz);
  1364. s.Flush();
  1365. s.Close();
  1366. }
  1367. [Test]
  1368. public void IdBag()
  1369. {
  1370. ISession s = OpenSession();
  1371. Baz baz = new Baz();
  1372. s.Save(baz);
  1373. IList<Foo> l = new List<Foo>();
  1374. IList<byte[]> l2 = new List<byte[]>();
  1375. baz.IdFooBag = l;
  1376. baz.ByteBag = l2;
  1377. l.Add(new Foo());
  1378. l.Add(new Bar());
  1379. byte[] bytes = GetBytes("ffo");
  1380. l2.Add(bytes);
  1381. l2.Add(GetBytes("foo"));
  1382. s.Flush();
  1383. l.Add(new Foo());
  1384. l.Add(new Bar());
  1385. l2.Add(GetBytes("bar"));
  1386. s.Flush();
  1387. object removedObject = l[3];
  1388. l.RemoveAt(3);
  1389. s.Delete(removedObject);
  1390. bytes[1] = Convert.ToByte('o');
  1391. s.Flush();
  1392. s.Close();
  1393. s = OpenSession();
  1394. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  1395. Assert.AreEqual(3, baz.IdFooBag.Count);
  1396. Assert.AreEqual(3, baz.ByteBag.Count);
  1397. bytes = GetBytes("foobar");
  1398. foreach (object obj in baz.IdFooBag)
  1399. {
  1400. s.Delete(obj);
  1401. }
  1402. baz.IdFooBag = null;
  1403. baz.ByteBag.Add(bytes);
  1404. baz.ByteBag.Add(bytes);
  1405. s.Flush();
  1406. s.Close();
  1407. s = OpenSession();
  1408. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  1409. Assert.AreEqual(0, baz.IdFooBag.Count);
  1410. Assert.AreEqual(5, baz.ByteBag.Count);
  1411. s.Delete(baz);
  1412. s.Flush();
  1413. s.Close();
  1414. }
  1415. [Test]
  1416. public void ForceOuterJoin()
  1417. {
  1418. if (Sfi.Settings.IsOuterJoinFetchEnabled == false)
  1419. {
  1420. // don't bother to run the test if we can't test it
  1421. return;
  1422. }
  1423. ISession s = OpenSession();
  1424. Glarch g = new Glarch();
  1425. FooComponent fc = new FooComponent();
  1426. fc.Glarch = g;
  1427. FooProxy f = new Foo();
  1428. FooProxy f2 = new Foo();
  1429. f.Component = fc;
  1430. f.TheFoo = f2;
  1431. s.Save(f2);
  1432. object id = s.Save(f);
  1433. object gid = s.GetIdentifier(f.Component.Glarch);
  1434. s.Flush();
  1435. s.Close();
  1436. s = OpenSession();
  1437. f = (FooProxy) s.Load(typeof(Foo), id);
  1438. Assert.IsFalse(NHibernateUtil.IsInitialized(f));
  1439. Assert.IsTrue(NHibernateUtil.IsInitialized(f.Component.Glarch)); //outer-join="true"
  1440. Assert.IsFalse(NHibernateUtil.IsInitialized(f.TheFoo)); //outer-join="auto"
  1441. Assert.AreEqual(gid, s.GetIdentifier(f.Component.Glarch));
  1442. s.Delete(f);
  1443. s.Delete(f.TheFoo);
  1444. s.Delete(f.Component.Glarch);
  1445. s.Flush();
  1446. s.Close();
  1447. }
  1448. [Test]
  1449. public void EmptyCollection()
  1450. {
  1451. ISession s = OpenSession();
  1452. object id = s.Save(new Baz());
  1453. s.Flush();
  1454. s.Close();
  1455. s = OpenSession();
  1456. Baz baz = (Baz) s.Load(typeof(Baz), id);
  1457. Assert.IsTrue(baz.FooSet.Count == 0);
  1458. Foo foo = new Foo();
  1459. baz.FooSet.Add(foo);
  1460. s.Save(foo);
  1461. s.Flush();
  1462. s.Delete(foo);
  1463. s.Delete(baz);
  1464. s.Flush();
  1465. s.Close();
  1466. }
  1467. [Test]
  1468. public void OneToOneGenerator()
  1469. {
  1470. ISession s = OpenSession();
  1471. X x = new X();
  1472. Y y = new Y();
  1473. x.Y = y;
  1474. y.TheX = x;
  1475. object yId = s.Save(y);
  1476. object xId = s.Save(x);
  1477. Assert.AreEqual(yId, xId);
  1478. s.Flush();
  1479. Assert.IsTrue(s.Contains(y) && s.Contains(x));
  1480. s.Close();
  1481. Assert.AreEqual(x.Id, y.Id);
  1482. s = OpenSession();
  1483. x = new X();
  1484. y = new Y();
  1485. x.Y = y;
  1486. y.TheX = x;
  1487. s.Save(y);
  1488. s.Flush();
  1489. Assert.IsTrue(s.Contains(y) && s.Contains(x));
  1490. s.Close();
  1491. Assert.AreEqual(x.Id, y.Id);
  1492. s = OpenSession();
  1493. x = new X();
  1494. y = new Y();
  1495. x.Y = y;
  1496. y.TheX = x;
  1497. xId = s.Save(x);
  1498. Assert.AreEqual(xId, y.Id);
  1499. Assert.AreEqual(xId, x.Id);
  1500. s.Flush();
  1501. Assert.IsTrue(s.Contains(y) && s.Contains(x));
  1502. s.Delete("from X x");
  1503. s.Flush();
  1504. s.Close();
  1505. // check to see if Y can exist without a X
  1506. y = new Y();
  1507. s = OpenSession();
  1508. s.Save(y);
  1509. s.Flush();
  1510. s.Close();
  1511. s = OpenSession();
  1512. y = (Y) s.Load(typeof(Y), y.Id);
  1513. Assert.IsNull(y.X, "y does not need an X");
  1514. s.Delete(y);
  1515. s.Flush();
  1516. s.Close();
  1517. }
  1518. [Test]
  1519. public void Limit()
  1520. {
  1521. ISession s = OpenSession();
  1522. ITransaction txn = s.BeginTransaction();
  1523. for (int i = 0; i < 10; i++)
  1524. {
  1525. s.Save(new Foo());
  1526. }
  1527. IEnumerable enumerable = s.CreateQuery("from Foo foo")
  1528. .SetMaxResults(4)
  1529. .SetFirstResult(2)
  1530. .Enumerable();
  1531. int count = 0;
  1532. IEnumerator e = enumerable.GetEnumerator();
  1533. while (e.MoveNext())
  1534. {
  1535. object temp = e.Current;
  1536. count++;
  1537. }
  1538. Assert.AreEqual(4, count);
  1539. Assert.AreEqual(10, s.Delete("from Foo foo"));
  1540. txn.Commit();
  1541. s.Close();
  1542. }
  1543. [Test]
  1544. public void Custom()
  1545. {
  1546. GlarchProxy g = new Glarch();
  1547. Multiplicity m = new Multiplicity();
  1548. m.count = 12;
  1549. m.glarch = (Glarch) g;
  1550. g.Multiple = m;
  1551. ISession s = OpenSession();
  1552. object gid = s.Save(g);
  1553. s.Flush();
  1554. s.Close();
  1555. s = OpenSession();
  1556. g = (Glarch) s.CreateQuery("from Glarch g where g.Multiple.glarch=g and g.Multiple.count=12").List()[0];
  1557. Assert.IsNotNull(g.Multiple);
  1558. Assert.AreEqual(12, g.Multiple.count);
  1559. Assert.AreSame(g, g.Multiple.glarch);
  1560. s.Flush();
  1561. s.Close();
  1562. s = OpenSession();
  1563. g = (GlarchProxy) s.Load(typeof(Glarch), gid);
  1564. Assert.IsNotNull(g.Multiple);
  1565. Assert.AreEqual(12, g.Multiple.count);
  1566. Assert.AreSame(g, g.Multiple.glarch);
  1567. s.Delete(g);
  1568. s.Flush();
  1569. s.Close();
  1570. }
  1571. [Test]
  1572. public void SaveAddDelete()
  1573. {
  1574. ISession s = OpenSession();
  1575. Baz baz = new Baz();
  1576. baz.CascadingBars = new HashSet<BarProxy>();
  1577. s.Save(baz);
  1578. s.Flush();
  1579. baz.CascadingBars.Add(new Bar());
  1580. s.Delete(baz);
  1581. s.Flush();
  1582. s.Close();
  1583. }
  1584. [Test]
  1585. public void NamedParams()
  1586. {
  1587. Bar bar = new Bar();
  1588. Bar bar2 = new Bar();
  1589. bar.Name = "Bar";
  1590. bar2.Name = "Bar Two";
  1591. Baz baz = new Baz();
  1592. baz.CascadingBars = new HashSet<BarProxy> {bar};
  1593. bar.Baz = baz;
  1594. ISession s = OpenSession();
  1595. ITransaction txn = s.BeginTransaction();
  1596. s.Save(baz);
  1597. s.Save(bar2);
  1598. IList list =
  1599. s.CreateQuery("from Bar bar left join bar.Baz baz left join baz.CascadingBars b where bar.Name like 'Bar %'").List();
  1600. object row = list[0];
  1601. Assert.IsTrue(row is object[] && ((object[]) row).Length == 3);
  1602. IQuery q = s.CreateQuery(
  1603. "select bar, b " +
  1604. "from Bar bar " +
  1605. "left join bar.Baz baz left join baz.CascadingBars b " +
  1606. "where (bar.Name in (:nameList) or bar.Name in (:nameList)) and bar.String = :stringVal");
  1607. var nameList = new List<string> {"bar", "Bar", "Bar Two"};
  1608. q.SetParameterList("nameList", nameList);
  1609. q.SetParameter("stringVal", "a string");
  1610. list = q.List();
  1611. // a check for SAPDialect here
  1612. Assert.AreEqual(2, list.Count);
  1613. q = s.CreateQuery(
  1614. "select bar, b " +
  1615. "from Bar bar " +
  1616. "inner join bar.Baz baz inner join baz.CascadingBars b " +
  1617. "where bar.Name like 'Bar%'");
  1618. list = q.List();
  1619. Assert.AreEqual(1, list.Count);
  1620. q = s.CreateQuery(
  1621. "select bar, b " +
  1622. "from Bar bar " +
  1623. "left join bar.Baz baz left join baz.CascadingBars b " +
  1624. "where bar.Name like :name and b.Name like :name");
  1625. // add a check for HSQLDialect
  1626. q.SetString("name", "Bar%");
  1627. list = q.List();
  1628. Assert.AreEqual(1, list.Count);
  1629. s.Delete(baz);
  1630. s.Delete(bar2);
  1631. txn.Commit();
  1632. s.Close();
  1633. }
  1634. [Test]
  1635. public void VerifyParameterNamedMissing()
  1636. {
  1637. using (ISession s = OpenSession())
  1638. {
  1639. IQuery q = s.CreateQuery("select bar from Bar as bar where bar.X > :myX");
  1640. Assert.Throws<QueryException>(() =>q.List());
  1641. }
  1642. }
  1643. [Test]
  1644. public void VerifyParameterPositionalMissing()
  1645. {
  1646. using (ISession s = OpenSession())
  1647. {
  1648. IQuery q = s.CreateQuery("select bar from Bar as bar where bar.X > ?");
  1649. Assert.Throws<QueryException>(() =>q.List());
  1650. }
  1651. }
  1652. [Test]
  1653. public void VerifyParameterPositionalInQuotes()
  1654. {
  1655. using (ISession s = OpenSession())
  1656. {
  1657. IQuery q = s.CreateQuery("select bar from Bar as bar where bar.X > ? or bar.Short = 1 or bar.String = 'ff ? bb'");
  1658. q.SetInt32(0, 1);
  1659. q.List();
  1660. }
  1661. }
  1662. [Test]
  1663. public void VerifyParameterPositionalInQuotes2()
  1664. {
  1665. using (ISession s = OpenSession())
  1666. {
  1667. IQuery q = s.CreateQuery("select bar from Bar as bar where bar.String = ' ? ' or bar.String = '?'");
  1668. q.List();
  1669. }
  1670. }
  1671. [Test]
  1672. public void VerifyParameterPositionalMissing2()
  1673. {
  1674. using (ISession s = OpenSession())
  1675. {
  1676. IQuery q = s.CreateQuery("select bar from Bar as bar where bar.String = ? or bar.String = ? or bar.String = ?");
  1677. q.SetParameter(0, "bull");
  1678. q.SetParameter(2, "shit");
  1679. Assert.Throws<QueryException>(() =>q.List());
  1680. }
  1681. }
  1682. [Test]
  1683. public void Dyna()
  1684. {
  1685. ISession s = OpenSession();
  1686. GlarchProxy g = new Glarch();
  1687. g.Name = "G";
  1688. object id = s.Save(g);
  1689. s.Flush();
  1690. s.Close();
  1691. s = OpenSession();
  1692. g = (GlarchProxy) s.Load(typeof(Glarch), id);
  1693. Assert.AreEqual("G", g.Name);
  1694. Assert.AreEqual("foo", g.DynaBean["foo"]);
  1695. Assert.AreEqual(66, g.DynaBean["bar"]);
  1696. Assert.IsFalse(g is Glarch);
  1697. g.DynaBean["foo"] = "bar";
  1698. s.Flush();
  1699. s.Close();
  1700. s = OpenSession();
  1701. g = (GlarchProxy) s.Load(typeof(Glarch), id);
  1702. Assert.AreEqual("bar", g.DynaBean["foo"]);
  1703. Assert.AreEqual(66, g.DynaBean["bar"]);
  1704. g.DynaBean = null;
  1705. s.Flush();
  1706. s.Close();
  1707. s = OpenSession();
  1708. g = (GlarchProxy) s.Load(typeof(Glarch), id);
  1709. Assert.IsNull(g.DynaBean);
  1710. s.Delete(g);
  1711. s.Flush();
  1712. s.Close();
  1713. }
  1714. [Test]
  1715. public void FindByCriteria()
  1716. {
  1717. ISession s = OpenSession();
  1718. Foo f = new Foo();
  1719. s.Save(f);
  1720. s.Flush();
  1721. IList list = s.CreateCriteria(typeof(Foo))
  1722. .Add(Expression.Eq("Integer", f.Integer))
  1723. .Add(Expression.EqProperty("Integer", "Integer"))
  1724. .Add(Expression.Like("String", f.String))
  1725. .Add(Expression.In("Boolean", new bool[] {f.Boolean, f.Boolean}))
  1726. .Fetch("TheFoo")
  1727. .Fetch(SelectMode.Skip, "Baz")
  1728. .List();
  1729. Assert.IsTrue(list.Count == 1 && list[0] == f);
  1730. list = s.CreateCriteria(typeof(Foo)).Add(
  1731. Expression.Disjunction()
  1732. .Add(Expression.Eq("Integer", f.Integer))
  1733. .Add(Expression.Like("String", f.String))
  1734. .Add(Expression.Eq("Boolean", f.Boolean))
  1735. )
  1736. .Add(Expression.IsNotNull("Boolean"))
  1737. .List();
  1738. Assert.IsTrue(list.Count == 1 && list[0] == f);
  1739. ICriterion andExpression;
  1740. ICriterion orExpression;
  1741. andExpression =
  1742. Expression.And(Expression.Eq("Integer", f.Integer),
  1743. Expression.Like("String", f.String));
  1744. orExpression = Expression.Or(andExpression, Expression.Eq("Boolean", f.Boolean));
  1745. list = s.CreateCriteria(typeof(Foo))
  1746. .Add(orExpression)
  1747. .List();
  1748. Assert.IsTrue(list.Count == 1 && list[0] == f);
  1749. list = s.CreateCriteria(typeof(Foo))
  1750. .SetMaxResults(5)
  1751. .AddOrder(Order.Asc("Date"))
  1752. .List();
  1753. Assert.IsTrue(list.Count == 1 && list[0] == f);
  1754. list = s.CreateCriteria(typeof(Foo)).SetMaxResults(0).List();
  1755. Assert.AreEqual(0, list.Count);
  1756. list = s.CreateCriteria(typeof(Foo))
  1757. .SetFirstResult(1)
  1758. .AddOrder(Order.Asc("Date"))
  1759. .AddOrder(Order.Desc("String"))
  1760. .List();
  1761. Assert.AreEqual(0, list.Count);
  1762. f.TheFoo = new Foo();
  1763. s.Save(f.TheFoo);
  1764. s.Flush();
  1765. s.Close();
  1766. s = OpenSession();
  1767. list = s.CreateCriteria(typeof(Foo))
  1768. .Add(Expression.Eq("Integer", f.Integer))
  1769. .Add(Expression.Like("String", f.String))
  1770. .Add(Expression.In("Boolean", new bool[] {f.Boolean, f.Boolean}))
  1771. .Add(Expression.IsNotNull("TheFoo"))
  1772. .Fetch("TheFoo")
  1773. .Fetch(SelectMode.Skip, "Baz")
  1774. .Fetch(SelectMode.Skip, "Component.Glarch")
  1775. .Fetch(SelectMode.Skip, "TheFoo.Baz")
  1776. .Fetch(SelectMode.Skip, "TheFoo.Component.Glarch")
  1777. .List();
  1778. f = (Foo) list[0];
  1779. Assert.IsTrue(NHibernateUtil.IsInitialized(f.TheFoo));
  1780. Assert.IsFalse(NHibernateUtil.IsInitialized(f.Component.Glarch));
  1781. s.Delete(f.TheFoo);
  1782. s.Delete(f);
  1783. s.Flush();
  1784. s.Close();
  1785. }
  1786. [Test]
  1787. public void AfterDelete()
  1788. {
  1789. ISession s = OpenSession();
  1790. Foo foo = new Foo();
  1791. s.Save(foo);
  1792. s.Flush();
  1793. s.Delete(foo);
  1794. s.Save(foo);
  1795. s.Delete(foo);
  1796. s.Flush();
  1797. s.Close();
  1798. }
  1799. [Test]
  1800. public void CollectionWhere()
  1801. {
  1802. Foo foo1 = new Foo();
  1803. Foo foo2 = new Foo();
  1804. Baz baz = new Baz();
  1805. Foo[] arr = new Foo[10];
  1806. arr[0] = foo1;
  1807. arr[9] = foo2;
  1808. ISession s = OpenSession();
  1809. s.Save(foo1);
  1810. s.Save(foo2);
  1811. baz.FooArray = arr;
  1812. s.Save(baz);
  1813. s.Flush();
  1814. s.Close();
  1815. s = OpenSession();
  1816. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  1817. Assert.AreEqual(1, baz.FooArray.Length);
  1818. Assert.AreEqual(1, s.CreateQuery("from Baz baz join baz.FooArray foo").List().Count);
  1819. Assert.AreEqual(2, s.CreateQuery("from Foo foo").List().Count);
  1820. Assert.AreEqual(1, s.CreateFilter(baz.FooArray, "").List().Count);
  1821. s.Delete("from Foo foo");
  1822. s.Delete(baz);
  1823. var deleteCmd = s.Connection.CreateCommand();
  1824. deleteCmd.CommandText = "delete from FooArray where id_='" + baz.Code + "' and i>=8";
  1825. deleteCmd.CommandType = CommandType.Text;
  1826. int rows = deleteCmd.ExecuteNonQuery();
  1827. Assert.AreEqual(1, rows);
  1828. s.Flush();
  1829. s.Close();
  1830. }
  1831. [Test]
  1832. public void ComponentParent()
  1833. {
  1834. ISession s = OpenSession();
  1835. ITransaction t = s.BeginTransaction();
  1836. BarProxy bar = new Bar();
  1837. bar.Component = new FooComponent();
  1838. Baz baz = new Baz();
  1839. baz.Components = new FooComponent[] {new FooComponent(), new FooComponent()};
  1840. s.Save(bar);
  1841. s.Save(baz);
  1842. t.Commit();
  1843. s.Close();
  1844. s = OpenSession();
  1845. t = s.BeginTransaction();
  1846. bar = (BarProxy) s.Load(typeof(Bar), bar.Key);
  1847. s.Load(baz, baz.Code);
  1848. Assert.AreEqual(bar, bar.BarComponent.Parent);
  1849. Assert.IsTrue(baz.Components[0].Baz == baz && baz.Components[1].Baz == baz);
  1850. s.Delete(baz);
  1851. s.Delete(bar);
  1852. t.Commit();
  1853. s.Close();
  1854. }
  1855. [Test]
  1856. public void CollectionCache()
  1857. {
  1858. ISession s = OpenSession();
  1859. Baz baz = new Baz();
  1860. baz.SetDefaults();
  1861. s.Save(baz);
  1862. s.Flush();
  1863. s.Close();
  1864. s = OpenSession();
  1865. s.Load(typeof(Baz), baz.Code);
  1866. s.Flush();
  1867. s.Close();
  1868. s = OpenSession();
  1869. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  1870. s.Delete(baz);
  1871. s.Flush();
  1872. s.Close();
  1873. }
  1874. [Test]
  1875. //[Ignore("TimeZone Portions commented out - http://nhibernate.jira.com/browse/NH-88")]
  1876. public void AssociationId()
  1877. {
  1878. string id;
  1879. Bar bar;
  1880. MoreStuff more;
  1881. using (ISession s = OpenSession())
  1882. {
  1883. using (ITransaction t = s.BeginTransaction())
  1884. {
  1885. bar = new Bar();
  1886. id = (string) s.Save(bar);
  1887. more = new MoreStuff();
  1888. more.Name = "More Stuff";
  1889. more.IntId = 12;
  1890. more.StringId = "id";
  1891. Stuff stuf = new Stuff();
  1892. stuf.MoreStuff = more;
  1893. more.Stuffs = new List<Stuff> {stuf};
  1894. stuf.Foo = bar;
  1895. stuf.Id = 1234;
  1896. s.Save(more);
  1897. t.Commit();
  1898. }
  1899. }
  1900. using (ISession s = OpenSession())
  1901. {
  1902. using (ITransaction t = s.BeginTransaction())
  1903. {
  1904. //The special property (lowercase) id may be used to reference the unique identifier of an object. (You may also use its property name.)
  1905. string hqlString =
  1906. "from s in class Stuff where s.Foo.id = ? and s.id.Id = ? and s.MoreStuff.id.IntId = ? and s.MoreStuff.id.StringId = ?";
  1907. object[] values = new object[] {bar, (long) 1234, 12, "id"};
  1908. IType[] types = new IType[]
  1909. {
  1910. NHibernateUtil.Entity(typeof(Foo)),
  1911. NHibernateUtil.Int64,
  1912. NHibernateUtil.Int32,
  1913. NHibernateUtil.String
  1914. };
  1915. //IList results = s.List( hqlString, values, types );
  1916. IQuery q = s.CreateQuery(hqlString);
  1917. for (int i = 0; i < values.Length; i++)
  1918. {
  1919. q.SetParameter(i, values[i], types[i]);
  1920. }
  1921. IList results = q.List();
  1922. Assert.AreEqual(1, results.Count);
  1923. hqlString = "from s in class Stuff where s.Foo.id = ? and s.id.Id = ? and s.MoreStuff.Name = ?";
  1924. values = new object[] {bar, (long) 1234, "More Stuff"};
  1925. types = new IType[]
  1926. {
  1927. NHibernateUtil.Entity(typeof(Foo)),
  1928. NHibernateUtil.Int64,
  1929. NHibernateUtil.String
  1930. };
  1931. q = s.CreateQuery(hqlString);
  1932. for (int i = 0; i < values.Length; i++)
  1933. {
  1934. q.SetParameter(i, values[i], types[i]);
  1935. }
  1936. results = q.List();
  1937. Assert.AreEqual(1, results.Count);
  1938. hqlString = "from s in class Stuff where s.Foo.String is not null";
  1939. s.CreateQuery(hqlString).List();
  1940. hqlString = "from s in class Stuff where s.Foo > '0' order by s.Foo";
  1941. results = s.CreateQuery(hqlString).List();
  1942. Assert.AreEqual(1, results.Count);
  1943. t.Commit();
  1944. }
  1945. }
  1946. FooProxy foo;
  1947. using (ISession s = OpenSession())
  1948. {
  1949. using (ITransaction t = s.BeginTransaction())
  1950. {
  1951. foo = (FooProxy) s.Load(typeof(Foo), id);
  1952. s.Load(more, more);
  1953. t.Commit();
  1954. }
  1955. }
  1956. using (ISession s = OpenSession())
  1957. {
  1958. using (ITransaction t = s.BeginTransaction())
  1959. {
  1960. Stuff stuff = new Stuff();
  1961. stuff.Foo = foo;
  1962. stuff.Id = 1234;
  1963. stuff.MoreStuff = more;
  1964. s.Load(stuff, stuff);
  1965. Assert.AreEqual("More Stuff", stuff.MoreStuff.Name);
  1966. s.Delete("from ms in class MoreStuff");
  1967. s.Delete("from foo in class Foo");
  1968. t.Commit();
  1969. }
  1970. }
  1971. }
  1972. [Test]
  1973. public void CascadeSave()
  1974. {
  1975. ISession s = OpenSession();
  1976. ITransaction t = s.BeginTransaction();
  1977. Baz baz = new Baz();
  1978. IList<Fee> list = new List<Fee>();
  1979. list.Add(new Fee());
  1980. list.Add(new Fee());
  1981. baz.Fees = list;
  1982. s.Save(baz);
  1983. t.Commit();
  1984. s.Close();
  1985. s = OpenSession();
  1986. t = s.BeginTransaction();
  1987. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  1988. Assert.AreEqual(2, baz.Fees.Count);
  1989. s.Delete(baz);
  1990. Assert.IsTrue(IsEmpty(s.CreateQuery("from fee in class Fee").Enumerable()));
  1991. t.Commit();
  1992. s.Close();
  1993. }
  1994. [Test]
  1995. public void CompositeKeyPathExpressions()
  1996. {
  1997. ISession s = OpenSession();
  1998. string hql = "select fum1.Fo from fum1 in class Fum where fum1.Fo.FumString is not null";
  1999. s.CreateQuery(hql).List();
  2000. hql = "from fum1 in class Fum where fum1.Fo.FumString is not null order by fum1.Fo.FumString";
  2001. s.CreateQuery(hql).List();
  2002. if (Dialect.SupportsScalarSubSelects)
  2003. {
  2004. hql = "from fum1 in class Fum where size(fum1.Friends) = 0";
  2005. s.CreateQuery(hql).List();
  2006. hql = "from fum1 in class Fum where exists elements (fum1.Friends)";
  2007. s.CreateQuery(hql).List();
  2008. }
  2009. hql = "select elements(fum1.Friends) from fum1 in class Fum";
  2010. s.CreateQuery(hql).List();
  2011. hql = "from fum1 in class Fum, fr in elements( fum1.Friends )";
  2012. s.CreateQuery(hql).List();
  2013. s.Close();
  2014. }
  2015. [Test]
  2016. public void CollectionsInSelect()
  2017. {
  2018. ISession s = OpenSession();
  2019. ITransaction t = s.BeginTransaction();
  2020. Foo[] foos = new Foo[] {null, new Foo()};
  2021. s.Save(foos[1]);
  2022. Baz baz = new Baz();
  2023. baz.SetDefaults();
  2024. baz.FooArray = foos;
  2025. s.Save(baz);
  2026. Baz baz2 = new Baz();
  2027. baz2.SetDefaults();
  2028. s.Save(baz2);
  2029. Bar bar = new Bar();
  2030. bar.Baz = baz;
  2031. s.Save(bar);
  2032. IList list = s.CreateQuery("select new Result(foo.String, foo.Long, foo.Integer) from foo in class Foo").List();
  2033. Assert.AreEqual(2, list.Count);
  2034. Assert.IsTrue(list[0] is Result);
  2035. Assert.IsTrue(list[1] is Result);
  2036. list =
  2037. s.CreateQuery(
  2038. "select new Result( baz.Name, foo.Long, count(elements(baz.FooArray)) ) from Baz baz join baz.FooArray foo group by baz.Name, foo.Long")
  2039. .List();
  2040. Assert.AreEqual(1, list.Count);
  2041. Assert.IsTrue(list[0] is Result);
  2042. Result r = (Result) list[0];
  2043. Assert.AreEqual(baz.Name, r.Name);
  2044. Assert.AreEqual(1, r.Count);
  2045. Assert.AreEqual(foos[1].Long, r.Amount);
  2046. list =
  2047. s.CreateQuery(
  2048. "select new Result( baz.Name, max(foo.Long), count(foo) ) from Baz baz join baz.FooArray foo group by baz.Name").
  2049. List();
  2050. Assert.AreEqual(1, list.Count);
  2051. Assert.IsTrue(list[0] is Result);
  2052. r = (Result) list[0];
  2053. Assert.AreEqual(baz.Name, r.Name);
  2054. Assert.AreEqual(1, r.Count);
  2055. s.CreateQuery("select max( elements(bar.Baz.FooArray) ) from Bar as bar").List();
  2056. // the following test is disable for databases with no subselects... also for Interbase (not sure why) - comment from h2.0.3
  2057. if (Dialect.SupportsSubSelects)
  2058. {
  2059. s.CreateQuery("select count(*) from Baz as baz where 1 in indices(baz.FooArray)").List();
  2060. s.CreateQuery("select count(*) from Bar as bar where 'abc' in elements(bar.Baz.FooArray)").List();
  2061. s.CreateQuery("select count(*) from Bar as bar where 1 in indices(bar.Baz.FooArray)").List();
  2062. s.CreateQuery(
  2063. "select count(*) from Bar as bar where '1' in (from bar.Component.Glarch.ProxyArray g where g.Name='foo')").List();
  2064. // The nex query is wrong and is not present in H3.2:
  2065. // The SQL result, from Classic parser, is the same of the previous query.
  2066. // The AST parser has some problem to parse 'from g in bar.Component.Glarch.ProxyArray'
  2067. // which should be parsed as 'from bar.Component.Glarch.ProxyArray g'
  2068. //s.CreateQuery(
  2069. // "select count(*) from Bar as bar where '1' in (from g in bar.Component.Glarch.ProxyArray.elements where g.Name='foo')")
  2070. // .List();
  2071. // TODO: figure out why this is throwing an ORA-1722 error
  2072. // probably the conversion ProxyArray.id (to_number ensuring a not null value)
  2073. // Indeed, ProxyArray.id is Glarch.tha_key which is a string filled with a Guid. It does
  2074. // not fail with most engine likely because there are no results thanks to other conditions.
  2075. if (!(Dialect is Oracle8iDialect) && !(Dialect is MsSqlCeDialect) && !(Dialect is HanaDialectBase))
  2076. {
  2077. s.CreateQuery(
  2078. "select count(*) from Bar as bar join bar.Component.Glarch.ProxyArray as g where cast(g.id as Int32) in indices(bar.Baz.FooArray)").
  2079. List();
  2080. s.CreateQuery(
  2081. "select max( elements(bar.Baz.FooArray) ) from Bar as bar join bar.Component.Glarch.ProxyArray as g where cast(g.id as Int32) in indices(bar.Baz.FooArray)")
  2082. .List();
  2083. s.CreateQuery(
  2084. "select count(*) from Bar as bar left outer join bar.Component.Glarch.ProxyArray as pg where '1' in (from g in bar.Component.Glarch.ProxyArray)")
  2085. .List();
  2086. }
  2087. }
  2088. list =
  2089. s.CreateQuery("from Baz baz left join baz.FooToGlarch join fetch baz.FooArray foo left join fetch foo.TheFoo").List();
  2090. Assert.AreEqual(1, list.Count);
  2091. Assert.AreEqual(2, ((object[]) list[0]).Length);
  2092. list =
  2093. s.CreateQuery(
  2094. "select baz.Name from Bar bar inner join bar.Baz baz inner join baz.FooSet foo where baz.Name = bar.String").List();
  2095. s.CreateQuery(
  2096. "SELECT baz.Name FROM Bar AS bar INNER JOIN bar.Baz AS baz INNER JOIN baz.FooSet AS foo WHERE baz.Name = bar.String")
  2097. .List();
  2098. s.CreateQuery(
  2099. "select baz.Name from Bar bar join bar.Baz baz left outer join baz.FooSet foo where baz.Name = bar.String").List();
  2100. s.CreateQuery("select baz.Name from Bar bar join bar.Baz baz join baz.FooSet foo where baz.Name = bar.String").List();
  2101. s.CreateQuery("SELECT baz.Name FROM Bar AS bar join bar.Baz AS baz join baz.FooSet AS foo WHERE baz.Name = bar.String").List();
  2102. s.CreateQuery(
  2103. "select baz.Name from Bar bar left join bar.Baz baz left join baz.FooSet foo where baz.Name = bar.String").List();
  2104. s.CreateQuery("select foo.String from Bar bar left join bar.Baz.FooSet foo where bar.String = foo.String").List();
  2105. s.CreateQuery(
  2106. "select baz.Name from Bar bar left join bar.Baz baz left join baz.FooArray foo where baz.Name = bar.String").List();
  2107. s.CreateQuery("select foo.String from Bar bar left join bar.Baz.FooArray foo where bar.String = foo.String").List();
  2108. s.CreateQuery(
  2109. "select bar.String, foo.String from bar in class Bar inner join bar.Baz as baz inner join elements(baz.FooSet) as foo where baz.Name = 'name'")
  2110. .List();
  2111. s.CreateQuery("select foo from bar in class Bar inner join bar.Baz as baz inner join baz.FooSet as foo").List();
  2112. s.CreateQuery("select foo from bar in class Bar inner join bar.Baz.FooSet as foo").List();
  2113. s.CreateQuery(
  2114. "select bar.String, foo.String from bar in class Bar join bar.Baz as baz, elements(baz.FooSet) as foo where baz.Name = 'name'")
  2115. .List();
  2116. s.CreateQuery("select foo from bar in class Bar join bar.Baz as baz join baz.FooSet as foo").List();
  2117. s.CreateQuery("select foo from bar in class Bar join bar.Baz.FooSet as foo").List();
  2118. Assert.AreEqual(1, s.CreateQuery("from Bar bar join bar.Baz.FooArray foo").List().Count);
  2119. Assert.AreEqual(0, s.CreateQuery("from bar in class Bar, foo in elements(bar.Baz.FooSet)").List().Count);
  2120. Assert.AreEqual(1, s.CreateQuery("from bar in class Bar, foo in elements( bar.Baz.FooArray )").List().Count);
  2121. s.Delete(bar);
  2122. s.Delete(baz);
  2123. s.Delete(baz2);
  2124. s.Delete(foos[1]);
  2125. t.Commit();
  2126. s.Close();
  2127. }
  2128. [Test]
  2129. public void NewFlushing()
  2130. {
  2131. ISession s = OpenSession();
  2132. ITransaction txn = s.BeginTransaction();
  2133. Baz baz = new Baz();
  2134. baz.SetDefaults();
  2135. s.Save(baz);
  2136. s.Flush();
  2137. baz.StringArray[0] = "a new value";
  2138. IEnumerator enumer = s.CreateQuery("from baz in class Baz").Enumerable().GetEnumerator(); // no flush
  2139. Assert.IsTrue(enumer.MoveNext());
  2140. Assert.AreSame(baz, enumer.Current);
  2141. enumer = s.CreateQuery("select elements(baz.StringArray) from baz in class Baz").Enumerable().GetEnumerator();
  2142. bool found = false;
  2143. while (enumer.MoveNext())
  2144. {
  2145. if (enumer.Current.Equals("a new value"))
  2146. {
  2147. found = true;
  2148. }
  2149. }
  2150. Assert.IsTrue(found);
  2151. baz.StringArray = null;
  2152. s.CreateQuery("from baz in class Baz").Enumerable(); // no flush
  2153. enumer = s.CreateQuery("select elements(baz.StringArray) from baz in class Baz").Enumerable().GetEnumerator();
  2154. Assert.IsFalse(enumer.MoveNext());
  2155. baz.StringList.Add("1E1");
  2156. enumer = s.CreateQuery("from foo in class Foo").Enumerable().GetEnumerator(); // no flush
  2157. Assert.IsFalse(enumer.MoveNext());
  2158. enumer = s.CreateQuery("select elements(baz.StringList) from baz in class Baz").Enumerable().GetEnumerator();
  2159. found = false;
  2160. while (enumer.MoveNext())
  2161. {
  2162. if (enumer.Current.Equals("1E1"))
  2163. {
  2164. found = true;
  2165. }
  2166. }
  2167. Assert.IsTrue(found);
  2168. baz.StringList.Remove("1E1");
  2169. s.CreateQuery("select elements(baz.StringArray) from baz in class Baz").Enumerable(); //no flush
  2170. enumer = s.CreateQuery("select elements(baz.StringList) from baz in class Baz").Enumerable().GetEnumerator();
  2171. found = false;
  2172. while (enumer.MoveNext())
  2173. {
  2174. if (enumer.Current.Equals("1E1"))
  2175. {
  2176. found = true;
  2177. }
  2178. }
  2179. Assert.IsFalse(found);
  2180. IList<string> newList = new List<string>();
  2181. newList.Add("value");
  2182. baz.StringList = newList;
  2183. s.CreateQuery("from foo in class Foo").Enumerable().GetEnumerator(); //no flush
  2184. baz.StringList = null;
  2185. enumer = s.CreateQuery("select elements(baz.StringList) from baz in class Baz").Enumerable().GetEnumerator();
  2186. Assert.IsFalse(enumer.MoveNext());
  2187. s.Delete(baz);
  2188. txn.Commit();
  2189. s.Close();
  2190. }
  2191. [Test]
  2192. public void PersistCollections()
  2193. {
  2194. ISession s = OpenSession();
  2195. ITransaction txn = s.BeginTransaction();
  2196. IEnumerator enumer = s.CreateQuery("select count(*) from b in class Bar").Enumerable().GetEnumerator();
  2197. enumer.MoveNext();
  2198. Assert.AreEqual(0L, enumer.Current);
  2199. Baz baz = new Baz();
  2200. s.Save(baz);
  2201. baz.SetDefaults();
  2202. baz.StringArray = new string[] {"stuff"};
  2203. baz.CascadingBars = new HashSet<BarProxy> {new Bar()};
  2204. IDictionary<string, Glarch> sgm = new Dictionary<string, Glarch>();
  2205. sgm["a"] = new Glarch();
  2206. sgm["b"] = new Glarch();
  2207. baz.StringGlarchMap = sgm;
  2208. txn.Commit();
  2209. s.Close();
  2210. s = OpenSession();
  2211. txn = s.BeginTransaction();
  2212. baz = (Baz) ((object[]) s.CreateQuery("select baz, baz from baz in class NHibernate.DomainModel.Baz").List()[0])[1];
  2213. Assert.AreEqual(1, baz.CascadingBars.Count, "baz.CascadingBars.Count");
  2214. Foo foo = new Foo();
  2215. s.Save(foo);
  2216. Foo foo2 = new Foo();
  2217. s.Save(foo2);
  2218. baz.FooArray = new Foo[] {foo, foo, null, foo2};
  2219. baz.FooSet.Add(foo);
  2220. baz.Customs.Add(new string[] {"new", "custom"});
  2221. baz.StringArray = null;
  2222. baz.StringList[0] = "new value";
  2223. baz.StringSet = new HashSet<string>();
  2224. // NOTE: We put two items in the map, but expect only one to come back, because
  2225. // of where="..." specified in the mapping for StringGlarchMap
  2226. Assert.AreEqual(1, baz.StringGlarchMap.Count, "baz.StringGlarchMap.Count");
  2227. IList list;
  2228. // disable this for dbs with no subselects
  2229. if (Dialect.SupportsSubSelects && TestDialect.SupportsOperatorAll)
  2230. {
  2231. list = s.CreateQuery(
  2232. "select foo from foo in class NHibernate.DomainModel.Foo, baz in class NHibernate.DomainModel.Baz where foo in elements(baz.FooArray) and 3 = some elements(baz.IntArray) and 4 > all indices(baz.IntArray)")
  2233. .List();
  2234. Assert.AreEqual(2, list.Count, "collection.elements find");
  2235. }
  2236. // sapdb doesn't like distinct with binary type
  2237. //if( !(dialect is Dialect.SAPDBDialect) )
  2238. //{
  2239. list = s.CreateQuery("select distinct foo from baz in class NHibernate.DomainModel.Baz, foo in elements(baz.FooArray)").
  2240. List();
  2241. Assert.AreEqual(2, list.Count, "collection.elements find");
  2242. //}
  2243. list = s.CreateQuery("select foo from baz in class NHibernate.DomainModel.Baz, foo in elements(baz.FooSet)").List();
  2244. Assert.AreEqual(1, list.Count, "association.elements find");
  2245. txn.Commit();
  2246. s.Close();
  2247. s = OpenSession();
  2248. txn = s.BeginTransaction();
  2249. baz = (Baz)s.CreateQuery("select baz from baz in class NHibernate.DomainModel.Baz order by baz").List()[0];
  2250. Assert.AreEqual(4, baz.Customs.Count, "collection of custom types - added element");
  2251. Assert.IsNotNull(baz.Customs[0], "collection of custom types - added element");
  2252. Assert.IsNotNull(baz.Components[1].Subcomponent, "component of component in collection");
  2253. Assert.AreSame(baz, baz.Components[1].Baz);
  2254. IEnumerator fooSetEnumer = baz.FooSet.GetEnumerator();
  2255. fooSetEnumer.MoveNext();
  2256. Assert.IsTrue(((FooProxy) fooSetEnumer.Current).Key.Equals(foo.Key), "set of objects");
  2257. Assert.AreEqual(0, baz.StringArray.Length, "collection removed");
  2258. Assert.AreEqual("new value", baz.StringList[0], "changed element");
  2259. Assert.AreEqual(0, baz.StringSet.Count, "replaced set");
  2260. baz.StringSet.Add("two");
  2261. baz.StringSet.Add("one");
  2262. baz.Bag.Add("three");
  2263. txn.Commit();
  2264. s.Close();
  2265. s = OpenSession();
  2266. txn = s.BeginTransaction();
  2267. baz = (Baz)s.CreateQuery("select baz from baz in class NHibernate.DomainModel.Baz order by baz").List()[0];
  2268. Assert.AreEqual(2, baz.StringSet.Count);
  2269. int index = 0;
  2270. foreach (string key in baz.StringSet)
  2271. {
  2272. // h2.0.3 doesn't have this because the Set has a first() and last() method
  2273. index++;
  2274. if (index == 1)
  2275. {
  2276. Assert.AreEqual("one", key);
  2277. }
  2278. if (index == 2)
  2279. {
  2280. Assert.AreEqual("two", key);
  2281. }
  2282. if (index > 2)
  2283. {
  2284. Assert.Fail("should not be more than 2 items in StringSet");
  2285. }
  2286. }
  2287. Assert.AreEqual(5, baz.Bag.Count);
  2288. baz.StringSet.Remove("two");
  2289. baz.Bag.Remove("duplicate");
  2290. txn.Commit();
  2291. s.Close();
  2292. s = OpenSession();
  2293. txn = s.BeginTransaction();
  2294. baz = (Baz)s.Load(typeof(Baz), baz.Code);
  2295. Bar bar = new Bar();
  2296. Bar bar2 = new Bar();
  2297. s.Save(bar);
  2298. s.Save(bar2);
  2299. baz.TopFoos = new HashSet<Bar> { bar, bar2 };
  2300. baz.TopGlarchez = new Dictionary<char, GlarchProxy>();
  2301. GlarchProxy g = new Glarch();
  2302. s.Save(g);
  2303. baz.TopGlarchez['G'] = g;
  2304. var map = new Dictionary<Foo, GlarchProxy>();
  2305. map[bar] = g;
  2306. map[bar2] = g;
  2307. baz.FooToGlarch = map;
  2308. var map2 = new Dictionary<FooComponent, Foo>();
  2309. map2[new FooComponent("name", 123, null, null)] = bar;
  2310. map2[new FooComponent("nameName", 12, null, null)] = bar;
  2311. baz.FooComponentToFoo = map2;
  2312. var map3 = new Dictionary<Foo, GlarchProxy>();
  2313. map3[bar] = g;
  2314. baz.GlarchToFoo = map3;
  2315. txn.Commit();
  2316. s.Close();
  2317. using(s = OpenSession())
  2318. using (txn = s.BeginTransaction())
  2319. {
  2320. baz = (Baz) s.CreateQuery("select baz from baz in class NHibernate.DomainModel.Baz order by baz").List()[0];
  2321. ISession s2 = OpenSession();
  2322. ITransaction txn2 = s2.BeginTransaction();
  2323. baz = (Baz) s.CreateQuery("select baz from baz in class NHibernate.DomainModel.Baz order by baz").List()[0];
  2324. object o = baz.FooComponentToFoo[new FooComponent("name", 123, null, null)];
  2325. Assert.IsNotNull(o);
  2326. Assert.AreEqual(o, baz.FooComponentToFoo[new FooComponent("nameName", 12, null, null)]);
  2327. txn2.Commit();
  2328. s2.Close();
  2329. Assert.AreEqual(2, baz.TopFoos.Count);
  2330. Assert.AreEqual(1, baz.TopGlarchez.Count);
  2331. enumer = baz.TopFoos.GetEnumerator();
  2332. Assert.IsTrue(enumer.MoveNext());
  2333. Assert.IsNotNull(enumer.Current);
  2334. Assert.AreEqual(1, baz.StringSet.Count);
  2335. Assert.AreEqual(4, baz.Bag.Count);
  2336. Assert.AreEqual(2, baz.FooToGlarch.Count);
  2337. Assert.AreEqual(2, baz.FooComponentToFoo.Count);
  2338. Assert.AreEqual(1, baz.GlarchToFoo.Count);
  2339. enumer = baz.FooToGlarch.Keys.GetEnumerator();
  2340. for (int i = 0; i < 2; i++)
  2341. {
  2342. enumer.MoveNext();
  2343. Assert.IsTrue(enumer.Current is BarProxy);
  2344. }
  2345. enumer = baz.FooComponentToFoo.Keys.GetEnumerator();
  2346. enumer.MoveNext();
  2347. FooComponent fooComp = (FooComponent) enumer.Current;
  2348. Assert.IsTrue((fooComp.Count == 123 && fooComp.Name.Equals("name"))
  2349. || (fooComp.Count == 12 && fooComp.Name.Equals("nameName")));
  2350. Assert.IsTrue(baz.FooComponentToFoo[fooComp] is BarProxy);
  2351. Glarch g2 = new Glarch();
  2352. s.Save(g2);
  2353. g = (GlarchProxy) baz.TopGlarchez['G'];
  2354. baz.TopGlarchez['H'] = g;
  2355. baz.TopGlarchez['G'] = g2;
  2356. txn.Commit();
  2357. s.Close();
  2358. }
  2359. s = OpenSession();
  2360. txn = s.BeginTransaction();
  2361. baz = (Baz)s.CreateQuery("select baz from baz in class NHibernate.DomainModel.Baz order by baz").List()[0];
  2362. Assert.AreEqual(2, baz.TopGlarchez.Count);
  2363. txn.Commit();
  2364. s.Disconnect();
  2365. // serialize and then deserialize the session.
  2366. Stream stream = new MemoryStream();
  2367. #if NETFX
  2368. var formatter = new BinaryFormatter();
  2369. #else
  2370. var selector = new SurrogateSelector();
  2371. selector.AddSurrogate(
  2372. typeof(CultureInfo),
  2373. new StreamingContext(StreamingContextStates.All),
  2374. new CultureInfoSerializationSurrogate());
  2375. selector.ChainSelector(new SerializationHelper.SurrogateSelector());
  2376. var formatter = new BinaryFormatter
  2377. {
  2378. SurrogateSelector = selector
  2379. };
  2380. #endif
  2381. formatter.Serialize(stream, s);
  2382. s.Close();
  2383. stream.Position = 0;
  2384. s = (ISession) formatter.Deserialize(stream);
  2385. stream.Close();
  2386. s.Reconnect();
  2387. txn = s.BeginTransaction();
  2388. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  2389. s.Delete(baz);
  2390. s.Delete(baz.TopGlarchez['G']);
  2391. s.Delete(baz.TopGlarchez['H']);
  2392. var cmd = s.Connection.CreateCommand();
  2393. txn.Enlist(cmd);
  2394. cmd.CommandText = "update " + Dialect.QuoteForTableName("glarchez") + " set baz_map_id=null where baz_map_index='a'";
  2395. int rows = cmd.ExecuteNonQuery();
  2396. Assert.AreEqual(1, rows);
  2397. Assert.AreEqual(2, s.Delete("from bar in class NHibernate.DomainModel.Bar"));
  2398. FooProxy[] arr = baz.FooArray;
  2399. Assert.AreEqual(4, arr.Length);
  2400. Assert.AreEqual(foo.Key, arr[1].Key);
  2401. for (int i = 0; i < arr.Length; i++)
  2402. {
  2403. if (arr[i] != null)
  2404. {
  2405. s.Delete(arr[i]);
  2406. }
  2407. }
  2408. try
  2409. {
  2410. s.Load(typeof(Qux), (long) 666); //nonexistent
  2411. }
  2412. catch (ObjectNotFoundException onfe)
  2413. {
  2414. Assert.IsNotNull(onfe, "should not find a Qux with id of 666 when Proxies are not implemented.");
  2415. }
  2416. Assert.AreEqual(1, s.Delete("from g in class Glarch"), "Delete('from g in class Glarch')");
  2417. txn.Commit();
  2418. s.Disconnect();
  2419. // serialize and then deserialize the session.
  2420. stream = new MemoryStream();
  2421. formatter.Serialize(stream, s);
  2422. s.Close();
  2423. stream.Position = 0;
  2424. s = (ISession) formatter.Deserialize(stream);
  2425. stream.Close();
  2426. Qux nonexistentQux = (Qux) s.Load(typeof(Qux), (long) 666); //nonexistent
  2427. Assert.IsNotNull(nonexistentQux, "even though it doesn't exists should still get a proxy - no db hit.");
  2428. s.Close();
  2429. }
  2430. [Test]
  2431. public void SaveFlush()
  2432. {
  2433. ISession s = OpenSession();
  2434. Fee fee = new Fee();
  2435. s.Save(fee, "key");
  2436. fee.Fi = "blah";
  2437. s.Flush();
  2438. s.Close();
  2439. s = OpenSession();
  2440. fee = (Fee) s.Load(typeof(Fee), fee.Key);
  2441. Assert.AreEqual("blah", fee.Fi);
  2442. Assert.AreEqual("key", fee.Key);
  2443. s.Delete(fee);
  2444. s.Flush();
  2445. s.Close();
  2446. }
  2447. [Test]
  2448. public void CreateUpdate()
  2449. {
  2450. ISession s = OpenSession();
  2451. Foo foo = new Foo();
  2452. s.Save(foo);
  2453. foo.String = "dirty";
  2454. s.Flush();
  2455. s.Close();
  2456. s = OpenSession();
  2457. Foo foo2 = new Foo();
  2458. s.Load(foo2, foo.Key);
  2459. Assert.IsTrue(foo.EqualsFoo(foo2), "create-update");
  2460. s.Delete(foo2);
  2461. s.Flush();
  2462. s.Close();
  2463. s = OpenSession();
  2464. foo = new Foo();
  2465. s.Save(foo, "assignedid");
  2466. foo.String = "dirty";
  2467. s.Flush();
  2468. s.Close();
  2469. s = OpenSession();
  2470. s.Load(foo2, "assignedid");
  2471. Assert.IsTrue(foo.EqualsFoo(foo2), "create-update");
  2472. s.Delete(foo2);
  2473. s.Flush();
  2474. s.Close();
  2475. }
  2476. [Test]
  2477. public void UpdateCollections()
  2478. {
  2479. ISession s = OpenSession();
  2480. Holder baz = new Holder();
  2481. baz.Name = "123";
  2482. Foo f1 = new Foo();
  2483. Foo f2 = new Foo();
  2484. Foo f3 = new Foo();
  2485. One o = new One();
  2486. baz.Ones = new List<One>();
  2487. baz.Ones.Add(o);
  2488. Foo[] foos = new Foo[] {f1, null, f2};
  2489. baz.FooArray = foos;
  2490. // in h2.0.3 this is a Set
  2491. baz.Foos = new HashSet<Foo> { f1 };
  2492. s.Save(f1);
  2493. s.Save(f2);
  2494. s.Save(f3);
  2495. s.Save(o);
  2496. s.Save(baz);
  2497. s.Flush();
  2498. s.Close();
  2499. baz.Ones[0] = null;
  2500. baz.Ones.Add(o);
  2501. baz.Foos.Add(f2);
  2502. foos[0] = f3;
  2503. foos[1] = f1;
  2504. s = OpenSession();
  2505. s.SaveOrUpdate(baz);
  2506. s.Flush();
  2507. s.Close();
  2508. s = OpenSession();
  2509. Holder h = (Holder) s.Load(typeof(Holder), baz.Id);
  2510. Assert.IsNull(h.Ones[0]);
  2511. Assert.IsNotNull(h.Ones[1]);
  2512. Assert.IsNotNull(h.FooArray[0]);
  2513. Assert.IsNotNull(h.FooArray[1]);
  2514. Assert.IsNotNull(h.FooArray[2]);
  2515. Assert.AreEqual(2, h.Foos.Count);
  2516. // new to nh to make sure right items in right index
  2517. Assert.AreEqual(f3.Key, h.FooArray[0].Key);
  2518. Assert.AreEqual(o.Key, ((One) h.Ones[1]).Key);
  2519. Assert.AreEqual(f1.Key, h.FooArray[1].Key);
  2520. Assert.AreEqual(f2.Key, h.FooArray[2].Key);
  2521. s.Close();
  2522. baz.Foos.Remove(f1);
  2523. baz.Foos.Remove(f2);
  2524. baz.FooArray[0] = null;
  2525. baz.FooArray[1] = null;
  2526. baz.FooArray[2] = null;
  2527. s = OpenSession();
  2528. s.SaveOrUpdate(baz);
  2529. s.Delete("from f in class NHibernate.DomainModel.Foo");
  2530. baz.Ones.Remove(o);
  2531. s.Delete("from o in class NHibernate.DomainModel.One");
  2532. s.Delete(baz);
  2533. s.Flush();
  2534. s.Close();
  2535. }
  2536. [Test]
  2537. public void Load()
  2538. {
  2539. ISession s = OpenSession();
  2540. Qux q = new Qux();
  2541. s.Save(q);
  2542. BarProxy b = new Bar();
  2543. s.Save(b);
  2544. s.Flush();
  2545. s.Close();
  2546. s = OpenSession();
  2547. q = (Qux) s.Load(typeof(Qux), q.Key);
  2548. b = (BarProxy) s.Load(typeof(Foo), b.Key);
  2549. string tempKey = b.Key;
  2550. Assert.IsFalse(NHibernateUtil.IsInitialized(b), "b should have been an unitialized Proxy");
  2551. string tempString = b.BarString;
  2552. Assert.IsTrue(NHibernateUtil.IsInitialized(b), "b should have been an initialized Proxy");
  2553. BarProxy b2 = (BarProxy) s.Load(typeof(Bar), tempKey);
  2554. Qux q2 = (Qux) s.Load(typeof(Qux), q.Key);
  2555. Assert.AreSame(q, q2, "loaded same Qux");
  2556. Assert.AreSame(b, b2, "loaded same BarProxy");
  2557. s.Delete(q2);
  2558. s.Delete(b2);
  2559. s.Flush();
  2560. s.Close();
  2561. }
  2562. [Test]
  2563. public void Create()
  2564. {
  2565. ISession s = OpenSession();
  2566. Foo foo = new Foo();
  2567. s.Save(foo);
  2568. s.Flush();
  2569. s.Close();
  2570. s = OpenSession();
  2571. Foo foo2 = new Foo();
  2572. s.Load(foo2, foo.Key);
  2573. Assert.IsTrue(foo.EqualsFoo(foo2), "create");
  2574. s.Delete(foo2);
  2575. s.Flush();
  2576. s.Close();
  2577. }
  2578. [Test]
  2579. public void Callback()
  2580. {
  2581. ISession s = OpenSession();
  2582. Qux q = new Qux("0");
  2583. s.Save(q);
  2584. q.Child = (new Qux("1"));
  2585. s.Save(q.Child);
  2586. Qux q2 = new Qux("2");
  2587. q2.Child = q.Child;
  2588. Qux q3 = new Qux("3");
  2589. q.Child.Child = q3;
  2590. s.Save(q3);
  2591. Qux q4 = new Qux("4");
  2592. q4.Child = q3;
  2593. s.Save(q4);
  2594. s.Save(q2);
  2595. s.Flush();
  2596. s.Close();
  2597. s = OpenSession();
  2598. IList l = s.CreateQuery("from q in class NHibernate.DomainModel.Qux").List();
  2599. Assert.AreEqual(5, l.Count);
  2600. s.Delete(l[0]);
  2601. s.Delete(l[1]);
  2602. s.Delete(l[2]);
  2603. s.Delete(l[3]);
  2604. s.Delete(l[4]);
  2605. s.Flush();
  2606. s.Close();
  2607. }
  2608. [Test]
  2609. public void Polymorphism()
  2610. {
  2611. ISession s = OpenSession();
  2612. Bar bar = new Bar();
  2613. s.Save(bar);
  2614. bar.BarString = "bar bar";
  2615. s.Flush();
  2616. s.Close();
  2617. s = OpenSession();
  2618. FooProxy foo = (FooProxy) s.Load(typeof(Foo), bar.Key);
  2619. Assert.IsTrue(foo is BarProxy, "polymorphic");
  2620. Assert.IsTrue(((BarProxy) foo).BarString.Equals(bar.BarString), "subclass property");
  2621. s.Delete(foo);
  2622. s.Flush();
  2623. s.Close();
  2624. }
  2625. [Test]
  2626. public void RemoveContains()
  2627. {
  2628. ISession s = OpenSession();
  2629. Baz baz = new Baz();
  2630. baz.SetDefaults();
  2631. s.Save(baz);
  2632. s.Flush();
  2633. Assert.IsTrue(s.Contains(baz));
  2634. s.Evict(baz);
  2635. Assert.IsFalse(s.Contains(baz), "baz should have been evicted");
  2636. Baz baz2 = (Baz) s.Load(typeof(Baz), baz.Code);
  2637. Assert.IsFalse(baz == baz2, "should be different objects because Baz not contained in Session");
  2638. s.Delete(baz2);
  2639. s.Flush();
  2640. s.Close();
  2641. }
  2642. [Test]
  2643. public void CollectionOfSelf()
  2644. {
  2645. ISession s = OpenSession();
  2646. Bar bar = new Bar();
  2647. s.Save(bar);
  2648. // h2.0.3 was a set
  2649. bar.Abstracts = new HashSet<object>();
  2650. bar.Abstracts.Add(bar);
  2651. Bar bar2 = new Bar();
  2652. bar.Abstracts.Add(bar2);
  2653. bar.TheFoo = bar;
  2654. s.Save(bar2);
  2655. s.Flush();
  2656. s.Close();
  2657. bar.Abstracts = null;
  2658. s = OpenSession();
  2659. s.Load(bar, bar.Key);
  2660. Assert.AreEqual(2, bar.Abstracts.Count);
  2661. Assert.IsTrue(bar.Abstracts.Contains(bar), "collection contains self");
  2662. Assert.AreSame(bar, bar.TheFoo, "association to self");
  2663. if (Dialect is MySQLDialect)
  2664. {
  2665. // Break the self-reference cycle to avoid error when deleting the row
  2666. bar.TheFoo = null;
  2667. s.Flush();
  2668. }
  2669. foreach (object obj in bar.Abstracts)
  2670. {
  2671. s.Delete(obj);
  2672. }
  2673. s.Flush();
  2674. s.Close();
  2675. }
  2676. [Test]
  2677. public void Find()
  2678. {
  2679. ISession s = OpenSession();
  2680. ITransaction txn = s.BeginTransaction();
  2681. // some code commented out in h2.0.3
  2682. Bar bar = new Bar();
  2683. s.Save(bar);
  2684. bar.BarString = "bar bar";
  2685. bar.String = "xxx";
  2686. Foo foo = new Foo();
  2687. s.Save(foo);
  2688. foo.String = "foo bar";
  2689. s.Save(new Foo());
  2690. s.Save(new Bar());
  2691. IList list1 =
  2692. s.CreateQuery("select foo from foo in class NHibernate.DomainModel.Foo where foo.String='foo bar'").List();
  2693. Assert.AreEqual(1, list1.Count, "find size");
  2694. Assert.AreSame(foo, list1[0], "find ==");
  2695. IList list2 = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo order by foo.String, foo.Date").List();
  2696. Assert.AreEqual(4, list2.Count, "find size");
  2697. list1 = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where foo.class='B'").List();
  2698. Assert.AreEqual(2, list1.Count, "class special property");
  2699. list1 =
  2700. s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where foo.class=NHibernate.DomainModel.Bar").List();
  2701. Assert.AreEqual(2, list1.Count, "class special property");
  2702. list1 = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where foo.class=Bar").List();
  2703. list2 =
  2704. s.CreateQuery(
  2705. "select bar from bar in class NHibernate.DomainModel.Bar, foo in class NHibernate.DomainModel.Foo where bar.String = foo.String and not bar=foo")
  2706. .List();
  2707. Assert.AreEqual(2, list1.Count, "class special property");
  2708. Assert.AreEqual(1, list2.Count, "select from a subclass");
  2709. Trivial t = new Trivial();
  2710. s.Save(t);
  2711. txn.Commit();
  2712. s.Close();
  2713. s = OpenSession();
  2714. txn = s.BeginTransaction();
  2715. list1 = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where foo.String='foo bar'").List();
  2716. Assert.AreEqual(1, list1.Count, "find count");
  2717. // There is an interbase bug that causes null integers to return as 0, also numeric precision is <=15 -h2.0.3 comment
  2718. Assert.IsTrue(((Foo) list1[0]).EqualsFoo(foo), "find equals");
  2719. list2 = s.CreateQuery("select foo from foo in class NHibernate.DomainModel.Foo").List();
  2720. Assert.AreEqual(5, list2.Count, "find count");
  2721. IList list3 = s.CreateQuery("from bar in class NHibernate.DomainModel.Bar where bar.BarString='bar bar'").List();
  2722. Assert.AreEqual(1, list3.Count, "find count");
  2723. Assert.IsTrue(list2.Contains(list1[0]) && list2.Contains(list2[0]), "find same instance");
  2724. Assert.AreEqual(1, s.CreateQuery("from t in class NHibernate.DomainModel.Trivial").List().Count);
  2725. s.Delete("from t in class NHibernate.DomainModel.Trivial");
  2726. list2 =
  2727. s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where foo.Date = ?").SetDateTime(0,
  2728. new DateTime(1970, 01,
  2729. 01)).List();
  2730. Assert.AreEqual(4, list2.Count, "find by date");
  2731. IEnumerator enumer = list2.GetEnumerator();
  2732. while (enumer.MoveNext())
  2733. {
  2734. s.Delete(enumer.Current);
  2735. }
  2736. list2 = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List();
  2737. Assert.AreEqual(0, list2.Count, "find deleted");
  2738. txn.Commit();
  2739. s.Close();
  2740. }
  2741. [Test]
  2742. public void DeleteRecursive()
  2743. {
  2744. ISession s = OpenSession();
  2745. Foo x = new Foo();
  2746. Foo y = new Foo();
  2747. x.TheFoo = y;
  2748. y.TheFoo = x;
  2749. s.Save(x);
  2750. s.Save(y);
  2751. s.Flush();
  2752. s.Delete(y);
  2753. s.Delete(x);
  2754. s.Flush();
  2755. s.Close();
  2756. }
  2757. [Test]
  2758. public void Reachability()
  2759. {
  2760. // first for unkeyed collections
  2761. ISession s = OpenSession();
  2762. Baz baz1 = new Baz();
  2763. s.Save(baz1);
  2764. Baz baz2 = new Baz();
  2765. s.Save(baz2);
  2766. baz1.IntArray = new int[] {1, 2, 3, 4};
  2767. baz1.FooSet = new HashSet<FooProxy>();
  2768. Foo foo = new Foo();
  2769. s.Save(foo);
  2770. baz1.FooSet.Add(foo);
  2771. s.Flush();
  2772. s.Close();
  2773. s = OpenSession();
  2774. baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
  2775. baz1 = (Baz) s.Load(typeof(Baz), baz1.Code);
  2776. baz2.FooSet = baz1.FooSet;
  2777. baz1.FooSet = null;
  2778. baz2.IntArray = baz1.IntArray;
  2779. baz1.IntArray = null;
  2780. s.Flush();
  2781. s.Close();
  2782. s = OpenSession();
  2783. baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
  2784. baz1 = (Baz) s.Load(typeof(Baz), baz1.Code);
  2785. Assert.AreEqual(4, baz2.IntArray.Length, "unkeyed reachability - baz2.IntArray");
  2786. Assert.AreEqual(1, baz2.FooSet.Count, "unkeyed reachability - baz2.FooSet");
  2787. Assert.AreEqual(0, baz1.IntArray.Length, "unkeyed reachability - baz1.IntArray");
  2788. Assert.AreEqual(0, baz1.FooSet.Count, "unkeyed reachability - baz1.FooSet");
  2789. foreach (object obj in baz2.FooSet)
  2790. {
  2791. s.Delete((FooProxy) obj);
  2792. }
  2793. s.Delete(baz1);
  2794. s.Delete(baz2);
  2795. s.Flush();
  2796. s.Close();
  2797. // now for collections of collections
  2798. s = OpenSession();
  2799. baz1 = new Baz();
  2800. s.Save(baz1);
  2801. baz2 = new Baz();
  2802. s.Save(baz2);
  2803. s.Flush();
  2804. s.Close();
  2805. s = OpenSession();
  2806. baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
  2807. baz1 = (Baz) s.Load(typeof(Baz), baz1.Code);
  2808. s.Flush();
  2809. s.Close();
  2810. s = OpenSession();
  2811. baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
  2812. s.Flush();
  2813. s.Close();
  2814. s = OpenSession();
  2815. baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
  2816. baz1 = (Baz) s.Load(typeof(Baz), baz1.Code);
  2817. s.Delete(baz1);
  2818. s.Delete(baz2);
  2819. s.Flush();
  2820. s.Close();
  2821. // now for keyed collections
  2822. s = OpenSession();
  2823. baz1 = new Baz();
  2824. s.Save(baz1);
  2825. baz2 = new Baz();
  2826. s.Save(baz2);
  2827. Foo foo1 = new Foo();
  2828. Foo foo2 = new Foo();
  2829. s.Save(foo1);
  2830. s.Save(foo2);
  2831. baz1.FooArray = new Foo[] {foo1, null, foo2};
  2832. baz1.StringDateMap = new Dictionary<string, DateTime?>();
  2833. baz1.StringDateMap["today"] = DateTime.Today;
  2834. baz1.StringDateMap["foo"] = null;
  2835. baz1.StringDateMap["tomm"] = new DateTime(DateTime.Today.Ticks + (new TimeSpan(1, 0, 0, 0, 0)).Ticks);
  2836. s.Flush();
  2837. s.Close();
  2838. s = OpenSession();
  2839. baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
  2840. baz1 = (Baz) s.Load(typeof(Baz), baz1.Code);
  2841. baz2.FooArray = baz1.FooArray;
  2842. baz1.FooArray = null;
  2843. baz2.StringDateMap = baz1.StringDateMap;
  2844. baz1.StringDateMap = null;
  2845. s.Flush();
  2846. s.Close();
  2847. s = OpenSession();
  2848. baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
  2849. baz1 = (Baz) s.Load(typeof(Baz), baz1.Code);
  2850. Assert.AreEqual(3, baz2.StringDateMap.Count, "baz2.StringDateMap count - reachability");
  2851. Assert.AreEqual(3, baz2.FooArray.Length, "baz2.FooArray length - reachability");
  2852. Assert.AreEqual(0, baz1.StringDateMap.Count, "baz1.StringDateMap count - reachability");
  2853. Assert.AreEqual(0, baz1.FooArray.Length, "baz1.FooArray length - reachability");
  2854. Assert.IsNull(baz2.FooArray[1], "null element");
  2855. Assert.IsNotNull(baz2.StringDateMap["today"], "today non-null element");
  2856. Assert.IsNotNull(baz2.StringDateMap["tomm"], "tomm non-null element");
  2857. Assert.IsNull(baz2.StringDateMap["foo"], "foo is null element");
  2858. s.Delete(baz2.FooArray[0]);
  2859. s.Delete(baz2.FooArray[2]);
  2860. s.Delete(baz1);
  2861. s.Delete(baz2);
  2862. s.Flush();
  2863. s.Close();
  2864. }
  2865. [Test]
  2866. public void PersistentLifecycle()
  2867. {
  2868. ISession s = OpenSession();
  2869. Qux q = new Qux();
  2870. s.Save(q);
  2871. q.Stuff = "foo bar baz qux";
  2872. s.Flush();
  2873. s.Close();
  2874. s = OpenSession();
  2875. q = (Qux) s.Load(typeof(Qux), q.Key);
  2876. Assert.IsTrue(q.Created, "lifecycle create");
  2877. Assert.IsTrue(q.Loaded, "lifecycle load");
  2878. Assert.IsNotNull(q.Foo, "lifecycle subobject");
  2879. s.Delete(q);
  2880. Assert.IsTrue(q.Deleted, "lifecyle delete");
  2881. s.Flush();
  2882. s.Close();
  2883. s = OpenSession();
  2884. Assert.AreEqual(0, s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List().Count, "subdeletion");
  2885. s.Flush();
  2886. s.Close();
  2887. }
  2888. [Test]
  2889. public void Enumerable()
  2890. {
  2891. // this test used to be called Iterators()
  2892. ISession s = OpenSession();
  2893. for (int i = 0; i < 10; i++)
  2894. {
  2895. Qux q = new Qux();
  2896. object qid = s.Save(q);
  2897. Assert.IsNotNull(q, "q is not null");
  2898. Assert.IsNotNull(qid, "qid is not null");
  2899. }
  2900. s.Flush();
  2901. s.Close();
  2902. s = OpenSession();
  2903. IEnumerator enumer =
  2904. s.CreateQuery("from q in class NHibernate.DomainModel.Qux where q.Stuff is null").Enumerable().GetEnumerator();
  2905. int count = 0;
  2906. while (enumer.MoveNext())
  2907. {
  2908. Qux q = (Qux) enumer.Current;
  2909. q.Stuff = "foo";
  2910. // can't remove item from IEnumerator in .net
  2911. if (count == 0 || count == 5)
  2912. {
  2913. s.Delete(q);
  2914. }
  2915. count++;
  2916. }
  2917. Assert.AreEqual(10, count, "found 10 items");
  2918. s.Flush();
  2919. s.Close();
  2920. s = OpenSession();
  2921. Assert.AreEqual(8,
  2922. s.Delete("from q in class NHibernate.DomainModel.Qux where q.Stuff=?", "foo", NHibernateUtil.String),
  2923. "delete by query");
  2924. s.Flush();
  2925. s.Close();
  2926. s = OpenSession();
  2927. enumer = s.CreateQuery("from q in class NHibernate.DomainModel.Qux").Enumerable().GetEnumerator();
  2928. Assert.IsFalse(enumer.MoveNext(), "no items in enumerator");
  2929. s.Flush();
  2930. s.Close();
  2931. }
  2932. /// <summary>
  2933. /// Adding a test to verify that a database action can occur in the
  2934. /// middle of an Enumeration. Under certain conditions an open
  2935. /// DataReader can be kept open and cause any other action to fail.
  2936. /// </summary>
  2937. [Test]
  2938. public void EnumerableDisposable()
  2939. {
  2940. // this test used to be called Iterators()
  2941. ISession s = OpenSession();
  2942. for (long i = 0L; i < 10L; i++)
  2943. {
  2944. Simple simple = new Simple();
  2945. simple.Count = (int) i;
  2946. s.Save(simple, i);
  2947. Assert.IsNotNull(simple, "simple is not null");
  2948. }
  2949. s.Flush();
  2950. s.Close();
  2951. s = OpenSession();
  2952. ITransaction t = s.BeginTransaction();
  2953. Simple simp = (Simple) s.Load(typeof(Simple), 8L);
  2954. // the reader under the enum has to still be a SqlDataReader (subst db name here) and
  2955. // can't be a NDataReader - the best way to get this result is to query on just a property
  2956. // of an object. If the query is "from Simple as s" then it will be converted to a NDataReader
  2957. // on the MoveNext so it can get the object from the id - thus needing another open DataReader so
  2958. // it must convert to an NDataReader.
  2959. IEnumerable enumer = s.CreateQuery("select s.Count from Simple as s").Enumerable();
  2960. //int count = 0;
  2961. foreach (object obj in enumer)
  2962. {
  2963. if ((int) obj == 7)
  2964. {
  2965. break;
  2966. }
  2967. }
  2968. // if Enumerable doesn't implement Dispose() then the test fails on this line
  2969. t.Commit();
  2970. s.Close();
  2971. s = OpenSession();
  2972. Assert.AreEqual(10,
  2973. s.Delete("from Simple"),
  2974. "delete by query");
  2975. s.Flush();
  2976. s.Close();
  2977. s = OpenSession();
  2978. enumer = s.CreateQuery("from Simple").Enumerable();
  2979. Assert.IsFalse(enumer.GetEnumerator().MoveNext(), "no items in enumerator");
  2980. s.Flush();
  2981. s.Close();
  2982. }
  2983. [Test]
  2984. public void Versioning()
  2985. {
  2986. GlarchProxy g = new Glarch();
  2987. GlarchProxy g2 = new Glarch();
  2988. object gid, g2id;
  2989. using (ISession s = OpenSession())
  2990. using (ITransaction txn = s.BeginTransaction())
  2991. {
  2992. s.Save(g);
  2993. s.Save(g2);
  2994. gid = s.GetIdentifier(g);
  2995. g2id = s.GetIdentifier(g2);
  2996. g.Name = "glarch";
  2997. txn.Commit();
  2998. }
  2999. Sfi.Evict(typeof(Glarch));
  3000. using (ISession s = OpenSession())
  3001. using (ITransaction txn = s.BeginTransaction())
  3002. {
  3003. g = (GlarchProxy) s.Load(typeof(Glarch), gid);
  3004. s.Lock(g, LockMode.Upgrade);
  3005. g2 = (GlarchProxy) s.Load(typeof(Glarch), g2id);
  3006. // Versions are initialized to 1 in NH (not to 0 like in Hibernate)
  3007. Assert.AreEqual(2, g.Version, "version");
  3008. Assert.AreEqual(2, g.DerivedVersion, "version");
  3009. Assert.AreEqual(1, g2.Version, "version");
  3010. g.Name = "foo";
  3011. Assert.IsTrue(
  3012. s.CreateQuery("from g in class Glarch where g.Version=3").List().Count == 1,
  3013. "find by version"
  3014. );
  3015. g.Name = "bar";
  3016. txn.Commit();
  3017. }
  3018. Sfi.Evict(typeof(Glarch));
  3019. using (ISession s = OpenSession())
  3020. using (ITransaction txn = s.BeginTransaction())
  3021. {
  3022. g = (GlarchProxy) s.Load(typeof(Glarch), gid);
  3023. g2 = (GlarchProxy) s.Load(typeof(Glarch), g2id);
  3024. Assert.AreEqual(4, g.Version, "version");
  3025. Assert.AreEqual(4, g.DerivedVersion, "version");
  3026. Assert.AreEqual(1, g2.Version, "version");
  3027. g.Next = null;
  3028. g2.Next = g;
  3029. s.Delete(g2);
  3030. s.Delete(g);
  3031. txn.Commit();
  3032. }
  3033. }
  3034. // The test below is commented out, it fails because Glarch is mapped with optimistic-lock="dirty"
  3035. // which means that the version column is not used for optimistic locking.
  3036. /*
  3037. [Test]
  3038. public void Versioning()
  3039. {
  3040. object gid, g2id;
  3041. using( ISession s = OpenSession() )
  3042. {
  3043. GlarchProxy g = new Glarch();
  3044. s.Save(g);
  3045. GlarchProxy g2 = new Glarch();
  3046. s.Save(g2);
  3047. gid = s.GetIdentifier(g);
  3048. g2id = s.GetIdentifier(g2);
  3049. g.Name = "glarch";
  3050. s.Flush();
  3051. }
  3052. GlarchProxy gOld;
  3053. // grab a version of g that is old and hold onto it until later
  3054. // for a StaleObjectException check.
  3055. using( ISession sOld = OpenSession() )
  3056. {
  3057. gOld = (GlarchProxy)sOld.Get( typeof(Glarch), gid );
  3058. // want gOld to be initialized so later I can change a property
  3059. Assert.IsTrue( NHibernateUtil.IsInitialized( gOld ), "should be initialized" );
  3060. }
  3061. using( ISession s = OpenSession() )
  3062. {
  3063. GlarchProxy g = (GlarchProxy)s.Load( typeof(Glarch), gid );
  3064. s.Lock(g, LockMode.Upgrade);
  3065. GlarchProxy g2 = (GlarchProxy)s.Load( typeof(Glarch), g2id );
  3066. Assert.AreEqual(1, g.Version, "g's version");
  3067. Assert.AreEqual(1, g.DerivedVersion, "g's derived version");
  3068. Assert.AreEqual(0, g2.Version, "g2's version");
  3069. g.Name = "foo";
  3070. Assert.AreEqual(1, s.CreateQuery("from g in class NHibernate.DomainModel.Glarch where g.Version=2").List().Count, "find by version");
  3071. g.Name = "bar";
  3072. s.Flush();
  3073. }
  3074. // now that g has been changed verify that we can't go back and update
  3075. // it with an old version of g
  3076. bool isStale = false;
  3077. using( ISession sOld = OpenSession() )
  3078. {
  3079. gOld.Name = "should not update";
  3080. try
  3081. {
  3082. sOld.Update( gOld, gid );
  3083. sOld.Flush();
  3084. //sOld.Close();
  3085. sOld.Dispose();
  3086. }
  3087. catch(Exception e)
  3088. {
  3089. Exception exc = e;
  3090. while( exc!=null )
  3091. {
  3092. if( exc is StaleObjectStateException )
  3093. {
  3094. isStale = true;
  3095. break;
  3096. }
  3097. exc = exc.InnerException;
  3098. }
  3099. }
  3100. }
  3101. Assert.IsTrue( isStale, "Did not catch a stale object exception when updating an old GlarchProxy." );
  3102. using( ISession s = OpenSession() )
  3103. {
  3104. GlarchProxy g = (GlarchProxy)s.Load( typeof(Glarch), gid );
  3105. GlarchProxy g2 = (GlarchProxy)s.Load( typeof(Glarch), g2id );
  3106. Assert.AreEqual(3, g.Version, "g's version");
  3107. Assert.AreEqual(3, g.DerivedVersion, "g's derived version");
  3108. Assert.AreEqual(0, g2.Version, "g2's version");
  3109. g.Next = null;
  3110. g2.Next = g;
  3111. s.Delete(g2);
  3112. s.Delete(g);
  3113. s.Flush();
  3114. //s.Close();
  3115. }
  3116. }
  3117. */
  3118. [Test]
  3119. public void VersionedCollections()
  3120. {
  3121. ISession s = OpenSession();
  3122. GlarchProxy g = new Glarch();
  3123. s.Save(g);
  3124. g.ProxyArray = new GlarchProxy[] {g};
  3125. string gid = (string) s.GetIdentifier(g);
  3126. IList<string> list = new List<string>();
  3127. list.Add("foo");
  3128. g.Strings = list;
  3129. // <sets> in h2.0.3
  3130. g.ProxySet = new HashSet<GlarchProxy> { g };
  3131. s.Flush();
  3132. s.Close();
  3133. s = OpenSession();
  3134. g = (GlarchProxy) s.Load(typeof(Glarch), gid);
  3135. Assert.AreEqual(1, g.Strings.Count);
  3136. Assert.AreEqual(1, g.ProxyArray.Length);
  3137. Assert.AreEqual(1, g.ProxySet.Count);
  3138. Assert.AreEqual(2, g.Version, "version collection before");
  3139. s.Flush();
  3140. s.Close();
  3141. s = OpenSession();
  3142. g = (GlarchProxy) s.Load(typeof(Glarch), gid);
  3143. Assert.AreEqual("foo", g.Strings[0]);
  3144. Assert.AreSame(g, g.ProxyArray[0]);
  3145. IEnumerator enumer = g.ProxySet.GetEnumerator();
  3146. enumer.MoveNext();
  3147. Assert.AreSame(g, enumer.Current);
  3148. Assert.AreEqual(2, g.Version, "versioned collection before");
  3149. s.Flush();
  3150. s.Close();
  3151. s = OpenSession();
  3152. g = (GlarchProxy) s.Load(typeof(Glarch), gid);
  3153. Assert.AreEqual(2, g.Version, "versioned collection before");
  3154. g.Strings.Add("bar");
  3155. s.Flush();
  3156. s.Close();
  3157. s = OpenSession();
  3158. g = (GlarchProxy) s.Load(typeof(Glarch), gid);
  3159. Assert.AreEqual(3, g.Version, "versioned collection after");
  3160. Assert.AreEqual(2, g.Strings.Count, "versioned collection after");
  3161. g.ProxyArray = null;
  3162. s.Flush();
  3163. s.Close();
  3164. s = OpenSession();
  3165. g = (GlarchProxy) s.Load(typeof(Glarch), gid);
  3166. Assert.AreEqual(4, g.Version, "versioned collection after");
  3167. Assert.AreEqual(0, g.ProxyArray.Length, "version collection after");
  3168. g.FooComponents = new List<FooComponent>();
  3169. g.ProxyArray = null;
  3170. s.Flush();
  3171. s.Close();
  3172. s = OpenSession();
  3173. g = (GlarchProxy) s.Load(typeof(Glarch), gid);
  3174. Assert.AreEqual(5, g.Version, "versioned collection after");
  3175. s.Delete(g);
  3176. s.Flush();
  3177. s.Close();
  3178. }
  3179. [Test]
  3180. public void RecursiveLoad()
  3181. {
  3182. // Non polymorphisc class (there is an implementation optimization
  3183. // being tested here) - from h2.0.3 - what does that mean?
  3184. ISession s = OpenSession();
  3185. ITransaction txn = s.BeginTransaction();
  3186. GlarchProxy last = new Glarch();
  3187. s.Save(last);
  3188. last.Order = 0;
  3189. for (int i = 0; i < 5; i++)
  3190. {
  3191. GlarchProxy next = new Glarch();
  3192. s.Save(next);
  3193. last.Next = next;
  3194. last = next;
  3195. last.Order = (short) (i + 1);
  3196. }
  3197. IEnumerator enumer = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch").Enumerable().GetEnumerator();
  3198. while (enumer.MoveNext())
  3199. {
  3200. object objTemp = enumer.Current;
  3201. }
  3202. IList list = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch").List();
  3203. Assert.AreEqual(6, list.Count, "recursive find");
  3204. txn.Commit();
  3205. s.Close();
  3206. s = OpenSession();
  3207. txn = s.BeginTransaction();
  3208. list = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch").List();
  3209. Assert.AreEqual(6, list.Count, "recursive iter");
  3210. list = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch where g.Next is not null").List();
  3211. Assert.AreEqual(5, list.Count, "exclude the null next");
  3212. txn.Commit();
  3213. s.Close();
  3214. s = OpenSession();
  3215. txn = s.BeginTransaction();
  3216. enumer =
  3217. s.CreateQuery("from g in class NHibernate.DomainModel.Glarch order by g.Order asc").Enumerable().GetEnumerator();
  3218. while (enumer.MoveNext())
  3219. {
  3220. GlarchProxy g = (GlarchProxy) enumer.Current;
  3221. Assert.IsNotNull(g, "not null");
  3222. // no equiv in .net - so ran a delete query
  3223. // iter.remove();
  3224. }
  3225. s.Delete("from NHibernate.DomainModel.Glarch as g");
  3226. txn.Commit();
  3227. s.Close();
  3228. // same thing bug using polymorphic class (no optimization possible)
  3229. s = OpenSession();
  3230. txn = s.BeginTransaction();
  3231. FooProxy flast = new Bar();
  3232. s.Save(flast);
  3233. for (int i = 0; i < 5; i++)
  3234. {
  3235. FooProxy foo = new Bar();
  3236. s.Save(foo);
  3237. flast.TheFoo = foo;
  3238. flast = flast.TheFoo;
  3239. flast.String = "foo" + (i + 1);
  3240. }
  3241. enumer = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").Enumerable().GetEnumerator();
  3242. while (enumer.MoveNext())
  3243. {
  3244. object objTemp = enumer.Current;
  3245. }
  3246. list = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List();
  3247. Assert.AreEqual(6, list.Count, "recursive find");
  3248. txn.Commit();
  3249. s.Close();
  3250. s = OpenSession();
  3251. txn = s.BeginTransaction();
  3252. list = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List();
  3253. Assert.AreEqual(6, list.Count, "recursive iter");
  3254. enumer = list.GetEnumerator();
  3255. while (enumer.MoveNext())
  3256. {
  3257. Assert.IsTrue(enumer.Current is BarProxy, "polymorphic recursive load");
  3258. }
  3259. txn.Commit();
  3260. s.Close();
  3261. s = OpenSession();
  3262. txn = s.BeginTransaction();
  3263. enumer =
  3264. s.CreateQuery("from foo in class NHibernate.DomainModel.Foo order by foo.String asc").Enumerable().GetEnumerator();
  3265. string currentString = String.Empty;
  3266. while (enumer.MoveNext())
  3267. {
  3268. BarProxy bar = (BarProxy) enumer.Current;
  3269. string theString = bar.String;
  3270. Assert.IsNotNull(bar, "not null");
  3271. if (currentString != String.Empty)
  3272. {
  3273. Assert.IsTrue(theString.CompareTo(currentString) >= 0, "not in asc order");
  3274. }
  3275. currentString = theString;
  3276. // no equiv in .net - so made a hql delete
  3277. // iter.remove();
  3278. }
  3279. s.Delete("from NHibernate.DomainModel.Foo as foo");
  3280. txn.Commit();
  3281. s.Close();
  3282. }
  3283. // Not ported - testScrollableIterator - ScrollableResults are not supported by NH,
  3284. // since they rely on the underlying ResultSet to support scrolling, and ADO.NET
  3285. // DbDataReaders do not support it.
  3286. [Test]
  3287. public void MultiColumnQueries()
  3288. {
  3289. ISession s = OpenSession();
  3290. ITransaction txn = s.BeginTransaction();
  3291. Foo foo = new Foo();
  3292. s.Save(foo);
  3293. Foo foo1 = new Foo();
  3294. s.Save(foo1);
  3295. foo.TheFoo = foo1;
  3296. IList l =
  3297. s.CreateQuery(
  3298. "select parent, child from parent in class NHibernate.DomainModel.Foo, child in class NHibernate.DomainModel.Foo where parent.TheFoo = child")
  3299. .List();
  3300. Assert.AreEqual(1, l.Count, "multi-column find");
  3301. IEnumerator rs;
  3302. object[] row;
  3303. if (TestDialect.SupportsCountDistinct)
  3304. {
  3305. rs =
  3306. s.CreateQuery(
  3307. "select count(distinct child.id), count(distinct parent.id) from parent in class NHibernate.DomainModel.Foo, child in class NHibernate.DomainModel.Foo where parent.TheFoo = child")
  3308. .Enumerable().GetEnumerator();
  3309. Assert.IsTrue(rs.MoveNext());
  3310. row = (object[]) rs.Current;
  3311. Assert.AreEqual(1, row[0], "multi-column count");
  3312. Assert.AreEqual(1, row[1], "multi-column count");
  3313. Assert.IsFalse(rs.MoveNext());
  3314. }
  3315. rs =
  3316. s.CreateQuery(
  3317. "select child.id, parent.id, child.Long from parent in class NHibernate.DomainModel.Foo, child in class NHibernate.DomainModel.Foo where parent.TheFoo = child")
  3318. .Enumerable().GetEnumerator();
  3319. Assert.IsTrue(rs.MoveNext());
  3320. row = (object[]) rs.Current;
  3321. Assert.AreEqual(foo.TheFoo.Key, row[0], "multi-column id");
  3322. Assert.AreEqual(foo.Key, row[1], "multi-column id");
  3323. Assert.AreEqual(foo.TheFoo.Long, row[2], "multi-column property");
  3324. Assert.IsFalse(rs.MoveNext());
  3325. rs =
  3326. s.CreateQuery(
  3327. "select child.id, parent.id, child.Long, child, parent.TheFoo from parent in class NHibernate.DomainModel.Foo, child in class NHibernate.DomainModel.Foo where parent.TheFoo = child")
  3328. .Enumerable().GetEnumerator();
  3329. Assert.IsTrue(rs.MoveNext());
  3330. row = (object[]) rs.Current;
  3331. Assert.AreEqual(foo.TheFoo.Key, row[0], "multi-column id");
  3332. Assert.AreEqual(foo.Key, row[1], "multi-column id");
  3333. Assert.AreEqual(foo.TheFoo.Long, row[2], "multi-column property");
  3334. Assert.AreSame(foo.TheFoo, row[3], "multi-column object");
  3335. Assert.AreSame(row[3], row[4], "multi-column same object");
  3336. Assert.IsFalse(rs.MoveNext());
  3337. row = (object[]) l[0];
  3338. Assert.AreSame(foo, row[0], "multi-column find");
  3339. Assert.AreSame(foo.TheFoo, row[1], "multi-column find");
  3340. txn.Commit();
  3341. s.Close();
  3342. s = OpenSession();
  3343. txn = s.BeginTransaction();
  3344. IEnumerator enumer =
  3345. s.CreateQuery(
  3346. "select parent, child from parent in class NHibernate.DomainModel.Foo, child in class NHibernate.DomainModel.Foo where parent.TheFoo = child and parent.String='a string'")
  3347. .Enumerable().GetEnumerator();
  3348. int deletions = 0;
  3349. while (enumer.MoveNext())
  3350. {
  3351. object[] pnc = (object[]) enumer.Current;
  3352. s.Delete(pnc[0]);
  3353. s.Delete(pnc[1]);
  3354. deletions++;
  3355. }
  3356. Assert.AreEqual(1, deletions, "multi-column enumerate");
  3357. txn.Commit();
  3358. s.Close();
  3359. }
  3360. [Test]
  3361. public void DeleteTransient()
  3362. {
  3363. Fee fee = new Fee();
  3364. ISession s = OpenSession();
  3365. ITransaction tx = s.BeginTransaction();
  3366. s.Save(fee);
  3367. s.Flush();
  3368. fee.Count = 123;
  3369. tx.Commit();
  3370. s.Close();
  3371. s = OpenSession();
  3372. tx = s.BeginTransaction();
  3373. s.Delete(fee);
  3374. tx.Commit();
  3375. s.Close();
  3376. s = OpenSession();
  3377. tx = s.BeginTransaction();
  3378. Assert.AreEqual(0, s.CreateQuery("from fee in class Fee").List().Count);
  3379. tx.Commit();
  3380. s.Close();
  3381. }
  3382. [Test]
  3383. public void DeleteUpdatedTransient()
  3384. {
  3385. Fee fee = new Fee();
  3386. Fee fee2 = new Fee();
  3387. fee2.AnotherFee = fee;
  3388. using (ISession s = OpenSession())
  3389. {
  3390. using (ITransaction tx = s.BeginTransaction())
  3391. {
  3392. s.Save(fee);
  3393. s.Save(fee2);
  3394. s.Flush();
  3395. fee.Count = 123;
  3396. tx.Commit();
  3397. }
  3398. }
  3399. using (ISession s = OpenSession())
  3400. {
  3401. using (ITransaction tx = s.BeginTransaction())
  3402. {
  3403. s.Update(fee);
  3404. //fee2.AnotherFee = null;
  3405. s.Update(fee2);
  3406. s.Delete(fee);
  3407. s.Delete(fee2);
  3408. tx.Commit();
  3409. }
  3410. }
  3411. using (ISession s = OpenSession())
  3412. {
  3413. using (ITransaction tx = s.BeginTransaction())
  3414. {
  3415. Assert.AreEqual(0, s.CreateQuery("from fee in class Fee").List().Count);
  3416. tx.Commit();
  3417. }
  3418. }
  3419. }
  3420. [Test]
  3421. public void UpdateOrder()
  3422. {
  3423. Fee fee1, fee2, fee3;
  3424. using (ISession s = OpenSession())
  3425. {
  3426. fee1 = new Fee();
  3427. s.Save(fee1);
  3428. fee2 = new Fee();
  3429. fee1.TheFee = fee2;
  3430. fee2.TheFee = fee1;
  3431. fee2.Fees = new HashSet<string>();
  3432. fee3 = new Fee();
  3433. fee3.TheFee = fee1;
  3434. fee3.AnotherFee = fee2;
  3435. fee2.AnotherFee = fee3;
  3436. s.Save(fee3);
  3437. s.Save(fee2);
  3438. s.Flush();
  3439. }
  3440. using (ISession s = OpenSession())
  3441. {
  3442. fee1.Count = 10;
  3443. fee2.Count = 20;
  3444. fee3.Count = 30;
  3445. s.Update(fee1);
  3446. s.Update(fee2);
  3447. s.Update(fee3);
  3448. s.Flush();
  3449. s.Delete(fee1);
  3450. s.Delete(fee2);
  3451. s.Delete(fee3);
  3452. s.Flush();
  3453. }
  3454. using (ISession s = OpenSession())
  3455. {
  3456. using (ITransaction tx = s.BeginTransaction())
  3457. {
  3458. Assert.AreEqual(0, s.CreateQuery("from fee in class Fee").List().Count);
  3459. tx.Commit();
  3460. }
  3461. }
  3462. }
  3463. [Test]
  3464. public void UpdateFromTransient()
  3465. {
  3466. if (!TestDialect.SupportsBatchingDependentDML)
  3467. Assert.Ignore($"Dialect {Dialect} does not support batching of dependent DML (fee update on related fee)");
  3468. ISession s = OpenSession();
  3469. Fee fee1 = new Fee();
  3470. s.Save(fee1);
  3471. Fee fee2 = new Fee();
  3472. fee1.TheFee = fee2;
  3473. fee2.TheFee = fee1;
  3474. fee2.Fees = new HashSet<string>();
  3475. Fee fee3 = new Fee();
  3476. fee3.TheFee = fee1;
  3477. fee3.AnotherFee = fee2;
  3478. fee2.AnotherFee = fee3;
  3479. s.Save(fee3);
  3480. s.Save(fee2);
  3481. s.Flush();
  3482. s.Close();
  3483. fee1.Fi = "changed";
  3484. s = OpenSession();
  3485. s.SaveOrUpdate(fee1);
  3486. s.Flush();
  3487. s.Close();
  3488. Qux q = new Qux("quxxy");
  3489. fee1.Qux = q;
  3490. s = OpenSession();
  3491. s.SaveOrUpdate(fee1);
  3492. s.Flush();
  3493. s.Close();
  3494. s = OpenSession();
  3495. fee1 = (Fee) s.Load(typeof(Fee), fee1.Key);
  3496. Assert.AreEqual("changed", fee1.Fi, "updated from transient");
  3497. Assert.IsNotNull(fee1.Qux, "unsaved-value");
  3498. s.Delete(fee1.Qux);
  3499. fee1.Qux = null;
  3500. s.Flush();
  3501. s.Close();
  3502. fee2.Fi = "CHANGED";
  3503. fee2.Fees.Add("an element");
  3504. fee1.Fi = "changed again";
  3505. s = OpenSession();
  3506. s.SaveOrUpdate(fee2);
  3507. s.Update(fee1, fee1.Key);
  3508. s.Flush();
  3509. s.Close();
  3510. s = OpenSession();
  3511. Fee fee = new Fee();
  3512. s.Load(fee, fee2.Key);
  3513. fee1 = (Fee) s.Load(typeof(Fee), fee1.Key);
  3514. Assert.AreEqual("changed again", fee1.Fi, "updated from transient");
  3515. Assert.AreEqual("CHANGED", fee.Fi, "updated from transient");
  3516. Assert.IsTrue(fee.Fees.Contains("an element"), "updated collection");
  3517. s.Flush();
  3518. s.Close();
  3519. fee.Fees.Clear();
  3520. fee.Fees.Add("new element");
  3521. fee1.TheFee = null;
  3522. s = OpenSession();
  3523. s.SaveOrUpdate(fee);
  3524. s.SaveOrUpdate(fee1);
  3525. s.Flush();
  3526. s.Close();
  3527. s = OpenSession();
  3528. s.Load(fee, fee.Key);
  3529. Assert.IsNotNull(fee.AnotherFee, "update");
  3530. Assert.IsNotNull(fee.TheFee, "update");
  3531. Assert.AreSame(fee.AnotherFee.TheFee, fee.TheFee, "update");
  3532. Assert.IsTrue(fee.Fees.Contains("new element"), "updated collection");
  3533. Assert.IsFalse(fee.Fees.Contains("an element"), "updated collection");
  3534. s.Flush();
  3535. s.Close();
  3536. fee.Qux = new Qux("quxy");
  3537. s = OpenSession();
  3538. s.SaveOrUpdate(fee);
  3539. s.Flush();
  3540. s.Close();
  3541. fee.Qux.Stuff = "xxx";
  3542. s = OpenSession();
  3543. s.SaveOrUpdate(fee);
  3544. s.Flush();
  3545. s.Close();
  3546. s = OpenSession();
  3547. s.Load(fee, fee.Key);
  3548. Assert.IsNotNull(fee.Qux, "cascade update");
  3549. Assert.AreEqual("xxx", fee.Qux.Stuff, "cascade update");
  3550. Assert.IsNotNull(fee.AnotherFee, "update");
  3551. Assert.IsNotNull(fee.TheFee, "update");
  3552. Assert.AreSame(fee.AnotherFee.TheFee, fee.TheFee, "update");
  3553. fee.AnotherFee.AnotherFee = null;
  3554. s.Delete(fee);
  3555. s.Delete("from fee in class NHibernate.DomainModel.Fee");
  3556. s.Flush();
  3557. s.Close();
  3558. }
  3559. [Test]
  3560. public void ArraysOfTimes()
  3561. {
  3562. Baz baz;
  3563. using (ISession s = OpenSession())
  3564. {
  3565. baz = new Baz();
  3566. s.Save(baz);
  3567. baz.SetDefaults();
  3568. s.Flush();
  3569. }
  3570. using (ISession s = OpenSession())
  3571. {
  3572. baz.TimeArray[2] = new DateTime(123); // H2.1: new Date(123)
  3573. baz.TimeArray[3] = new DateTime(1234); // H2.1: new java.sql.Time(1234)
  3574. s.Update(baz); // missing in H2.1
  3575. s.Flush();
  3576. }
  3577. using (ISession s = OpenSession())
  3578. {
  3579. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  3580. s.Delete(baz);
  3581. s.Flush();
  3582. }
  3583. }
  3584. [Test]
  3585. public void Components()
  3586. {
  3587. ISession s = OpenSession();
  3588. ITransaction txn = s.BeginTransaction();
  3589. Foo foo = new Foo();
  3590. foo.Component = new FooComponent("foo", 69, null, new FooComponent("bar", 96, null, null));
  3591. s.Save(foo);
  3592. foo.Component.Name = "IFA";
  3593. txn.Commit();
  3594. s.Close();
  3595. foo.Component = null;
  3596. s = OpenSession();
  3597. txn = s.BeginTransaction();
  3598. s.Load(foo, foo.Key);
  3599. Assert.AreEqual("IFA", foo.Component.Name, "save components");
  3600. Assert.AreEqual("bar", foo.Component.Subcomponent.Name, "save subcomponent");
  3601. Assert.IsNotNull(foo.Component.Glarch, "cascades save via component");
  3602. foo.Component.Subcomponent.Name = "baz";
  3603. txn.Commit();
  3604. s.Close();
  3605. foo.Component = null;
  3606. s = OpenSession();
  3607. txn = s.BeginTransaction();
  3608. s.Load(foo, foo.Key);
  3609. Assert.AreEqual("IFA", foo.Component.Name, "update components");
  3610. Assert.AreEqual("baz", foo.Component.Subcomponent.Name, "update subcomponent");
  3611. s.Delete(foo);
  3612. txn.Commit();
  3613. s.Close();
  3614. s = OpenSession();
  3615. txn = s.BeginTransaction();
  3616. foo = new Foo();
  3617. s.Save(foo);
  3618. foo.Custom = new string[] {"one", "two"};
  3619. // Custom.s1 uses the first column under the <property name="Custom"...>
  3620. // which is first_name
  3621. Assert.AreSame(foo, s.CreateQuery("from Foo foo where foo.Custom.s1 = 'one'").List()[0]);
  3622. s.Delete(foo);
  3623. txn.Commit();
  3624. s.Close();
  3625. }
  3626. [Test]
  3627. public void Enum()
  3628. {
  3629. ISession s = OpenSession();
  3630. FooProxy foo = new Foo();
  3631. object id = s.Save(foo);
  3632. foo.Status = FooStatus.ON;
  3633. s.Flush();
  3634. s.Close();
  3635. // verify an enum can be in the ctor
  3636. s = OpenSession();
  3637. IList list =
  3638. s.CreateQuery("select new Result(foo.String, foo.Long, foo.Integer, foo.Status) from foo in class Foo").List();
  3639. Assert.AreEqual(1, list.Count, "Should have found foo");
  3640. Assert.AreEqual(FooStatus.ON, ((Result) list[0]).Status, "verifying enum set in ctor - should have been ON");
  3641. s.Close();
  3642. s = OpenSession();
  3643. foo = (FooProxy) s.Load(typeof(Foo), id);
  3644. Assert.AreEqual(FooStatus.ON, foo.Status);
  3645. foo.Status = FooStatus.OFF;
  3646. s.Flush();
  3647. s.Close();
  3648. s = OpenSession();
  3649. foo = (FooProxy) s.Load(typeof(Foo), id);
  3650. Assert.AreEqual(FooStatus.OFF, foo.Status);
  3651. s.Close();
  3652. // verify that SetEnum with named params works correctly
  3653. s = OpenSession();
  3654. IQuery q = s.CreateQuery("from Foo as f where f.Status = :status");
  3655. q.SetEnum("status", FooStatus.OFF);
  3656. IList results = q.List();
  3657. Assert.AreEqual(1, results.Count, "should have found 1");
  3658. foo = (Foo) results[0];
  3659. q = s.CreateQuery("from Foo as f where f.Status = :status");
  3660. q.SetEnum("status", FooStatus.ON);
  3661. results = q.List();
  3662. Assert.AreEqual(0, results.Count, "no foo with status of ON");
  3663. // try to have the Query guess the enum type
  3664. q = s.CreateQuery("from Foo as f where f.Status = :status");
  3665. q.SetParameter("status", FooStatus.OFF);
  3666. results = q.List();
  3667. Assert.AreEqual(1, results.Count, "found the 1 result");
  3668. // have the query guess the enum type in a ParameterList.
  3669. q = s.CreateQuery("from Foo as f where f.Status in (:status)");
  3670. q.SetParameterList("status", new FooStatus[] {FooStatus.OFF, FooStatus.ON});
  3671. results = q.List();
  3672. Assert.AreEqual(1, results.Count, "should have found the 1 foo");
  3673. q = s.CreateQuery("from Foo as f where f.Status = FooStatus.OFF");
  3674. Assert.AreEqual(1, q.List().Count, "Enum in string - should have found OFF");
  3675. q = s.CreateQuery("from Foo as f where f.Status = FooStatus.ON");
  3676. Assert.AreEqual(0, q.List().Count, "Enum in string - should not have found ON");
  3677. s.Delete(foo);
  3678. s.Flush();
  3679. s.Close();
  3680. }
  3681. [Test]
  3682. public void NoForeignKeyViolations()
  3683. {
  3684. ISession s = OpenSession();
  3685. Glarch g1 = new Glarch();
  3686. Glarch g2 = new Glarch();
  3687. g1.Next = g2;
  3688. g2.Next = g1;
  3689. s.Save(g1);
  3690. s.Save(g2);
  3691. s.Flush();
  3692. s.Close();
  3693. s = OpenSession();
  3694. IList l = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch where g.Next is not null").List();
  3695. s.Delete(l[0]);
  3696. s.Delete(l[1]);
  3697. s.Flush();
  3698. s.Close();
  3699. }
  3700. [Test]
  3701. public void LazyCollections()
  3702. {
  3703. ISession s = OpenSession();
  3704. Qux q = new Qux();
  3705. s.Save(q);
  3706. s.Flush();
  3707. s.Close();
  3708. s = OpenSession();
  3709. q = (Qux) s.Load(typeof(Qux), q.Key);
  3710. s.Flush();
  3711. s.Close();
  3712. // two exceptions are supposed to occur:")
  3713. bool ok = false;
  3714. try
  3715. {
  3716. int countMoreFums = q.MoreFums.Count;
  3717. }
  3718. catch (LazyInitializationException lie)
  3719. {
  3720. Debug.WriteLine("caught expected " + lie.ToString());
  3721. ok = true;
  3722. }
  3723. Assert.IsTrue(ok, "lazy collection with one-to-many");
  3724. ok = false;
  3725. try
  3726. {
  3727. int countFums = q.Fums.Count;
  3728. }
  3729. catch (LazyInitializationException lie)
  3730. {
  3731. Debug.WriteLine("caught expected " + lie.ToString());
  3732. ok = true;
  3733. }
  3734. Assert.IsTrue(ok, "lazy collection with many-to-many");
  3735. s = OpenSession();
  3736. q = (Qux) s.Load(typeof(Qux), q.Key);
  3737. s.Delete(q);
  3738. s.Flush();
  3739. s.Close();
  3740. }
  3741. [Test]
  3742. public void NewSessionLifecycle()
  3743. {
  3744. ISession s = OpenSession();
  3745. ITransaction t = s.BeginTransaction();
  3746. object fid = null;
  3747. try
  3748. {
  3749. Foo f = new Foo();
  3750. s.Save(f);
  3751. fid = s.GetIdentifier(f);
  3752. //s.Flush();
  3753. t.Commit();
  3754. }
  3755. catch (Exception)
  3756. {
  3757. t.Rollback();
  3758. throw;
  3759. }
  3760. finally
  3761. {
  3762. s.Close();
  3763. }
  3764. s = OpenSession();
  3765. t = s.BeginTransaction();
  3766. try
  3767. {
  3768. Foo f = new Foo();
  3769. s.Delete(f);
  3770. //s.Flush();
  3771. t.Commit();
  3772. }
  3773. catch (Exception)
  3774. {
  3775. t.Rollback();
  3776. }
  3777. finally
  3778. {
  3779. s.Close();
  3780. }
  3781. s = OpenSession();
  3782. t = s.BeginTransaction();
  3783. try
  3784. {
  3785. Foo f = (Foo) s.Load(typeof(Foo), fid, LockMode.Upgrade);
  3786. s.Delete(f);
  3787. // s.Flush();
  3788. t.Commit();
  3789. }
  3790. catch (Exception)
  3791. {
  3792. t.Rollback();
  3793. throw;
  3794. }
  3795. finally
  3796. {
  3797. Assert.IsNull(s.Close());
  3798. }
  3799. }
  3800. [Test]
  3801. public void Disconnect()
  3802. {
  3803. ISession s = OpenSession();
  3804. Foo foo = new Foo();
  3805. Foo foo2 = new Foo();
  3806. s.Save(foo);
  3807. s.Save(foo2);
  3808. foo2.TheFoo = foo;
  3809. s.Flush();
  3810. s.Disconnect();
  3811. s.Reconnect();
  3812. s.Delete(foo);
  3813. foo2.TheFoo = null;
  3814. s.Flush();
  3815. s.Disconnect();
  3816. s.Reconnect();
  3817. s.Delete(foo2);
  3818. s.Flush();
  3819. s.Close();
  3820. }
  3821. [Test]
  3822. public void OrderBy()
  3823. {
  3824. ISession s = OpenSession();
  3825. ITransaction t = s.BeginTransaction();
  3826. Foo foo = new Foo();
  3827. s.Save(foo);
  3828. IList list =
  3829. s.CreateQuery(
  3830. "select foo from foo in class Foo, fee in class Fee where foo.Dependent = fee order by foo.String desc, foo.Component.Count asc, fee.id")
  3831. .List();
  3832. Assert.AreEqual(1, list.Count, "order by");
  3833. Foo foo2 = new Foo();
  3834. s.Save(foo2);
  3835. foo.TheFoo = foo2;
  3836. list =
  3837. s.CreateQuery(
  3838. "select foo.TheFoo, foo.Dependent from foo in class Foo order by foo.TheFoo.String desc, foo.Component.Count asc, foo.Dependent.id")
  3839. .List();
  3840. Assert.AreEqual(1, list.Count, "order by");
  3841. list =
  3842. s.CreateQuery("select foo from foo in class NHibernate.DomainModel.Foo order by foo.Dependent.id, foo.Dependent.Fi")
  3843. .List();
  3844. Assert.AreEqual(2, list.Count, "order by");
  3845. s.Delete(foo);
  3846. s.Delete(foo2);
  3847. t.Commit();
  3848. s.Close();
  3849. s = OpenSession();
  3850. Many manyB = new Many();
  3851. s.Save(manyB);
  3852. One oneB = new One();
  3853. s.Save(oneB);
  3854. oneB.Value = "b";
  3855. manyB.One = oneB;
  3856. Many manyA = new Many();
  3857. s.Save(manyA);
  3858. One oneA = new One();
  3859. s.Save(oneA);
  3860. oneA.Value = "a";
  3861. manyA.One = oneA;
  3862. s.Flush();
  3863. s.Close();
  3864. s = OpenSession();
  3865. IEnumerable enumerable =
  3866. s.CreateQuery("SELECT one FROM one IN CLASS " + typeof(One).Name + " ORDER BY one.Value ASC").Enumerable();
  3867. int count = 0;
  3868. foreach (One one in enumerable)
  3869. {
  3870. switch (count)
  3871. {
  3872. case 0:
  3873. Assert.AreEqual("a", one.Value, "a - ordering failed");
  3874. break;
  3875. case 1:
  3876. Assert.AreEqual("b", one.Value, "b - ordering failed");
  3877. break;
  3878. default:
  3879. Assert.Fail("more than two elements");
  3880. break;
  3881. }
  3882. count++;
  3883. }
  3884. s.Flush();
  3885. s.Close();
  3886. s = OpenSession();
  3887. enumerable =
  3888. s.CreateQuery("SELECT many.One FROM many IN CLASS " + typeof(Many).Name +
  3889. " ORDER BY many.One.Value ASC, many.One.id").Enumerable();
  3890. count = 0;
  3891. foreach (One one in enumerable)
  3892. {
  3893. switch (count)
  3894. {
  3895. case 0:
  3896. Assert.AreEqual("a", one.Value, "'a' should be first element");
  3897. break;
  3898. case 1:
  3899. Assert.AreEqual("b", one.Value, "'b' should be second element");
  3900. break;
  3901. default:
  3902. Assert.Fail("more than 2 elements");
  3903. break;
  3904. }
  3905. count++;
  3906. }
  3907. s.Flush();
  3908. s.Close();
  3909. s = OpenSession();
  3910. oneA = (One) s.Load(typeof(One), oneA.Key);
  3911. manyA = (Many) s.Load(typeof(Many), manyA.Key);
  3912. oneB = (One) s.Load(typeof(One), oneB.Key);
  3913. manyB = (Many) s.Load(typeof(Many), manyB.Key);
  3914. s.Delete(manyA);
  3915. s.Delete(oneA);
  3916. s.Delete(manyB);
  3917. s.Delete(oneB);
  3918. s.Flush();
  3919. s.Close();
  3920. }
  3921. [Test]
  3922. public void ManyToOne()
  3923. {
  3924. ISession s = OpenSession();
  3925. One one = new One();
  3926. s.Save(one);
  3927. one.Value = "yada";
  3928. Many many = new Many();
  3929. many.One = one;
  3930. s.Save(many);
  3931. s.Flush();
  3932. s.Close();
  3933. s = OpenSession();
  3934. one = (One) s.Load(typeof(One), one.Key);
  3935. int countManies = one.Manies.Count;
  3936. s.Close();
  3937. s = OpenSession();
  3938. many = (Many) s.Load(typeof(Many), many.Key);
  3939. Assert.IsNotNull(many.One, "many-to-one assoc");
  3940. s.Delete(many.One);
  3941. s.Delete(many);
  3942. s.Flush();
  3943. s.Close();
  3944. }
  3945. [Test]
  3946. public void SaveDelete()
  3947. {
  3948. ISession s = OpenSession();
  3949. Foo f = new Foo();
  3950. s.Save(f);
  3951. s.Flush();
  3952. s.Close();
  3953. s = OpenSession();
  3954. s.Delete(s.Load(typeof(Foo), f.Key));
  3955. s.Flush();
  3956. s.Close();
  3957. }
  3958. [Test]
  3959. public void ProxyArray()
  3960. {
  3961. ISession s = OpenSession();
  3962. GlarchProxy g = new Glarch();
  3963. Glarch g1 = new Glarch();
  3964. Glarch g2 = new Glarch();
  3965. g.ProxyArray = new GlarchProxy[] {g1, g2};
  3966. Glarch g3 = new Glarch();
  3967. s.Save(g3);
  3968. g2.ProxyArray = new GlarchProxy[] {null, g3, g};
  3969. g.ProxySet = new HashSet<GlarchProxy> { g1, g2 };
  3970. s.Save(g);
  3971. s.Save(g1);
  3972. s.Save(g2);
  3973. object id = s.GetIdentifier(g);
  3974. s.Flush();
  3975. s.Close();
  3976. s = OpenSession();
  3977. g = (GlarchProxy) s.Load(typeof(Glarch), id);
  3978. Assert.AreEqual(2, g.ProxyArray.Length, "array of proxies");
  3979. Assert.IsNotNull(g.ProxyArray[0], "array of proxies");
  3980. Assert.IsNull(g.ProxyArray[1].ProxyArray[0], "deferred load test");
  3981. Assert.AreEqual(g, g.ProxyArray[1].ProxyArray[2], "deferred load test");
  3982. Assert.AreEqual(2, g.ProxySet.Count, "set of proxies");
  3983. IEnumerator enumer = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch").Enumerable().GetEnumerator();
  3984. while (enumer.MoveNext())
  3985. {
  3986. s.Delete(enumer.Current);
  3987. }
  3988. s.Flush();
  3989. s.Disconnect();
  3990. // serialize the session.
  3991. Stream stream = new MemoryStream();
  3992. var formatter = new BinaryFormatter()
  3993. {
  3994. #if !NETFX
  3995. SurrogateSelector = new SerializationHelper.SurrogateSelector()
  3996. #endif
  3997. };
  3998. formatter.Serialize(stream, s);
  3999. // close the original session
  4000. s.Close();
  4001. // deserialize the session
  4002. stream.Position = 0;
  4003. s = (ISession) formatter.Deserialize(stream);
  4004. stream.Close();
  4005. s.Close();
  4006. }
  4007. [Test]
  4008. public void Cache()
  4009. {
  4010. NHibernate.DomainModel.Immutable im = new NHibernate.DomainModel.Immutable();
  4011. using (ISession s = OpenSession())
  4012. {
  4013. s.Save(im);
  4014. s.Flush();
  4015. }
  4016. using (ISession s = OpenSession())
  4017. {
  4018. s.Load(im, im.Id);
  4019. }
  4020. using (ISession s = OpenSession())
  4021. {
  4022. s.Load(im, im.Id);
  4023. NHibernate.DomainModel.Immutable imFromFind =
  4024. (NHibernate.DomainModel.Immutable) s.CreateQuery("from im in class Immutable where im = ?").SetEntity(0, im).List()[0];
  4025. NHibernate.DomainModel.Immutable imFromLoad = (NHibernate.DomainModel.Immutable) s.Load(typeof(NHibernate.DomainModel.Immutable), im.Id);
  4026. Assert.IsTrue(im == imFromFind, "cached object identity from Find ");
  4027. Assert.IsTrue(im == imFromLoad, "cached object identity from Load ");
  4028. }
  4029. // Clean up the immutable. Need to do this using direct SQL, since ISession
  4030. // refuses to delete immutable objects.
  4031. using (ISession s = OpenSession())
  4032. {
  4033. var connection = s.Connection;
  4034. using (var command = connection.CreateCommand())
  4035. {
  4036. command.CommandText = "delete from immut";
  4037. command.ExecuteNonQuery();
  4038. }
  4039. }
  4040. }
  4041. [Test]
  4042. public void FindLoad()
  4043. {
  4044. ISession s = OpenSession();
  4045. FooProxy foo = new Foo();
  4046. s.Save(foo);
  4047. s.Flush();
  4048. s.Close();
  4049. s = OpenSession();
  4050. foo = (FooProxy) s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List()[0];
  4051. FooProxy foo2 = (FooProxy) s.Load(typeof(Foo), foo.Key);
  4052. Assert.AreSame(foo, foo2, "find returns same object as load");
  4053. s.Flush();
  4054. s.Close();
  4055. s = OpenSession();
  4056. foo2 = (FooProxy) s.Load(typeof(Foo), foo.Key);
  4057. foo = (FooProxy) s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List()[0];
  4058. Assert.AreSame(foo2, foo, "find returns same object as load");
  4059. s.Delete("from foo in class NHibernate.DomainModel.Foo");
  4060. s.Flush();
  4061. s.Close();
  4062. }
  4063. [Test]
  4064. public void Refresh()
  4065. {
  4066. ISession s = OpenSession();
  4067. Foo foo = new Foo();
  4068. s.Save(foo);
  4069. s.Flush();
  4070. var cmd = s.Connection.CreateCommand();
  4071. cmd.CommandText = "update " + Dialect.QuoteForTableName("foos") + " set long_ = -3";
  4072. cmd.ExecuteNonQuery();
  4073. s.Refresh(foo);
  4074. Assert.AreEqual((long) -3, foo.Long);
  4075. Assert.AreEqual(LockMode.Read, s.GetCurrentLockMode(foo));
  4076. s.Refresh(foo, LockMode.Upgrade);
  4077. Assert.AreEqual(LockMode.Upgrade, s.GetCurrentLockMode(foo));
  4078. s.Delete(foo);
  4079. s.Flush();
  4080. s.Close();
  4081. }
  4082. [Test]
  4083. public void RefreshTransient()
  4084. {
  4085. ISession s = OpenSession();
  4086. Foo foo = new Foo();
  4087. s.Save(foo);
  4088. s.Flush();
  4089. /*
  4090. Commented to have same behavior of H3.2 (test named FooBarTest.testRefresh())
  4091. s.Close();
  4092. s = OpenSession();
  4093. btw using close and open a new session more than Transient the entity will be detached.
  4094. */
  4095. var cmd = s.Connection.CreateCommand();
  4096. cmd.CommandText = "update " + Dialect.QuoteForTableName("foos") + " set long_ = -3";
  4097. cmd.ExecuteNonQuery();
  4098. s.Refresh(foo);
  4099. Assert.AreEqual(-3L, foo.Long);
  4100. s.Delete(foo);
  4101. s.Flush();
  4102. s.Close();
  4103. }
  4104. [Test]
  4105. public void AutoFlush()
  4106. {
  4107. ISession s = OpenSession();
  4108. ITransaction txn = s.BeginTransaction();
  4109. FooProxy foo = new Foo();
  4110. s.Save(foo);
  4111. Assert.AreEqual(1, s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List().Count,
  4112. "autoflush inserted row");
  4113. foo.Char = 'X';
  4114. Assert.AreEqual(1, s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where foo.Char='X'").List().Count,
  4115. "autflush updated row");
  4116. txn.Commit();
  4117. s.Close();
  4118. s = OpenSession();
  4119. txn = s.BeginTransaction();
  4120. foo = (FooProxy) s.Load(typeof(Foo), foo.Key);
  4121. if (Dialect.SupportsSubSelects)
  4122. {
  4123. foo.Bytes = GetBytes("osama");
  4124. Assert.AreEqual(1,
  4125. s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where 111 in elements(foo.Bytes)")
  4126. .List().Count, "autoflush collection update");
  4127. foo.Bytes[0] = 69;
  4128. Assert.AreEqual(1,
  4129. s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where 69 in elements(foo.Bytes)")
  4130. .List().Count, "autoflush collection update");
  4131. }
  4132. s.Delete(foo);
  4133. Assert.AreEqual(0, s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List().Count, "autoflush delete");
  4134. txn.Commit();
  4135. s.Close();
  4136. }
  4137. [Test]
  4138. public void Veto()
  4139. {
  4140. ISession s = OpenSession();
  4141. Vetoer v = new Vetoer();
  4142. s.Save(v);
  4143. object id = s.Save(v);
  4144. s.Flush();
  4145. s.Close();
  4146. s = OpenSession();
  4147. s.Update(v, id);
  4148. s.Update(v, id);
  4149. s.Delete(v);
  4150. s.Delete(v);
  4151. s.Flush();
  4152. s.Close();
  4153. }
  4154. [Test]
  4155. public void SerializableType()
  4156. {
  4157. ISession s = OpenSession();
  4158. Vetoer v = new Vetoer();
  4159. v.Strings = new string[] {"foo", "bar", "baz"};
  4160. s.Save(v);
  4161. object id = s.Save(v);
  4162. v.Strings[1] = "osama";
  4163. s.Flush();
  4164. s.Close();
  4165. s = OpenSession();
  4166. v = (Vetoer) s.Load(typeof(Vetoer), id);
  4167. Assert.AreEqual("osama", v.Strings[1], "serializable type");
  4168. s.Delete(v);
  4169. s.Delete(v);
  4170. s.Flush();
  4171. s.Close();
  4172. }
  4173. [Test]
  4174. public void AutoFlushCollections()
  4175. {
  4176. ISession s = OpenSession();
  4177. ITransaction tx = s.BeginTransaction();
  4178. Baz baz = new Baz();
  4179. baz.SetDefaults();
  4180. s.Save(baz);
  4181. tx.Commit();
  4182. s.Close();
  4183. s = OpenSession();
  4184. tx = s.BeginTransaction();
  4185. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  4186. baz.StringArray[0] = "bark";
  4187. IEnumerator e;
  4188. e = s.CreateQuery("select elements(baz.StringArray) from baz in class NHibernate.DomainModel.Baz").Enumerable().
  4189. GetEnumerator();
  4190. bool found = false;
  4191. while (e.MoveNext())
  4192. {
  4193. if ("bark".Equals(e.Current))
  4194. {
  4195. found = true;
  4196. }
  4197. }
  4198. Assert.IsTrue(found);
  4199. baz.StringArray = null;
  4200. e = s.CreateQuery("select distinct elements(baz.StringArray) from baz in class NHibernate.DomainModel.Baz").Enumerable()
  4201. .GetEnumerator();
  4202. Assert.IsFalse(e.MoveNext());
  4203. baz.StringArray = new string[] {"foo", "bar"};
  4204. e = s.CreateQuery("select elements(baz.StringArray) from baz in class NHibernate.DomainModel.Baz").Enumerable().
  4205. GetEnumerator();
  4206. Assert.IsTrue(e.MoveNext());
  4207. Foo foo = new Foo();
  4208. s.Save(foo);
  4209. s.Flush();
  4210. baz.FooArray = new Foo[] {foo};
  4211. e = s.CreateQuery("select foo from baz in class NHibernate.DomainModel.Baz, foo in elements(baz.FooArray)").Enumerable()
  4212. .GetEnumerator();
  4213. found = false;
  4214. while (e.MoveNext())
  4215. {
  4216. if (foo == e.Current)
  4217. {
  4218. found = true;
  4219. }
  4220. }
  4221. Assert.IsTrue(found);
  4222. baz.FooArray[0] = null;
  4223. e = s.CreateQuery("select foo from baz in class NHibernate.DomainModel.Baz, foo in elements(baz.FooArray)").Enumerable()
  4224. .GetEnumerator();
  4225. Assert.IsFalse(e.MoveNext());
  4226. baz.FooArray[0] = foo;
  4227. e = s.CreateQuery("select elements(baz.FooArray) from baz in class NHibernate.DomainModel.Baz").Enumerable().
  4228. GetEnumerator();
  4229. Assert.IsTrue(e.MoveNext());
  4230. if (Dialect.SupportsSubSelects && !(Dialect is FirebirdDialect))
  4231. {
  4232. baz.FooArray[0] = null;
  4233. e = s.CreateQuery("from baz in class NHibernate.DomainModel.Baz where ? in elements(baz.FooArray)").SetEntity(0, foo).
  4234. Enumerable().GetEnumerator();
  4235. Assert.IsFalse(e.MoveNext());
  4236. baz.FooArray[0] = foo;
  4237. e = s.CreateQuery("select foo from foo in class NHibernate.DomainModel.Foo where foo in "
  4238. + "(select elt from baz in class NHibernate.DomainModel.Baz, elt in elements(baz.FooArray))").
  4239. Enumerable().GetEnumerator();
  4240. Assert.IsTrue(e.MoveNext());
  4241. }
  4242. s.Delete(foo);
  4243. s.Delete(baz);
  4244. tx.Commit();
  4245. s.Close();
  4246. }
  4247. [Test]
  4248. public void UserProvidedConnection()
  4249. {
  4250. using (var prov = ConnectionProviderFactory.NewConnectionProvider(cfg.Properties))
  4251. using (var connection = prov.GetConnection())
  4252. using (var s = Sfi.WithOptions().Connection(connection).OpenSession())
  4253. {
  4254. using (var tx = s.BeginTransaction())
  4255. {
  4256. s.CreateQuery("from foo in class NHibernate.DomainModel.Fo").List();
  4257. tx.Commit();
  4258. }
  4259. var c = s.Disconnect();
  4260. Assert.IsNotNull(c);
  4261. s.Reconnect(c);
  4262. using (var tx = s.BeginTransaction())
  4263. {
  4264. s.CreateQuery("from foo in class NHibernate.DomainModel.Fo").List();
  4265. tx.Commit();
  4266. }
  4267. Assert.AreSame(c, s.Close());
  4268. c.Close();
  4269. }
  4270. }
  4271. [Test]
  4272. public void CachedCollection()
  4273. {
  4274. ISession s = OpenSession();
  4275. Baz baz = new Baz();
  4276. baz.SetDefaults();
  4277. s.Save(baz);
  4278. s.Flush();
  4279. s.Close();
  4280. s = OpenSession();
  4281. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  4282. ((FooComponent) baz.TopComponents[0]).Count = 99;
  4283. s.Flush();
  4284. s.Close();
  4285. s = OpenSession();
  4286. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  4287. Assert.AreEqual(99, ((FooComponent) baz.TopComponents[0]).Count);
  4288. s.Delete(baz);
  4289. s.Flush();
  4290. s.Close();
  4291. }
  4292. [Test]
  4293. public void ComplicatedQuery()
  4294. {
  4295. ISession s = OpenSession();
  4296. ITransaction txn = s.BeginTransaction();
  4297. Foo foo = new Foo();
  4298. object id = s.Save(foo);
  4299. Assert.IsNotNull(id);
  4300. Qux q = new Qux("q");
  4301. foo.Dependent.Qux = q;
  4302. s.Save(q);
  4303. q.Foo.String = "foo2";
  4304. IEnumerator enumer =
  4305. s.CreateQuery("from foo in class Foo where foo.Dependent.Qux.Foo.String = 'foo2'").Enumerable().GetEnumerator();
  4306. Assert.IsTrue(enumer.MoveNext());
  4307. s.Delete(foo);
  4308. txn.Commit();
  4309. s.Close();
  4310. }
  4311. [Test]
  4312. public void LoadAfterDelete()
  4313. {
  4314. ISession s = OpenSession();
  4315. Foo foo = new Foo();
  4316. object id = s.Save(foo);
  4317. s.Flush();
  4318. s.Delete(foo);
  4319. bool err = false;
  4320. try
  4321. {
  4322. s.Load(typeof(Foo), id);
  4323. }
  4324. //catch (ObjectDeletedException ode) Changed to have same behavior of H3.2
  4325. catch (ObjectNotFoundException ode)
  4326. {
  4327. Assert.IsNotNull(ode); //getting ride of 'ode' is never used compile warning
  4328. err = true;
  4329. }
  4330. Assert.IsTrue(err);
  4331. s.Flush();
  4332. err = false;
  4333. try
  4334. {
  4335. bool proxyBoolean = ((FooProxy) s.Load(typeof(Foo), id)).Boolean;
  4336. }
  4337. catch (ObjectNotFoundException lie)
  4338. {
  4339. // Proxy initialization which failed because the object was not found
  4340. // now throws ONFE instead of LazyInitializationException
  4341. Assert.IsNotNull(lie); //getting ride of 'lie' is never used compile warning
  4342. err = true;
  4343. }
  4344. Assert.IsTrue(err);
  4345. Fo fo = Fo.NewFo();
  4346. id = FumTest.FumKey("abc"); //yuck!
  4347. s.Save(fo, id);
  4348. s.Flush();
  4349. s.Delete(fo);
  4350. err = false;
  4351. try
  4352. {
  4353. s.Load(typeof(Fo), id);
  4354. }
  4355. //catch (ObjectDeletedException ode) Changed to have same behavior of H3.2
  4356. catch (ObjectNotFoundException ode)
  4357. {
  4358. Assert.IsNotNull(ode); //getting ride of 'ode' is never used compile warning
  4359. err = true;
  4360. }
  4361. Assert.IsTrue(err);
  4362. s.Flush();
  4363. s.Close();
  4364. }
  4365. [Test]
  4366. public void ObjectType()
  4367. {
  4368. object gid;
  4369. using (ISession s = OpenSession())
  4370. {
  4371. GlarchProxy g = new Glarch();
  4372. Foo foo = new Foo();
  4373. g.Any = foo;
  4374. gid = s.Save(g);
  4375. s.Save(foo);
  4376. s.Flush();
  4377. }
  4378. using (ISession s = OpenSession())
  4379. {
  4380. GlarchProxy g = (GlarchProxy) s.Load(typeof(Glarch), gid);
  4381. Assert.IsNotNull(g.Any);
  4382. Assert.IsTrue(g.Any is FooProxy);
  4383. s.Delete(g.Any);
  4384. s.Delete(g);
  4385. s.Flush();
  4386. }
  4387. }
  4388. [Test]
  4389. public void Any()
  4390. {
  4391. ISession s = OpenSession();
  4392. One one = new One();
  4393. BarProxy foo = new Bar();
  4394. foo.Object = one;
  4395. object fid = s.Save(foo);
  4396. object oid = one.Key;
  4397. s.Flush();
  4398. s.Close();
  4399. s = OpenSession();
  4400. var list = s.CreateQuery("from Bar bar where bar.Object.id = ? and bar.Object.class = ?")
  4401. #pragma warning disable 618
  4402. .SetParameter(0, oid, NHibernateUtil.Int64).SetParameter(1, typeof(One).FullName, NHibernateUtil.ClassMetaType).List();
  4403. #pragma warning restore 618
  4404. Assert.AreEqual(1, list.Count);
  4405. list = s.CreateQuery("from Bar bar where bar.Object.id = ? and bar.Object.class = ?")
  4406. .SetParameter(0, oid, NHibernateUtil.Int64).SetParameter(1, typeof(One).FullName, NHibernateUtil.MetaType).List();
  4407. Assert.AreEqual(1, list.Count);
  4408. // this is a little different from h2.0.3 because the full type is stored, not
  4409. // just the class name.
  4410. list =
  4411. s.CreateQuery(
  4412. "select one from One one, Bar bar where bar.Object.id = one.id and bar.Object.class LIKE 'NHibernate.DomainModel.One%'")
  4413. .List();
  4414. Assert.AreEqual(1, list.Count);
  4415. s.Flush();
  4416. s.Close();
  4417. s = OpenSession();
  4418. foo = (BarProxy) s.Load(typeof(Foo), fid);
  4419. Assert.IsNotNull(foo);
  4420. Assert.IsTrue(foo.Object is One);
  4421. Assert.AreEqual(oid, s.GetIdentifier(foo.Object));
  4422. s.Delete(foo);
  4423. s.Delete(foo.Object);
  4424. s.Flush();
  4425. s.Close();
  4426. }
  4427. [Test]
  4428. public void EmbeddedCompositeID()
  4429. {
  4430. ISession s = OpenSession();
  4431. Location l = new Location();
  4432. l.CountryCode = "AU";
  4433. l.Description = "foo bar";
  4434. l.Locale = CultureInfo.CreateSpecificCulture("en-AU");
  4435. l.StreetName = "Brunswick Rd";
  4436. l.StreetNumber = 300;
  4437. l.City = "Melbourne";
  4438. s.Save(l);
  4439. s.Flush();
  4440. s.Close();
  4441. s = OpenSession();
  4442. s.FlushMode = FlushMode.Manual;
  4443. l =
  4444. (Location)
  4445. s.CreateQuery("from l in class Location where l.CountryCode = 'AU' and l.Description='foo bar'").List()[0];
  4446. Assert.AreEqual("AU", l.CountryCode);
  4447. Assert.AreEqual("Melbourne", l.City);
  4448. Assert.AreEqual(CultureInfo.CreateSpecificCulture("en-AU"), l.Locale);
  4449. s.Close();
  4450. s = OpenSession();
  4451. l.Description = "sick're";
  4452. s.Update(l);
  4453. s.Flush();
  4454. s.Close();
  4455. s = OpenSession();
  4456. l = new Location();
  4457. l.CountryCode = "AU";
  4458. l.Description = "foo bar";
  4459. l.Locale = CultureInfo.CreateSpecificCulture("en-US");
  4460. l.StreetName = "Brunswick Rd";
  4461. l.StreetNumber = 300;
  4462. l.City = "Melbourne";
  4463. Assert.AreSame(l, s.Load(typeof(Location), l));
  4464. Assert.AreEqual(CultureInfo.CreateSpecificCulture("en-AU"), l.Locale);
  4465. s.Delete(l);
  4466. s.Flush();
  4467. s.Close();
  4468. }
  4469. [Test]
  4470. public void AutosaveChildren()
  4471. {
  4472. ISession s = OpenSession();
  4473. ITransaction t = s.BeginTransaction();
  4474. Baz baz = new Baz();
  4475. baz.CascadingBars = new HashSet<BarProxy>();
  4476. s.Save(baz);
  4477. t.Commit();
  4478. s.Close();
  4479. s = OpenSession();
  4480. t = s.BeginTransaction();
  4481. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  4482. baz.CascadingBars.Add(new Bar());
  4483. baz.CascadingBars.Add(new Bar());
  4484. t.Commit();
  4485. s.Close();
  4486. s = OpenSession();
  4487. t = s.BeginTransaction();
  4488. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  4489. Assert.AreEqual(2, baz.CascadingBars.Count);
  4490. IEnumerator enumer = baz.CascadingBars.GetEnumerator();
  4491. Assert.IsTrue(enumer.MoveNext());
  4492. Assert.IsNotNull(enumer.Current);
  4493. baz.CascadingBars.Clear(); // test all-delete-orphan
  4494. s.Flush();
  4495. Assert.AreEqual(0, s.CreateQuery("from Bar bar").List().Count);
  4496. s.Delete(baz);
  4497. t.Commit();
  4498. s.Close();
  4499. }
  4500. [Test]
  4501. public void OrphanDelete()
  4502. {
  4503. ISession s = OpenSession();
  4504. ITransaction t = s.BeginTransaction();
  4505. Baz baz = new Baz();
  4506. IDictionary bars = new Hashtable();
  4507. bars.Add(new Bar(), new object());
  4508. bars.Add(new Bar(), new object());
  4509. bars.Add(new Bar(), new object());
  4510. s.Save(baz);
  4511. t.Commit();
  4512. s.Close();
  4513. s = OpenSession();
  4514. t = s.BeginTransaction();
  4515. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  4516. IEnumerator enumer = bars.GetEnumerator();
  4517. enumer.MoveNext();
  4518. bars.Remove(enumer.Current);
  4519. s.Delete(baz);
  4520. enumer.MoveNext();
  4521. bars.Remove(enumer.Current);
  4522. s.Flush();
  4523. Assert.AreEqual(0, s.CreateQuery("from Bar bar").List().Count);
  4524. t.Commit();
  4525. s.Close();
  4526. }
  4527. [Test]
  4528. public void TransientOrphanDelete()
  4529. {
  4530. ISession s = OpenSession();
  4531. ITransaction t = s.BeginTransaction();
  4532. Baz baz = new Baz();
  4533. var bars = new HashSet<BarProxy> { new Bar(), new Bar(), new Bar() };
  4534. baz.CascadingBars = bars;
  4535. IList<Foo> foos = new List<Foo>();
  4536. foos.Add(new Foo());
  4537. foos.Add(new Foo());
  4538. baz.FooBag = foos;
  4539. s.Save(baz);
  4540. IEnumerator enumer = new JoinedEnumerable(new IEnumerable[] {foos, bars}).GetEnumerator();
  4541. while (enumer.MoveNext())
  4542. {
  4543. FooComponent cmp = ((Foo) enumer.Current).Component;
  4544. s.Delete(cmp.Glarch);
  4545. cmp.Glarch = null;
  4546. }
  4547. t.Commit();
  4548. s.Close();
  4549. var enumerBar = bars.GetEnumerator();
  4550. enumerBar.MoveNext();
  4551. bars.Remove(enumerBar.Current);
  4552. foos.RemoveAt(1);
  4553. s = OpenSession();
  4554. t = s.BeginTransaction();
  4555. s.Update(baz);
  4556. Assert.AreEqual(2, s.CreateQuery("from Bar bar").List().Count);
  4557. Assert.AreEqual(3, s.CreateQuery("from Foo foo").List().Count);
  4558. t.Commit();
  4559. s.Close();
  4560. foos.RemoveAt(0);
  4561. s = OpenSession();
  4562. t = s.BeginTransaction();
  4563. s.Update(baz);
  4564. enumerBar = bars.GetEnumerator();
  4565. enumerBar.MoveNext();
  4566. bars.Remove(enumerBar.Current);
  4567. s.Delete(baz);
  4568. s.Flush();
  4569. Assert.AreEqual(0, s.CreateQuery("from Foo foo").List().Count);
  4570. t.Commit();
  4571. s.Close();
  4572. }
  4573. [Test]
  4574. public void ProxiesInCollections()
  4575. {
  4576. ISession s = OpenSession();
  4577. Baz baz = new Baz();
  4578. Bar bar = new Bar();
  4579. Bar bar2 = new Bar();
  4580. s.Save(bar);
  4581. object bar2id = s.Save(bar2);
  4582. baz.FooArray = new Foo[] {bar, bar2};
  4583. bar = new Bar();
  4584. s.Save(bar);
  4585. baz.FooSet = new HashSet<FooProxy> { bar };
  4586. baz.CascadingBars = new HashSet<BarProxy> { new Bar(), new Bar() };
  4587. var list = new List<Foo>();
  4588. list.Add(new Foo());
  4589. baz.FooBag = list;
  4590. object id = s.Save(baz);
  4591. IEnumerator enumer = baz.CascadingBars.GetEnumerator();
  4592. enumer.MoveNext();
  4593. object bid = ((Bar) enumer.Current).Key;
  4594. s.Flush();
  4595. s.Close();
  4596. s = OpenSession();
  4597. BarProxy barprox = (BarProxy) s.Load(typeof(Bar), bid);
  4598. BarProxy bar2prox = (BarProxy) s.Load(typeof(Bar), bar2id);
  4599. Assert.IsTrue(bar2prox is INHibernateProxy);
  4600. Assert.IsTrue(barprox is INHibernateProxy);
  4601. baz = (Baz) s.Load(typeof(Baz), id);
  4602. enumer = baz.CascadingBars.GetEnumerator();
  4603. enumer.MoveNext();
  4604. BarProxy b1 = (BarProxy) enumer.Current;
  4605. enumer.MoveNext();
  4606. BarProxy b2 = (BarProxy) enumer.Current;
  4607. Assert.IsTrue(
  4608. (b1 == barprox && !(b2 is INHibernateProxy))
  4609. || (b2 == barprox && !(b1 is INHibernateProxy))); //one-to-many
  4610. Assert.IsTrue(baz.FooArray[0] is INHibernateProxy); //many-to-many
  4611. Assert.AreEqual(bar2prox, baz.FooArray[1]);
  4612. if (Sfi.Settings.IsOuterJoinFetchEnabled)
  4613. {
  4614. enumer = baz.FooBag.GetEnumerator();
  4615. enumer.MoveNext();
  4616. Assert.IsFalse(enumer.Current is INHibernateProxy); // many-to-many outer-join="true"
  4617. }
  4618. enumer = baz.FooSet.GetEnumerator();
  4619. enumer.MoveNext();
  4620. Assert.IsFalse(enumer.Current is INHibernateProxy); //one-to-many
  4621. s.Delete("from o in class Baz");
  4622. s.Delete("from o in class Foo");
  4623. s.Flush();
  4624. s.Close();
  4625. }
  4626. // Not ported - testService() - not applicable to NHibernate
  4627. [Test]
  4628. public void PSCache()
  4629. {
  4630. using (ISession s = OpenSession())
  4631. using(ITransaction txn = s.BeginTransaction())
  4632. {
  4633. for (int i = 0; i < 10; i++)
  4634. {
  4635. s.Save(new Foo());
  4636. }
  4637. IQuery q = s.CreateQuery("from f in class Foo");
  4638. q.SetMaxResults(2);
  4639. q.SetFirstResult(5);
  4640. Assert.AreEqual(2, q.List().Count);
  4641. q = s.CreateQuery("from f in class Foo");
  4642. Assert.AreEqual(10, q.List().Count);
  4643. Assert.AreEqual(10, q.List().Count);
  4644. q.SetMaxResults(3);
  4645. q.SetFirstResult(3);
  4646. Assert.AreEqual(3, q.List().Count);
  4647. q = s.CreateQuery("from f in class Foo");
  4648. Assert.AreEqual(10, q.List().Count);
  4649. txn.Commit();
  4650. }
  4651. using (ISession s = OpenSession())
  4652. using (ITransaction txn = s.BeginTransaction())
  4653. {
  4654. IQuery q = s.CreateQuery("from f in class Foo");
  4655. Assert.AreEqual(10, q.List().Count);
  4656. q.SetMaxResults(5);
  4657. Assert.AreEqual(5, q.List().Count);
  4658. s.Delete("from f in class Foo");
  4659. txn.Commit();
  4660. }
  4661. }
  4662. #region NHibernate specific tests
  4663. [Test]
  4664. public void Formula()
  4665. {
  4666. Foo foo = new Foo();
  4667. ISession s = OpenSession();
  4668. object id = s.Save(foo);
  4669. s.Flush();
  4670. s.Close();
  4671. s = OpenSession();
  4672. foo = (Foo) s.CreateQuery("from Foo as f where f.id = ?").SetParameter(0, id, NHibernateUtil.String).List()[0];
  4673. Assert.AreEqual(4, foo.Formula, "should be 2x 'Int' property that is defaulted to 2");
  4674. s.Delete(foo);
  4675. s.Flush();
  4676. s.Close();
  4677. }
  4678. /// <summary>
  4679. /// This test verifies that the AddAll() method works
  4680. /// correctly for a persistent Set.
  4681. /// </summary>
  4682. [Test]
  4683. public void AddAll()
  4684. {
  4685. using (ISession s = OpenSession())
  4686. {
  4687. Foo foo1 = new Foo();
  4688. s.Save(foo1);
  4689. Foo foo2 = new Foo();
  4690. s.Save(foo2);
  4691. Foo foo3 = new Foo();
  4692. s.Save(foo3);
  4693. Baz baz = new Baz();
  4694. baz.FooSet = new HashSet<FooProxy> { foo1 };
  4695. s.Save(baz);
  4696. Assert.AreEqual(1, baz.FooSet.Count);
  4697. var foos = new List<FooProxy> { foo2, foo3 };
  4698. baz.FooSet.UnionWith(foos);
  4699. Assert.AreEqual(3, baz.FooSet.Count);
  4700. s.Flush();
  4701. // Clean up
  4702. foreach (Foo foo in baz.FooSet)
  4703. {
  4704. s.Delete(foo);
  4705. }
  4706. s.Delete(baz);
  4707. s.Flush();
  4708. }
  4709. }
  4710. [Test]
  4711. public void Copy()
  4712. {
  4713. Baz baz = new Baz();
  4714. baz.SetDefaults();
  4715. using (ISession s = OpenSession())
  4716. {
  4717. Baz persistentBaz = new Baz();
  4718. s.Save(persistentBaz);
  4719. s.Flush();
  4720. baz.Code = persistentBaz.Code;
  4721. }
  4722. using (ISession s = OpenSession())
  4723. {
  4724. Baz persistentBaz = s.Get(typeof(Baz), baz.Code) as Baz;
  4725. Baz copiedBaz = s.Merge(baz);
  4726. Assert.AreSame(persistentBaz, copiedBaz);
  4727. s.Delete(persistentBaz);
  4728. s.Flush();
  4729. }
  4730. }
  4731. [Test]
  4732. public void ParameterInHavingClause()
  4733. {
  4734. using (ISession s = OpenSession())
  4735. {
  4736. s.CreateQuery("select f.id from Foo f group by f.id having count(f.id) >= ?")
  4737. .SetInt32(0, 0)
  4738. .List();
  4739. }
  4740. }
  4741. // It's possible that this test only works on MS SQL Server. If somebody complains about
  4742. // the test not working on their DB, I'll put an if around the code to only run on MS SQL.
  4743. [Test]
  4744. public void ParameterInOrderByClause()
  4745. {
  4746. using (ISession s = OpenSession())
  4747. {
  4748. s.CreateQuery("from Foo as foo order by case ? when 0 then foo.id else foo.id end")
  4749. .SetInt32(0, 0)
  4750. .List();
  4751. }
  4752. }
  4753. #endregion
  4754. }
  4755. }