forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRootClass.cs
430 lines (389 loc) · 12.1 KB
/
RootClass.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
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Util;
namespace NHibernate.Mapping
{
/// <summary>
/// Declaration of a System.Type mapped with the <c><class></c> element that
/// is the root class of a table-per-subclass, or table-per-concrete-class
/// inheritance hierarchy.
/// </summary>
[Serializable]
public class RootClass : PersistentClass, ITableOwner
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(RootClass));
/// <summary>
/// The default name of the column for the Identifier
/// </summary>
/// <value><c>id</c> is the default column name for the Identifier.</value>
public const string DefaultIdentifierColumnName = "id";
/// <summary>
/// The default name of the column for the Discriminator
/// </summary>
/// <value><c>class</c> is the default column name for the Discriminator.</value>
public const string DefaultDiscriminatorColumnName = "class";
private Property identifierProperty;
private IKeyValue identifier;
private Property version;
private bool polymorphic;
private string cacheConcurrencyStrategy;
private string cacheRegionName;
private bool lazyPropertiesCacheable = true;
private IValue discriminator;
private bool mutable;
private bool embeddedIdentifier = false;
private bool explicitPolymorphism;
private System.Type entityPersisterClass;
private bool forceDiscriminator;
private string where;
private Table table;
private bool discriminatorInsertable = true;
private int nextSubclassId = 0;
public override int SubclassId
{
get { return 0; }
}
/// <summary>
/// Gets a boolean indicating if this mapped class is inherited from another.
/// </summary>
/// <value>
/// <see langword="false" /> because this is the root mapped class.
/// </value>
public override bool IsInherited
{
get { return false; }
}
/// <summary>
/// Gets an <see cref="ICollection"/> of <see cref="Property"/> objects that this mapped class contains.
/// </summary>
/// <value>
/// An <see cref="ICollection"/> of <see cref="Property"/> objects that
/// this mapped class contains.
/// </value>
public override IEnumerable<Property> PropertyClosureIterator
{
get { return PropertyIterator; }
}
/// <summary>
/// Gets an <see cref="ICollection"/> of <see cref="Table"/> objects that this
/// mapped class reads from and writes to.
/// </summary>
/// <value>
/// An <see cref="ICollection"/> of <see cref="Table"/> objects that
/// this mapped class reads from and writes to.
/// </value>
/// <remarks>
/// There is only one <see cref="Table"/> in the <see cref="ICollection"/> since
/// this is the root class.
/// </remarks>
public override IEnumerable<Table> TableClosureIterator
{
get { return new[] {Table}; }
}
public override IEnumerable<IKeyValue> KeyClosureIterator
{
get { return new[] {Key}; }
}
/// <summary>
/// Gets a boolean indicating if the mapped class has a version property.
/// </summary>
/// <value><see langword="true" /> if there is a Property for a <c>version</c>.</value>
public override bool IsVersioned
{
get { return version != null; }
}
public override System.Type EntityPersisterClass
{
get { return entityPersisterClass; }
set { entityPersisterClass = value; }
}
/// <summary>
/// Gets the <see cref="Table"/> of the class
/// that is mapped in the <c>class</c> element.
/// </summary>
/// <value>
/// The <see cref="Table"/> of the class this mapped class.
/// </value>
public override Table RootTable
{
get { return Table; }
}
/// <summary>
/// Gets or sets a boolean indicating if the identifier is
/// embedded in the class.
/// </summary>
/// <value><see langword="true" /> if the class identifies itself.</value>
/// <remarks>
/// An embedded identifier is true when using a <c>composite-id</c> specifying
/// properties of the class as the <c>key-property</c> instead of using a class
/// as the <c>composite-id</c>.
/// </remarks>
public override bool HasEmbeddedIdentifier
{
get { return embeddedIdentifier; }
set { embeddedIdentifier = value; }
}
/// <summary>
/// Gets or sets the cache region name.
/// </summary>
/// <value>The region name used with the Cache.</value>
public string CacheRegionName
{
get { return cacheRegionName ?? EntityName; }
set { cacheRegionName = value; }
}
/// <summary>
///
/// </summary>
public override bool IsJoinedSubclass
{
get { return false; }
}
public virtual ISet<Table> IdentityTables
{
get
{
ISet<Table> tables = new HashSet<Table>();
foreach (PersistentClass clazz in SubclassClosureIterator)
{
if (!clazz.IsAbstract.GetValueOrDefault())
tables.Add(clazz.IdentityTable);
}
return tables;
}
}
internal override int NextSubclassId()
{
return ++nextSubclassId;
}
public override Table Table
{
get { return table; }
}
Table ITableOwner.Table
{
set { table = value; }
}
/// <summary>
/// Gets or sets the <see cref="Property"/> that is used as the <c>id</c>.
/// </summary>
/// <value>
/// The <see cref="Property"/> that is used as the <c>id</c>.
/// </value>
public override Property IdentifierProperty
{
get { return identifierProperty; }
set
{
identifierProperty = value;
identifierProperty.PersistentClass = this;
}
}
/// <summary>
/// Gets or sets the <see cref="SimpleValue"/> that contains information about the identifier.
/// </summary>
/// <value>The <see cref="SimpleValue"/> that contains information about the identifier.</value>
public override IKeyValue Identifier
{
get { return identifier; }
set { identifier = value; }
}
/// <summary>
/// Gets a boolean indicating if the mapped class has a Property for the <c>id</c>.
/// </summary>
/// <value><see langword="true" /> if there is a Property for the <c>id</c>.</value>
public override bool HasIdentifierProperty
{
get { return identifierProperty != null; }
}
/// <summary>
/// Gets or sets the <see cref="SimpleValue"/> that contains information about the discriminator.
/// </summary>
/// <value>The <see cref="SimpleValue"/> that contains information about the discriminator.</value>
public override IValue Discriminator
{
get { return discriminator; }
set { discriminator = value; }
}
/// <summary>
/// Gets or sets if the mapped class has subclasses.
/// </summary>
/// <value>
/// <see langword="true" /> if the mapped class has subclasses.
/// </value>
public override bool IsPolymorphic
{
get { return polymorphic; }
set { polymorphic = value; }
}
/// <summary>
/// Gets the <see cref="RootClazz"/> of the class that is mapped in the <c>class</c> element.
/// </summary>
/// <value>
/// <c>this</c> since this is the root mapped class.
/// </value>
public override RootClass RootClazz
{
get { return this; }
}
/// <summary>
/// Adds a <see cref="Subclass"/> to the class hierarchy.
/// </summary>
/// <param name="subclass">The <see cref="Subclass"/> to add to the hierarchy.</param>
/// <remarks>
/// When a <see cref="Subclass"/> is added this mapped class has the property <see cref="IsPolymorphic"/>
/// set to <see langword="true" />.
/// </remarks>
public override void AddSubclass(Subclass subclass)
{
base.AddSubclass(subclass);
polymorphic = true;
}
/// <summary>
/// Gets or sets a boolean indicating if explicit polymorphism should be used in Queries.
/// </summary>
/// <value>
/// <see langword="true" /> if only classes queried on should be returned, <see langword="false" />
/// if any class in the hierarchy should implicitly be returned.
/// </value>
public override bool IsExplicitPolymorphism
{
get { return explicitPolymorphism; }
set { explicitPolymorphism = value; }
}
/// <summary>
/// Gets or sets the <see cref="Property"/> that is used as the version.
/// </summary>
/// <value>The <see cref="Property"/> that is used as the version.</value>
public override Property Version
{
get { return version; }
set { version = value; }
}
/// <summary>
/// Gets or set a boolean indicating if the mapped class has properties that can be changed.
/// </summary>
/// <value><see langword="true" /> if the object is mutable.</value>
public override bool IsMutable
{
get { return mutable; }
set { mutable = value; }
}
/// <summary>
/// Gets or sets the <see cref="PersistentClass"/> that this mapped class is extending.
/// </summary>
/// <value>
/// <see langword="null" /> since this is the root class.
/// </value>
/// <exception cref="InvalidOperationException">
/// Thrown when the setter is called. The Superclass can not be set on the
/// RootClass, only the SubclassType can have a Superclass set.
/// </exception>
public override PersistentClass Superclass
{
get { return null; }
set { throw new InvalidOperationException("Can not set the Superclass on a RootClass."); }
}
/// <summary>
/// Gets or sets the <see cref="SimpleValue"/> that contains information about the Key.
/// </summary>
/// <value>The <see cref="SimpleValue"/> that contains information about the Key.</value>
public override IKeyValue Key
{
get { return Identifier; }
set { throw new InvalidOperationException(); }
}
/// <summary>
///
/// </summary>
public override bool IsDiscriminatorInsertable
{
get { return discriminatorInsertable; }
set { discriminatorInsertable = value; }
}
/// <summary>
/// Gets or sets a boolean indicating if only values in the discriminator column that
/// are mapped will be included in the sql.
/// </summary>
/// <value><see langword="true" /> if the mapped discriminator values should be forced.</value>
public override bool IsForceDiscriminator
{
get { return forceDiscriminator; }
set { forceDiscriminator = value; }
}
/// <summary>
/// Gets or sets the sql string that should be a part of the where clause.
/// </summary>
/// <value>
/// The sql string that should be a part of the where clause.
/// </value>
public override string Where
{
get { return where; }
set { where = value; }
}
/// <summary>
///
/// </summary>
/// <param name="mapping"></param>
public override void Validate(IMapping mapping)
{
base.Validate(mapping);
if (!Identifier.IsValid(mapping))
{
throw new MappingException(
string.Format("identifier mapping has wrong number of columns: {0} type: {1}", EntityName,
Identifier.Type.Name));
}
CheckCompositeIdentifier();
}
private void CheckCompositeIdentifier()
{
Component id = Identifier as Component;
if (id!=null)
{
if (!id.IsDynamic)
{
System.Type idClass = id.ComponentClass;
if (idClass != null && !ReflectHelper.OverridesEquals(idClass))
{
log.Warn("composite-id class does not override Equals(): {0}", id.ComponentClass.FullName);
}
if (!ReflectHelper.OverridesGetHashCode(idClass))
{
log.Warn("composite-id class does not override GetHashCode(): {0}", id.ComponentClass.FullName);
}
// NH: Not ported
//if (!typeof(System.Runtime.Serialization.ISerializable).IsAssignableFrom(idClass))
//{
// throw new MappingException("composite-id class must implement Serializable: " + id.ComponentClass.FullName);
//}
}
}
}
/// <summary>
/// Gets or sets the CacheConcurrencyStrategy
/// to use to read/write instances of the persistent class to the Cache.
/// </summary>
/// <value>The CacheConcurrencyStrategy used with the Cache.</value>
public override string CacheConcurrencyStrategy
{
get { return cacheConcurrencyStrategy; }
set { cacheConcurrencyStrategy = value; }
}
public override bool IsLazyPropertiesCacheable
{
get { return lazyPropertiesCacheable; }
}
public void SetLazyPropertiesCacheable(bool isLazyPropertiesCacheable)
{
lazyPropertiesCacheable = isLazyPropertiesCacheable;
}
public override object Accept(IPersistentClassVisitor mv)
{
return mv.Accept(this);
}
}
}