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

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

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