/
DetachedCriteria.cs
283 lines (240 loc) · 6.85 KB
/
DetachedCriteria.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
using System;
using NHibernate.Engine;
using NHibernate.Impl;
using NHibernate.SqlCommand;
using NHibernate.Transform;
namespace NHibernate.Criterion
{
/// <summary>
/// Some applications need to create criteria queries in "detached
/// mode", where the Hibernate session is not available. This class
/// may be instantiated anywhere, and then a <c>ICriteria</c>
/// may be obtained by passing a session to
/// <c>GetExecutableCriteria()</c>. All methods have the
/// same semantics and behavior as the corresponding methods of the
/// <c>ICriteria</c> interface.
/// </summary>
[Serializable]
public class DetachedCriteria
{
private readonly CriteriaImpl impl;
private readonly ICriteria criteria;
protected DetachedCriteria(System.Type entityType)
{
impl = new CriteriaImpl(entityType, null);
criteria = impl;
}
protected DetachedCriteria(System.Type entityType, string alias)
{
impl = new CriteriaImpl(entityType, alias, null);
criteria = impl;
}
protected DetachedCriteria(string entityName)
{
impl = new CriteriaImpl(entityName, null);
criteria = impl;
}
protected DetachedCriteria(string entityName, string alias)
{
impl = new CriteriaImpl(entityName, alias, null);
criteria = impl;
}
protected internal DetachedCriteria(CriteriaImpl impl, ICriteria criteria)
{
this.impl = impl;
this.criteria = criteria;
}
internal DetachedCriteria(CriteriaImpl impl)
{
this.impl = impl;
criteria = impl;
}
/// <summary>
/// Get an executable instance of <c>Criteria</c>,
/// to actually run the query.</summary>
public ICriteria GetExecutableCriteria(ISession session)
{
impl.Session = session.GetSessionImplementation();
return impl;
}
/// <summary>
/// Get an executable instance of <c>Criteria</c>,
/// to actually run the query.</summary>
public ICriteria GetExecutableCriteria(IStatelessSession session)
{
impl.Session = session.GetSessionImplementation();
return impl;
}
public static DetachedCriteria For(System.Type entityType)
{
return new DetachedCriteria(entityType);
}
public static DetachedCriteria For<T>()
{
return new DetachedCriteria(typeof(T));
}
public static DetachedCriteria For<T>(string alias)
{
return new DetachedCriteria(typeof(T), alias);
}
public static DetachedCriteria For(System.Type entityType, string alias)
{
return new DetachedCriteria(entityType, alias);
}
public static DetachedCriteria ForEntityName(string entityName)
{
return new DetachedCriteria(entityName);
}
public static DetachedCriteria ForEntityName(string entityName, string alias)
{
return new DetachedCriteria(entityName, alias);
}
public DetachedCriteria Add(ICriterion criterion)
{
criteria.Add(criterion);
return this;
}
public DetachedCriteria AddOrder(Order order)
{
criteria.AddOrder(order);
return this;
}
public DetachedCriteria CreateAlias(string associationPath, string alias)
{
criteria.CreateAlias(associationPath, alias);
return this;
}
public DetachedCriteria CreateAlias(string associationPath, string alias, JoinType joinType)
{
criteria.CreateAlias(associationPath, alias, joinType);
return this;
}
public DetachedCriteria CreateAlias(string associationPath, string alias, JoinType joinType, ICriterion withClause)
{
criteria.CreateAlias(associationPath, alias, joinType, withClause);
return this;
}
public DetachedCriteria CreateCriteria(string associationPath, string alias)
{
return new DetachedCriteria(impl, criteria.CreateCriteria(associationPath, alias));
}
public DetachedCriteria CreateCriteria(string associationPath)
{
return new DetachedCriteria(impl, criteria.CreateCriteria(associationPath));
}
public DetachedCriteria CreateCriteria(string associationPath, JoinType joinType)
{
return new DetachedCriteria(impl, criteria.CreateCriteria(associationPath, joinType));
}
public DetachedCriteria CreateCriteria(string associationPath, string alias, JoinType joinType)
{
return new DetachedCriteria(impl, criteria.CreateCriteria(associationPath, alias, joinType));
}
public DetachedCriteria CreateCriteria(string associationPath, string alias, JoinType joinType, ICriterion withClause)
{
return new DetachedCriteria(impl, criteria.CreateCriteria(associationPath, alias, joinType, withClause));
}
public string Alias
{
get { return criteria.Alias; }
}
public string EntityOrClassName
{
get { return impl.EntityOrClassName; }
}
protected internal CriteriaImpl GetCriteriaImpl()
{
return impl;
}
public DetachedCriteria SetFetchMode(string associationPath, FetchMode mode)
{
criteria.SetFetchMode(associationPath, mode);
return this;
}
public DetachedCriteria SetLockMode(LockMode lockMode)
{
criteria.SetLockMode(lockMode);
return this;
}
public DetachedCriteria SetLockMode(string alias, LockMode lockMode)
{
criteria.SetLockMode(alias, lockMode);
return this;
}
public DetachedCriteria SetCacheMode(CacheMode cacheMode)
{
criteria.SetCacheMode(cacheMode);
return this;
}
public DetachedCriteria SetCacheRegion(string region)
{
criteria.SetCacheRegion(region);
return this;
}
public DetachedCriteria SetCacheable(bool cacheable)
{
criteria.SetCacheable(cacheable);
return this;
}
public DetachedCriteria SetProjection(IProjection projection)
{
criteria.SetProjection(projection);
return this;
}
public DetachedCriteria SetResultTransformer(IResultTransformer resultTransformer)
{
criteria.SetResultTransformer(resultTransformer);
return this;
}
public DetachedCriteria SetFirstResult(int firstResult)
{
criteria.SetFirstResult(firstResult);
return this;
}
public DetachedCriteria SetMaxResults(int maxResults)
{
criteria.SetMaxResults(maxResults);
return this;
}
public override string ToString()
{
return string.Format("DetachableCriteria({0})", criteria);
}
public DetachedCriteria GetCriteriaByPath(string path)
{
ICriteria tmpCrit = criteria.GetCriteriaByPath(path);
if (tmpCrit == null)
{
return null;
}
return new DetachedCriteria(impl, tmpCrit);
}
public DetachedCriteria GetCriteriaByAlias(string alias)
{
ICriteria tmpCrit = criteria.GetCriteriaByAlias(alias);
if (tmpCrit == null)
{
return null;
}
return new DetachedCriteria(impl, tmpCrit);
}
/// <summary>
/// Gets the root entity type if available, throws otherwise
/// </summary>
/// <remarks>
/// This is an NHibernate specific method, used by several dependent
/// frameworks for advance integration with NHibernate.
/// </remarks>
public System.Type GetRootEntityTypeIfAvailable()
{
return criteria.GetRootEntityTypeIfAvailable();
}
/// <summary>
/// Clear all orders from criteria.
/// </summary>
public void ClearOrders()
{
criteria.ClearOrders();
}
}
}