1+ using System ;
2+ using System . Linq ;
3+ using NHibernate . Cfg . MappingSchema ;
4+ using NHibernate . Linq ;
5+ using NHibernate . Mapping . ByCode ;
6+ using NUnit . Framework ;
7+
8+ namespace NHibernate . Test . NHSpecificTest . NH3093
9+ {
10+ public class Fixture : TestCaseMappingByCode
11+ {
12+ protected override HbmMapping GetMappings ( )
13+ {
14+ var mapper = new ModelMapper ( ) ;
15+ mapper . Class < Product > ( cm =>
16+ {
17+ cm . Id ( x => x . Id , m => m . Generator ( Generators . GuidComb ) ) ;
18+ cm . Property ( x => x . Name ) ;
19+ cm . ManyToOne ( x => x . Family ) ;
20+ } ) ;
21+ mapper . Class < Family > ( cm =>
22+ {
23+ cm . Id ( x => x . Id , m => m . Generator ( Generators . GuidComb ) ) ;
24+ cm . Property ( x => x . Name ) ;
25+ cm . ManyToOne ( x => x . Segment ) ;
26+ cm . Set ( x => x . Products , m => { } , m => m . OneToMany ( ) ) ;
27+ cm . Set ( x => x . Cultivations , m => { } , m => m . OneToMany ( ) ) ;
28+ } ) ;
29+ mapper . Class < Cultivation > ( cm =>
30+ {
31+ cm . Id ( x => x . Id , m => m . Generator ( Generators . GuidComb ) ) ;
32+ cm . Property ( x => x . Name ) ;
33+ cm . ManyToOne ( x => x . Family ) ;
34+ } ) ;
35+ mapper . Class < Segment > ( cm =>
36+ {
37+ cm . Id ( x => x . Id , m => m . Generator ( Generators . GuidComb ) ) ;
38+ cm . Property ( x => x . Name ) ;
39+ cm . Set ( x => x . Families , m => { } , m => m . OneToMany ( ) ) ;
40+ } ) ;
41+
42+ return mapper . CompileMappingForAllExplicitlyAddedEntities ( ) ;
43+ }
44+
45+ protected override void OnSetUp ( )
46+ {
47+ using ( var session = OpenSession ( ) )
48+ using ( var transaction = session . BeginTransaction ( ) )
49+ {
50+ var s = new Segment { Name = "segment 1" } ;
51+ session . Save ( s ) ;
52+
53+ var f = new Family { Name = "fam 1" , Segment = s } ;
54+ session . Save ( f ) ;
55+
56+ var c = new Cultivation { Name = "Sample" , Family = f } ;
57+ session . Save ( c ) ;
58+
59+ var p1 = new Product { Name = "product 1" , Family = f } ;
60+ session . Save ( p1 ) ;
61+
62+ var p2 = new Product { Name = "product 2" } ;
63+ session . Save ( p2 ) ;
64+
65+ session . Flush ( ) ;
66+ transaction . Commit ( ) ;
67+ }
68+ }
69+
70+ protected override void OnTearDown ( )
71+ {
72+ using ( var session = OpenSession ( ) )
73+ using ( var transaction = session . BeginTransaction ( ) )
74+ {
75+ session . Delete ( "from System.Object" ) ;
76+
77+ session . Flush ( ) ;
78+ transaction . Commit ( ) ;
79+ }
80+ }
81+
82+ [ Test ]
83+ public void Linq11 ( )
84+ {
85+ using ( var session = OpenSession ( ) )
86+ using ( session . BeginTransaction ( ) )
87+ {
88+ var cultivationsId = session . Query < Cultivation > ( ) . Select ( c => c . Id ) . ToArray ( ) ;
89+
90+ var products = ( from p in session . Query < Product > ( )
91+ where p . Family . Cultivations . Any ( c => cultivationsId . Contains ( c . Id ) )
92+ && p . Family . Segment . Name == "segment 1"
93+ select p ) . ToList ( ) ;
94+
95+ Assert . AreEqual ( 1 , products . Count ) ;
96+ }
97+ }
98+
99+ [ Test ]
100+ public void Linq12 ( )
101+ {
102+ using ( var session = OpenSession ( ) )
103+ using ( session . BeginTransaction ( ) )
104+ {
105+ var cultivationsId = session . Query < Cultivation > ( ) . Select ( c => c . Id ) . ToArray ( ) ;
106+
107+ var products = ( from p in session . Query < Product > ( )
108+ where p . Family . Cultivations . Any ( c => cultivationsId . Contains ( c . Id ) )
109+ orderby p . Family . Segment . Name
110+ select p ) . ToList ( ) ;
111+
112+ Assert . AreEqual ( 1 , products . Count ) ;
113+ }
114+ }
115+
116+ [ Test ]
117+ public void Linq2 ( )
118+ {
119+ using ( var session = OpenSession ( ) )
120+ using ( session . BeginTransaction ( ) )
121+ {
122+ var products = ( from p in session . Query < Product > ( )
123+ where p . Family . Cultivations . Any ( c => c . Name == "Sample" )
124+ && p . Family . Segment . Name == "segment 1"
125+ select p ) . ToList ( ) ;
126+
127+ Assert . AreEqual ( 1 , products . Count ) ;
128+ }
129+ }
130+ }
131+ }
0 commit comments