forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMasterDetailTest.cs
1307 lines (1144 loc) · 32.4 KB
/
MasterDetailTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NHibernate.Dialect;
using NHibernate.DomainModel;
using NHibernate.Engine;
using NHibernate.Criterion;
using NHibernate.Mapping;
using NHibernate.Util;
using NUnit.Framework;
using Single=NHibernate.DomainModel.Single;
namespace NHibernate.Test.Legacy
{
/// <summary>
/// Summary description for MasterDetailTest.
/// </summary>
[TestFixture]
public class MasterDetailTest : TestCase
{
protected override string[] Mappings
{
get
{
return new string[]
{
"MasterDetail.hbm.xml",
"Custom.hbm.xml",
"Category.hbm.xml",
"INameable.hbm.xml",
"SingleSeveral.hbm.xml",
"WZ.hbm.xml",
"UpDown.hbm.xml",
"Eye.hbm.xml",
"MN.hbm.xml"
};
}
}
[Test]
public void ParentChildren()
{
if (!TestDialect.SupportsEmptyInsertsOrHasNonIdentityNativeGenerator)
Assert.Ignore("Support of empty inserts is required");
ISession session = OpenSession();
M parent = new M();
N n = new N();
n.String = "0";
parent.AddChildren(n);
session.SaveOrUpdate(parent);
parent.RemoveChildren(n);
session.Flush();
session.Close();
session = OpenSession();
Assert.AreEqual(0, session.CreateQuery("from N").List().Count);
session.Delete("from M");
session.Flush();
session.Close();
}
[Test]
public void OuterJoin()
{
ISession s = OpenSession();
Eye e = new Eye();
e.Name = "Eye Eye";
Jay jay = new Jay(e);
e.Jay = jay;
s.SaveOrUpdate(e);
s.Flush();
s.Close();
s = OpenSession();
e = (Eye) s.CreateCriteria(typeof(Eye)).UniqueResult();
Assert.IsTrue(NHibernateUtil.IsInitialized(e.Jay));
Assert.IsTrue(NHibernateUtil.IsInitialized(e.Jays));
s.Close();
s = OpenSession();
jay = (Jay) s.CreateQuery("select new Jay(eye) from Eye eye").UniqueResult();
Assert.AreEqual("Eye Eye", jay.Eye.Name);
s.Delete(jay.Eye);
s.Flush();
s.Close();
}
[Test]
public void Meta()
{
PersistentClass clazz = cfg.GetClassMapping(typeof(Master));
MetaAttribute meta = clazz.GetMetaAttribute("foo");
Assert.AreEqual("foo", meta.Value);
meta = clazz.GetProperty("Name").GetMetaAttribute("bar");
Assert.IsTrue(meta.IsMultiValued);
}
[Test]
public void Copy()
{
Category catWA = new Category();
catWA.Name = "HSQL workaround";
Category cat = new Category();
cat.Name = "foo";
Category subCatBar = new Category();
subCatBar.Name = "bar";
Category subCatBaz = new Category();
subCatBaz.Name = "baz";
cat.Subcategories.Add(subCatBar);
cat.Subcategories.Add(subCatBaz);
using (ISession s = OpenSession())
{
s.Save(catWA);
s.Save(cat);
s.Flush();
}
cat.Name = "new foo";
subCatBar.Name = "new bar";
cat.Subcategories.Remove(subCatBaz);
Category newCat = new Category();
newCat.Name = "new";
cat.Subcategories.Add(newCat);
Category newSubCat = new Category();
newSubCat.Name = "new sub";
newCat.Subcategories.Add(newSubCat);
Category copiedCat;
using (ISession s = OpenSession())
{
copiedCat = s.Merge(cat);
s.Flush();
}
Assert.IsFalse(copiedCat == cat);
Assert.IsTrue(cat.Subcategories.Contains(newCat));
using (ISession s = OpenSession())
{
cat = (Category) s.CreateQuery("from Category cat where cat.Name='new foo'").UniqueResult();
newSubCat = (Category)s.CreateQuery("from Category cat left join fetch cat.Subcategories where cat.Name='new sub'").UniqueResult();
Assert.AreEqual("new sub", newSubCat.Name);
}
newSubCat.Subcategories.Add(cat);
cat.Name="new new foo";
using (ISession s = OpenSession())
{
newSubCat = s.Merge(newSubCat);
Assert.IsTrue(newSubCat.Name.Equals("new sub"));
Assert.AreEqual(1, newSubCat.Subcategories.Count);
cat = (Category)newSubCat.Subcategories[0];
Assert.AreEqual("new new foo", cat.Name);
newSubCat.Subcategories.Remove(cat);
s.Delete(cat);
s.Delete(subCatBaz);
s.Delete(catWA);
s.Flush();
}
}
[Test]
public void CopyCascade()
{
ISession s = OpenSession();
Category child = new Category();
child.Name = "child";
s.Save(child);
s.Flush();
s.Close();
s = OpenSession();
Category parent = new Category();
parent.Name = "parent";
parent.Subcategories.Add(child);
child.Name = "child2";
// Save parent and cascade update detached child
Category persistentParent = s.Merge(parent);
Assert.IsTrue(persistentParent.Subcategories.Count == 1);
Assert.AreEqual(((Category) persistentParent.Subcategories[0]).Name, "child2");
s.Flush();
s.Close();
s = OpenSession();
s.Delete(persistentParent);
s.Flush();
s.Close();
}
[Test]
public void NotNullDiscriminator()
{
ISession s = OpenSession();
ITransaction t = s.BeginTransaction();
Up up = new Up();
up.Id1 = "foo";
up.Id2 = 1231;
Down down = new Down();
down.Id1 = "foo";
down.Id2 = 3211;
down.Value = 123123121;
s.Save(up);
s.Save(down);
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
IList list = s.CreateQuery("from Up up order by up.Id2 asc").List();
Assert.IsTrue(list.Count == 2);
Assert.IsFalse(list[0] is Down);
Assert.IsTrue(list[1] is Down);
list = s.CreateQuery("from Down down").List();
Assert.IsTrue(list.Count == 1);
Assert.IsTrue(list[0] is Down);
s.Delete("from Up up");
t.Commit();
s.Close();
}
[Test]
public void SelfManyToOne()
{
// add a check to not run if HSQLDialect
ISession s = OpenSession();
ITransaction t = s.BeginTransaction();
Master m = new Master();
m.OtherMaster = m;
s.Save(m);
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
IEnumerator enumer = s.CreateQuery("from m in class Master").Enumerable().GetEnumerator();
enumer.MoveNext();
m = (Master) enumer.Current;
Assert.AreSame(m, m.OtherMaster);
if (Dialect is MySQLDialect)
{
m.OtherMaster = null;
s.Flush();
}
s.Delete(m);
t.Commit();
s.Close();
}
[Test]
public void ExampleTest()
{
ISession s = OpenSession();
ITransaction t = s.BeginTransaction();
Master m = new Master();
m.Name = "name";
m.X = 5;
m.OtherMaster = m;
s.Save(m);
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
Master m1 = (Master) s.CreateCriteria(typeof(Master))
.Add(Example.Create(m).EnableLike().IgnoreCase())
.UniqueResult();
Assert.AreSame(m1.OtherMaster, m1);
m1 = (Master) s.CreateCriteria(typeof(Master))
.Add(Expression.Eq("Name", "foobar"))
.UniqueResult();
Assert.IsNull(m1);
m1 = (Master) s.CreateCriteria(typeof(Master))
.Add(Example.Create(m))
.CreateCriteria("OtherMaster")
.Add(Example.Create(m).ExcludeZeroes())
.UniqueResult();
Assert.AreSame(m1.OtherMaster, m1);
Master m2 = (Master) s.CreateCriteria(typeof(Master))
.Add(Example.Create(m).ExcludeNone())
.UniqueResult();
Assert.AreSame(m1, m2);
m.Name = null;
m2 = (Master) s.CreateCriteria(typeof(Master))
.Add(Example.Create(m).ExcludeNone())
.UniqueResult();
Assert.IsNull(m2);
if (Dialect is MySQLDialect)
{
m1.OtherMaster = null;
s.Flush();
}
s.Delete(m1);
t.Commit();
s.Close();
}
[Test]
public void NonLazyBidirectional()
{
Single sin = new Single();
sin.Id = "asfdfds";
sin.String = "adsa asdfasd";
Several sev = new Several();
sev.Id = "asdfasdfasd";
sev.String = "asd ddd";
sin.Several.Add(sev);
sev.Single = sin;
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
s.Save(sin);
t.Commit();
}
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
sin = (Single) s.Load(typeof(Single), sin);
t.Commit();
}
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
sev = (Several) s.Load(typeof(Several), sev);
t.Commit();
}
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
s.CreateQuery("from s in class Several").List();
t.Commit();
}
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
s.CreateQuery("from s in class Single").List();
t.Commit();
}
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
s.Delete("from Single");
t.Commit();
}
}
[Test]
public void CollectionQuery()
{
ISession s = OpenSession();
ITransaction t = s.BeginTransaction();
const string path = "m.Details";
if (Dialect.SupportsSubSelects)
{
s.CreateQuery("FROM m IN CLASS Master WHERE NOT EXISTS ( FROM " + path + " d WHERE NOT d.I=5 )").Enumerable();
s.CreateQuery("FROM m IN CLASS Master WHERE NOT 5 IN ( SELECT d.I FROM d IN " + path + " )").Enumerable();
}
s.CreateQuery("SELECT m FROM m in CLASS NHibernate.DomainModel.Master join m.Details d WHERE d.I=5").Enumerable();
s.CreateQuery("SELECT m FROM m in CLASS NHibernate.DomainModel.Master join m.Details d WHERE d.I=5").List();
s.CreateQuery("SELECT m.id FROM m IN CLASS NHibernate.DomainModel.Master join m.Details d WHERE d.I=5").List();
t.Commit();
s.Close();
}
[Test]
public void MasterDetail()
{
//if( dialect is Dialect.HSQLDialect ) return;
ISession s = OpenSession();
ITransaction t = s.BeginTransaction();
Master master = new Master();
Assert.IsNotNull(s.Save(master), "save returned native id");
object mid = s.GetIdentifier(master);
Detail d1 = new Detail();
d1.Master = master;
object did = s.Save(d1);
Detail d2 = new Detail();
d2.I = 12;
d2.Master = master;
Assert.IsNotNull(s.Save(d2), "generated id returned");
master.AddDetail(d1);
master.AddDetail(d2);
if (Dialect.SupportsScalarSubSelects)
{
string hql = "from d in class NHibernate.DomainModel.Detail, m in class NHibernate.DomainModel.Master " +
"where m = d.Master and m.Outgoing.size = 0 and m.Incoming.size = 0";
Assert.AreEqual(2, s.CreateQuery(hql).List().Count, "query");
}
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
master = (Master) s.Load(typeof(Master), mid);
IEnumerator enumer = master.Details.GetEnumerator();
int i = 0;
while (enumer.MoveNext())
{
Detail d = (Detail) enumer.Current;
Assert.AreSame(master, d.Master, "master-detail");
i++;
}
Assert.AreEqual(2, i, "master-detail count");
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
Assert.AreEqual(2, s.CreateQuery("select elements(master.Details) from Master master").List().Count);
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
IList list = s.CreateQuery("from Master m left join fetch m.Details").List();
Master m = (Master) list[0];
Assert.IsTrue(NHibernateUtil.IsInitialized(m.Details), "joined fetch should initialize collection");
Assert.AreEqual(2, m.Details.Count);
list = s.CreateQuery("from Detail d inner join fetch d.Master").List();
Detail dt = (Detail) list[0];
object dtid = s.GetIdentifier(dt);
Assert.AreSame(m, dt.Master);
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
list = s.CreateQuery("select m from Master m1, Master m left join fetch m.Details where m.Name=m1.Name").List();
Assert.IsTrue(NHibernateUtil.IsInitialized(((Master) list[0]).Details));
dt = (Detail) s.Load(typeof(Detail), dtid);
Assert.IsTrue(((Master) list[0]).Details.Contains(dt));
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
list =
s.CreateQuery("select m, m1.Name from Master m1, Master m left join fetch m.Details where m.Name=m1.Name").List();
Master masterFromHql = (Master) ((object[]) list[0])[0];
Assert.IsTrue(NHibernateUtil.IsInitialized(masterFromHql.Details));
dt = (Detail) s.Load(typeof(Detail), dtid);
Assert.IsTrue(masterFromHql.Details.Contains(dt));
// This line is commentend in H3.2 tests because it work in the classic parser
// even if it as no sense ('join fetch' where the 'select' is a scalar)
// The AST check the case with an Exception
//list = s.CreateQuery("select m.id from Master m inner join fetch m.Details").List();
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
Detail dd = (Detail) s.Load(typeof(Detail), did);
master = dd.Master;
Assert.IsTrue(master.Details.Contains(dd), "detail-master");
Assert.AreEqual(2, s.CreateFilter(master.Details, "order by this.I desc").List().Count);
Assert.AreEqual(2, s.CreateFilter(master.Details, "select this where this.id > -1").List().Count);
IQuery q = s.CreateFilter(master.Details, "where this.id > :id");
q.SetInt32("id", -1);
Assert.AreEqual(2, q.List().Count);
q = s.CreateFilter(master.Details, "where this.id > :id1 and this.id < :id2");
q.SetInt32("id1", -1);
q.SetInt32("id2", 99999999);
Assert.AreEqual(2, q.List().Count);
q.SetInt32("id2", -1);
Assert.AreEqual(0, q.List().Count);
q = s.CreateFilter(master.Details, "where this.id in (:ids)");
q.SetParameterList("ids", new[] {did, (long) -1});
Assert.AreEqual(1, q.List().Count);
Assert.IsTrue(q.Enumerable().GetEnumerator().MoveNext());
Assert.AreEqual(2, s.CreateFilter(master.Details, "where this.id > -1").List().Count);
Assert.AreEqual(2, s.CreateFilter(master.Details, "select this.Master where this.id > -1").List().Count);
Assert.AreEqual(2,
s.CreateFilter(master.Details, "select m from m in class Master where this.id > -1 and this.Master=m")
.List().Count);
Assert.AreEqual(0, s.CreateFilter(master.Incoming, "where this.id > -1 and this.Name is not null").List().Count);
IQuery filter = s.CreateFilter(master.Details, "select max(this.I)");
enumer = filter.Enumerable().GetEnumerator();
Assert.IsTrue(enumer.MoveNext());
Assert.IsTrue(enumer.Current is Int32);
filter = s.CreateFilter(master.Details, "select max(this.I) group by this.id");
enumer = filter.Enumerable().GetEnumerator();
Assert.IsTrue(enumer.MoveNext());
Assert.IsTrue(enumer.Current is Int32);
filter = s.CreateFilter(master.Details, "select count(*)");
enumer = filter.Enumerable().GetEnumerator();
Assert.IsTrue(enumer.MoveNext());
Assert.IsTrue(enumer.Current is Int64);
Assert.AreEqual(2, s.CreateFilter(master.Details, "select this.Master").List().Count);
IQuery f = s.CreateFilter(master.Details, "select max(this.I) where this.I < :top and this.I>=:bottom");
f.SetInt32("top", 100);
f.SetInt32("bottom", 0);
enumer = f.Enumerable().GetEnumerator();
Assert.IsTrue(enumer.MoveNext());
Assert.AreEqual(12, enumer.Current);
f.SetInt32("top", 2);
enumer = f.Enumerable().GetEnumerator();
Assert.IsTrue(enumer.MoveNext());
Assert.AreEqual(0, enumer.Current);
f = s.CreateFilter(master.Details, "select max(this.I) where this.I not in (:list)");
f.SetParameterList("list", new List<int> {-666, 22, 0});
enumer = f.Enumerable().GetEnumerator();
Assert.IsTrue(enumer.MoveNext());
Assert.AreEqual(12, enumer.Current);
i = 0;
foreach (Detail d in master.Details)
{
Assert.AreSame(master, d.Master, "master-detail");
s.Delete(d);
i++;
}
Assert.AreEqual(2, i, "master-detail");
s.Delete(master);
t.Commit();
s.Close();
}
[Test]
public void IncomingOutgoing()
{
//if HSQLDialect skip test
ISession s = OpenSession();
Master master1 = new Master();
Master master2 = new Master();
Master master3 = new Master();
s.Save(master1);
s.Save(master2);
s.Save(master3);
master1.AddIncoming(master2);
master2.AddOutgoing(master1);
master1.AddIncoming(master3);
master3.AddOutgoing(master1);
object m1id = s.GetIdentifier(master1);
Assert.AreEqual(2, s.CreateFilter(master1.Incoming, "where this.id > 0 and this.Name is not null").List().Count);
s.Flush();
s.Close();
s = OpenSession();
master1 = (Master) s.Load(typeof(Master), m1id);
int i = 0;
foreach (Master m in master1.Incoming)
{
Assert.AreEqual(1, m.Outgoing.Count, "outgoing");
Assert.IsTrue(m.Outgoing.Contains(master1), "outgoing");
s.Delete(m);
i++;
}
Assert.AreEqual(2, i, "incoming-outgoing");
s.Delete(master1);
s.Flush();
s.Close();
}
[Test]
public void Cascading()
{
//HSQLDialect return;
ISession s = OpenSession();
Detail d1 = new Detail();
Detail d2 = new Detail();
d2.I = 22;
Master m = new Master();
Master m0 = new Master();
object m0id = s.Save(m0);
m0.AddDetail(d1);
m0.AddDetail(d2);
d1.Master = m0;
d2.Master = m0;
m.MoreDetails.Add(d1);
m.MoreDetails.Add(d2);
object mid = s.Save(m);
s.Flush();
s.Close();
s = OpenSession();
m = (Master) s.Load(typeof(Master), mid);
Assert.AreEqual(2, m.MoreDetails.Count, "cascade save");
IEnumerator enumer = m.MoreDetails.GetEnumerator();
enumer.MoveNext();
Assert.AreEqual(2, ((Detail) enumer.Current).Master.Details.Count, "cascade save");
s.Delete(m);
s.Delete(s.Load(typeof(Master), m0id));
s.Flush();
s.Close();
}
[Test]
public void NamedQuery()
{
ISession s = OpenSession();
IQuery q = s.GetNamedQuery("all_details");
q.List();
s.Close();
}
[Test]
public void Serialization()
{
ISession s = OpenSession();
Master m = new Master();
Detail d1 = new Detail();
Detail d2 = new Detail();
object mid = s.Save(m);
d1.Master = (m);
d2.Master = (m);
m.AddDetail(d1);
m.AddDetail(d2);
if (Dialect is MsSql2000Dialect)
{
s.Save(d1);
}
else
{
s.Save(d1, 666L);
}
s.Flush();
s.Disconnect();
MemoryStream stream = new MemoryStream();
var f = new BinaryFormatter
{
#if !NETFX
SurrogateSelector = new SerializationHelper.SurrogateSelector()
#endif
};
f.Serialize(stream, s);
s.Close();
stream.Position = 0;
Console.WriteLine(stream.Length);
s = (ISession) f.Deserialize(stream);
stream.Close();
s.Reconnect();
Master m2 = (Master) s.Load(typeof(Master), mid);
Assert.IsTrue(m2.Details.Count == 2, "serialized state");
foreach (Detail d in m2.Details)
{
Assert.IsTrue(d.Master == m2, "deserialization");
try
{
s.GetIdentifier(d);
s.Delete(d);
}
catch (Exception)
{
// Why are we ignoring exceptions here? /Oskar 2014-08-24
}
}
s.Delete(m2);
s.Flush();
s.Close();
s = OpenSession();
mid = s.Save(new Master());
object mid2 = s.Save(new Master());
s.Flush();
s.Disconnect();
stream = new MemoryStream();
f.Serialize(stream, s);
s.Close();
stream.Position = 0;
s = (ISession) f.Deserialize(stream);
stream.Close();
s.Reconnect();
s.Delete(s.Load(typeof(Master), mid));
s.Delete(s.Load(typeof(Master), mid2));
s.Flush();
s.Close();
s = OpenSession();
string db = s.Connection.Database; //force session to grab a connection
try
{
stream = new MemoryStream();
f.Serialize(stream, s);
}
catch (Exception e)
{
Assert.IsTrue(e is InvalidOperationException, "illegal state");
s.Close();
return;
}
finally
{
stream.Close();
}
Assert.IsTrue(false, "serialization should have failed");
}
[Test]
public void UpdateLazyCollections()
{
// if (dialect is HSQLDialect) return;
ISession s = OpenSession();
Master m = new Master();
Detail d1 = new Detail();
Detail d2 = new Detail();
d2.X = 14;
object mid = s.Save(m);
// s.Flush(); commented out in h2.0.3 also
d1.Master = m;
d2.Master = m;
m.AddDetail(d1);
m.AddDetail(d2);
if (Dialect is MsSql2000Dialect)
{
s.Save(d1);
s.Save(d2);
}
else
{
s.Save(d1, 666L);
s.Save(d2, 667L);
}
s.Flush();
s.Close();
s = OpenSession();
m = (Master) s.Load(typeof(Master), mid);
s.Close();
m.Name = "New Name";
s = OpenSession();
s.Update(m, mid);
IEnumerator enumer = m.Details.GetEnumerator();
int i = 0;
while (enumer.MoveNext())
{
Assert.IsNotNull(enumer.Current);
i++;
}
Assert.AreEqual(2, i);
enumer = m.Details.GetEnumerator();
while (enumer.MoveNext())
{
s.Delete(enumer.Current);
}
s.Delete(m);
s.Flush();
s.Close();
}
[Test]
public void MultiLevelCascade()
{
//if( dialect is Dialect.HSQLDialect ) return;
object mid;
object m0id;
using (ISession s = OpenSession())
{
Detail detail = new Detail();
SubDetail subdetail = new SubDetail();
Master m = new Master();
Master m0 = new Master();
m0id = s.Save(m0);
m0.AddDetail(detail);
detail.Master = m0;
m.MoreDetails.Add(detail);
detail.SubDetails = new HashSet<SubDetail> { subdetail };
mid = s.Save(m);
s.Flush();
}
using (ISession s = OpenSession())
{
Master m = (Master) s.Load(typeof(Master), mid);
IEnumerator enumer = m.MoreDetails.GetEnumerator();
enumer.MoveNext();
Assert.IsTrue(((Detail) enumer.Current).SubDetails.Count != 0);
s.Delete(m);
s.Delete(s.Load(typeof(Master), m0id));
s.Flush();
}
}
[Test]
public void MixNativeAssigned()
{
// if HSQLDialect then skip test
ISession s = OpenSession();
Category c = new Category();
c.Name = "NAME";
Assignable assn = new Assignable();
assn.Id = "i.d.";
assn.Categories = new List<Category> {c};
c.Assignable = assn;
s.Save(assn);
s.Flush();
s.Close();
s = OpenSession();
s.Delete(assn);
s.Flush();
s.Close();
}
[Test]
public void CollectionReplaceOnUpdate()
{
if (!TestDialect.SupportsEmptyInsertsOrHasNonIdentityNativeGenerator)
Assert.Ignore("Support of empty inserts is required");
ISession s = OpenSession();
ITransaction t = s.BeginTransaction();
Category c = new Category();
IList<Category> list = new List<Category>();
c.Subcategories = list;
list.Add(new Category());
s.Save(c);
t.Commit();
s.Close();
c.Subcategories = list;
s = OpenSession();
t = s.BeginTransaction();
s.Update(c);
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
c = (Category) s.Load(typeof(Category), c.Id, LockMode.Upgrade);
IList<Category> list2 = c.Subcategories;
t.Commit();
s.Close();
Assert.IsFalse(NHibernateUtil.IsInitialized(c.Subcategories));
c.Subcategories = list2;
s = OpenSession();
t = s.BeginTransaction();
s.Update(c);
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
c = (Category) s.Load(typeof(Category), c.Id, LockMode.Upgrade);
Assert.AreEqual(1, c.Subcategories.Count);
s.Delete(c);
t.Commit();
s.Close();
}
[Test]
public void CollectionReplace2()
{
if (!TestDialect.SupportsEmptyInsertsOrHasNonIdentityNativeGenerator)
Assert.Ignore("Support of empty inserts is required");
ISession s = OpenSession();
ITransaction t = s.BeginTransaction();
Category c = new Category();
IList<Category> list = new List<Category>();
c.Subcategories = list;
list.Add(new Category());
Category c2 = new Category();
s.Save(c2);
s.Save(c);
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
c = (Category) s.Load(typeof(Category), c.Id, LockMode.Upgrade);
IList<Category> list2 = c.Subcategories;
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
c2 = (Category) s.Load(typeof(Category), c2.Id, LockMode.Upgrade);
c2.Subcategories = list2;
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
c2 = (Category) s.Load(typeof(Category), c2.Id, LockMode.Upgrade);
Assert.AreEqual(1, c2.Subcategories.Count);
s.Delete(c2);
s.Delete(s.Load(typeof(Category), c.Id));
t.Commit();
s.Close();
}
[Test]
public void CollectionReplace()
{
if (!TestDialect.SupportsEmptyInsertsOrHasNonIdentityNativeGenerator)
Assert.Ignore("Support of empty inserts is required");
ISession s = OpenSession();
ITransaction t = s.BeginTransaction();
Category c = new Category();
IList<Category> list = new List<Category>();
c.Subcategories = list;
list.Add(new Category());
s.Save(c);
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
c = (Category) s.Load(typeof(Category), c.Id, LockMode.Upgrade);
c.Subcategories = list;
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
c = (Category) s.Load(typeof(Category), c.Id, LockMode.Upgrade);
IList<Category> list2 = c.Subcategories;
t.Commit();
s.Close();
Assert.IsFalse(NHibernateUtil.IsInitialized(c.Subcategories));
s = OpenSession();
t = s.BeginTransaction();
c = (Category) s.Load(typeof(Category), c.Id, LockMode.Upgrade);
c.Subcategories = list2;
t.Commit();
s.Close();
s = OpenSession();
t = s.BeginTransaction();
c = (Category) s.Load(typeof(Category), c.Id, LockMode.Upgrade);
Assert.AreEqual(1, c.Subcategories.Count);
s.Delete(c);
t.Commit();
s.Close();
}
[Test]
public void Categories()
{
if (!TestDialect.SupportsEmptyInsertsOrHasNonIdentityNativeGenerator)
Assert.Ignore("Support of empty inserts is required");
Category c = new Category();
c.Name = Category.RootCategory;
Category c1 = new Category();
Category c2 = new Category();
Category c3 = new Category();
c.Subcategories.Add(c1);
c.Subcategories.Add(c2);
c2.Subcategories.Add(null);
c2.Subcategories.Add(c3);