-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Collection.java
659 lines (532 loc) · 16.1 KB
/
Collection.java
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
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.mapping;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import org.hibernate.FetchMode;
import org.hibernate.MappingException;
import org.hibernate.cfg.Mappings;
import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.internal.FilterConfiguration;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.internal.util.collections.EmptyIterator;
import org.hibernate.type.CollectionType;
import org.hibernate.type.Type;
/**
* Mapping for a collection. Subclasses specialize to particular collection styles.
*
* @author Gavin King
*/
public abstract class Collection implements Fetchable, Value, Filterable {
public static final String DEFAULT_ELEMENT_COLUMN_NAME = "elt";
public static final String DEFAULT_KEY_COLUMN_NAME = "id";
private final Mappings mappings;
private PersistentClass owner;
private KeyValue key;
private Value element;
private Table collectionTable;
private String role;
private boolean lazy;
private boolean extraLazy;
private boolean inverse;
private boolean mutable = true;
private boolean subselectLoadable;
private String cacheConcurrencyStrategy;
private String cacheRegionName;
private String orderBy;
private String where;
private String manyToManyWhere;
private String manyToManyOrderBy;
private String referencedPropertyName;
private String nodeName;
private String elementNodeName;
private boolean sorted;
private Comparator comparator;
private String comparatorClassName;
private boolean orphanDelete;
private int batchSize = -1;
private FetchMode fetchMode;
private boolean embedded = true;
private boolean optimisticLocked = true;
private Class collectionPersisterClass;
private String typeName;
private Properties typeParameters;
private final java.util.List filters = new ArrayList();
private final java.util.List manyToManyFilters = new ArrayList();
private final java.util.Set synchronizedTables = new HashSet();
private String customSQLInsert;
private boolean customInsertCallable;
private ExecuteUpdateResultCheckStyle insertCheckStyle;
private String customSQLUpdate;
private boolean customUpdateCallable;
private ExecuteUpdateResultCheckStyle updateCheckStyle;
private String customSQLDelete;
private boolean customDeleteCallable;
private ExecuteUpdateResultCheckStyle deleteCheckStyle;
private String customSQLDeleteAll;
private boolean customDeleteAllCallable;
private ExecuteUpdateResultCheckStyle deleteAllCheckStyle;
private String loaderName;
protected Collection(Mappings mappings, PersistentClass owner) {
this.mappings = mappings;
this.owner = owner;
}
public Mappings getMappings() {
return mappings;
}
public boolean isSet() {
return false;
}
public KeyValue getKey() {
return key;
}
public Value getElement() {
return element;
}
public boolean isIndexed() {
return false;
}
public Table getCollectionTable() {
return collectionTable;
}
public void setCollectionTable(Table table) {
this.collectionTable = table;
}
public boolean isSorted() {
return sorted;
}
public Comparator getComparator() {
if ( comparator == null && comparatorClassName != null ) {
try {
setComparator( (Comparator) ReflectHelper.classForName( comparatorClassName ).newInstance() );
}
catch ( Exception e ) {
throw new MappingException(
"Could not instantiate comparator class [" + comparatorClassName
+ "] for collection " + getRole()
);
}
}
return comparator;
}
public boolean isLazy() {
return lazy;
}
public void setLazy(boolean lazy) {
this.lazy = lazy;
}
public String getRole() {
return role;
}
public abstract CollectionType getDefaultCollectionType() throws MappingException;
public boolean isPrimitiveArray() {
return false;
}
public boolean isArray() {
return false;
}
public boolean hasFormula() {
return false;
}
public boolean isOneToMany() {
return element instanceof OneToMany;
}
public boolean isInverse() {
return inverse;
}
public String getOwnerEntityName() {
return owner.getEntityName();
}
public String getOrderBy() {
return orderBy;
}
public void setComparator(Comparator comparator) {
this.comparator = comparator;
}
public void setElement(Value element) {
this.element = element;
}
public void setKey(KeyValue key) {
this.key = key;
}
public void setOrderBy(String orderBy) {
this.orderBy = orderBy;
}
public void setRole(String role) {
this.role = role==null ? null : role.intern();
}
public void setSorted(boolean sorted) {
this.sorted = sorted;
}
public void setInverse(boolean inverse) {
this.inverse = inverse;
}
public PersistentClass getOwner() {
return owner;
}
/**
* @deprecated Inject the owner into constructor.
*
* @param owner The owner
*/
@Deprecated
public void setOwner(PersistentClass owner) {
this.owner = owner;
}
public String getWhere() {
return where;
}
public void setWhere(String where) {
this.where = where;
}
public String getManyToManyWhere() {
return manyToManyWhere;
}
public void setManyToManyWhere(String manyToManyWhere) {
this.manyToManyWhere = manyToManyWhere;
}
public String getManyToManyOrdering() {
return manyToManyOrderBy;
}
public void setManyToManyOrdering(String orderFragment) {
this.manyToManyOrderBy = orderFragment;
}
public boolean isIdentified() {
return false;
}
public boolean hasOrphanDelete() {
return orphanDelete;
}
public void setOrphanDelete(boolean orphanDelete) {
this.orphanDelete = orphanDelete;
}
public int getBatchSize() {
return batchSize;
}
public void setBatchSize(int i) {
batchSize = i;
}
public FetchMode getFetchMode() {
return fetchMode;
}
public void setFetchMode(FetchMode fetchMode) {
this.fetchMode = fetchMode;
}
public void setCollectionPersisterClass(Class persister) {
this.collectionPersisterClass = persister;
}
public Class getCollectionPersisterClass() {
return collectionPersisterClass;
}
public void validate(Mapping mapping) throws MappingException {
if ( getKey().isCascadeDeleteEnabled() && ( !isInverse() || !isOneToMany() ) ) {
throw new MappingException(
"only inverse one-to-many associations may use on-delete=\"cascade\": "
+ getRole() );
}
if ( !getKey().isValid( mapping ) ) {
throw new MappingException(
"collection foreign key mapping has wrong number of columns: "
+ getRole()
+ " type: "
+ getKey().getType().getName() );
}
if ( !getElement().isValid( mapping ) ) {
throw new MappingException(
"collection element mapping has wrong number of columns: "
+ getRole()
+ " type: "
+ getElement().getType().getName() );
}
checkColumnDuplication();
if ( elementNodeName!=null && elementNodeName.startsWith("@") ) {
throw new MappingException("element node must not be an attribute: " + elementNodeName );
}
if ( elementNodeName!=null && elementNodeName.equals(".") ) {
throw new MappingException("element node must not be the parent: " + elementNodeName );
}
if ( nodeName!=null && nodeName.indexOf('@')>-1 ) {
throw new MappingException("collection node must not be an attribute: " + elementNodeName );
}
}
private void checkColumnDuplication(java.util.Set distinctColumns, Iterator columns)
throws MappingException {
while ( columns.hasNext() ) {
Selectable s = (Selectable) columns.next();
if ( !s.isFormula() ) {
Column col = (Column) s;
if ( !distinctColumns.add( col.getName() ) ) {
throw new MappingException( "Repeated column in mapping for collection: "
+ getRole()
+ " column: "
+ col.getName() );
}
}
}
}
private void checkColumnDuplication() throws MappingException {
HashSet cols = new HashSet();
checkColumnDuplication( cols, getKey().getColumnIterator() );
if ( isIndexed() ) {
checkColumnDuplication( cols, ( (IndexedCollection) this )
.getIndex()
.getColumnIterator() );
}
if ( isIdentified() ) {
checkColumnDuplication( cols, ( (IdentifierCollection) this )
.getIdentifier()
.getColumnIterator() );
}
if ( !isOneToMany() ) {
checkColumnDuplication( cols, getElement().getColumnIterator() );
}
}
public Iterator getColumnIterator() {
return EmptyIterator.INSTANCE;
}
public int getColumnSpan() {
return 0;
}
public Type getType() throws MappingException {
return getCollectionType();
}
public CollectionType getCollectionType() {
if ( typeName == null ) {
return getDefaultCollectionType();
}
else {
return mappings.getTypeResolver()
.getTypeFactory()
.customCollection( typeName, typeParameters, role, referencedPropertyName, isEmbedded() );
}
}
public boolean isNullable() {
return true;
}
public boolean isAlternateUniqueKey() {
return false;
}
public Table getTable() {
return owner.getTable();
}
public void createForeignKey() {
}
public boolean isSimpleValue() {
return false;
}
public boolean isValid(Mapping mapping) throws MappingException {
return true;
}
private void createForeignKeys() throws MappingException {
// if ( !isInverse() ) { // for inverse collections, let the "other end" handle it
if ( referencedPropertyName == null ) {
getElement().createForeignKey();
key.createForeignKeyOfEntity( getOwner().getEntityName() );
}
// }
}
abstract void createPrimaryKey();
public void createAllKeys() throws MappingException {
createForeignKeys();
if ( !isInverse() ) createPrimaryKey();
}
public String getCacheConcurrencyStrategy() {
return cacheConcurrencyStrategy;
}
public void setCacheConcurrencyStrategy(String cacheConcurrencyStrategy) {
this.cacheConcurrencyStrategy = cacheConcurrencyStrategy;
}
public void setTypeUsingReflection(String className, String propertyName) {
}
public String getCacheRegionName() {
return cacheRegionName == null ? role : cacheRegionName;
}
public void setCacheRegionName(String cacheRegionName) {
this.cacheRegionName = cacheRegionName;
}
public void setCustomSQLInsert(String customSQLInsert, boolean callable, ExecuteUpdateResultCheckStyle checkStyle) {
this.customSQLInsert = customSQLInsert;
this.customInsertCallable = callable;
this.insertCheckStyle = checkStyle;
}
public String getCustomSQLInsert() {
return customSQLInsert;
}
public boolean isCustomInsertCallable() {
return customInsertCallable;
}
public ExecuteUpdateResultCheckStyle getCustomSQLInsertCheckStyle() {
return insertCheckStyle;
}
public void setCustomSQLUpdate(String customSQLUpdate, boolean callable, ExecuteUpdateResultCheckStyle checkStyle) {
this.customSQLUpdate = customSQLUpdate;
this.customUpdateCallable = callable;
this.updateCheckStyle = checkStyle;
}
public String getCustomSQLUpdate() {
return customSQLUpdate;
}
public boolean isCustomUpdateCallable() {
return customUpdateCallable;
}
public ExecuteUpdateResultCheckStyle getCustomSQLUpdateCheckStyle() {
return updateCheckStyle;
}
public void setCustomSQLDelete(String customSQLDelete, boolean callable, ExecuteUpdateResultCheckStyle checkStyle) {
this.customSQLDelete = customSQLDelete;
this.customDeleteCallable = callable;
this.deleteCheckStyle = checkStyle;
}
public String getCustomSQLDelete() {
return customSQLDelete;
}
public boolean isCustomDeleteCallable() {
return customDeleteCallable;
}
public ExecuteUpdateResultCheckStyle getCustomSQLDeleteCheckStyle() {
return deleteCheckStyle;
}
public void setCustomSQLDeleteAll(String customSQLDeleteAll, boolean callable, ExecuteUpdateResultCheckStyle checkStyle) {
this.customSQLDeleteAll = customSQLDeleteAll;
this.customDeleteAllCallable = callable;
this.deleteAllCheckStyle = checkStyle;
}
public String getCustomSQLDeleteAll() {
return customSQLDeleteAll;
}
public boolean isCustomDeleteAllCallable() {
return customDeleteAllCallable;
}
public ExecuteUpdateResultCheckStyle getCustomSQLDeleteAllCheckStyle() {
return deleteAllCheckStyle;
}
public void addFilter(String name, String condition, boolean autoAliasInjection, java.util.Map<String,String> aliasTableMap, java.util.Map<String,String> aliasEntityMap) {
filters.add(new FilterConfiguration(name, condition, autoAliasInjection, aliasTableMap, aliasEntityMap, null));
}
public java.util.List getFilters() {
return filters;
}
public void addManyToManyFilter(String name, String condition, boolean autoAliasInjection, java.util.Map<String,String> aliasTableMap, java.util.Map<String,String> aliasEntityMap) {
manyToManyFilters.add(new FilterConfiguration(name, condition, autoAliasInjection, aliasTableMap, aliasEntityMap, null));
}
public java.util.List getManyToManyFilters() {
return manyToManyFilters;
}
@Override
public String toString() {
return getClass().getName() + '(' + getRole() + ')';
}
public java.util.Set getSynchronizedTables() {
return synchronizedTables;
}
public String getLoaderName() {
return loaderName;
}
public void setLoaderName(String name) {
this.loaderName = name==null ? null : name.intern();
}
public String getReferencedPropertyName() {
return referencedPropertyName;
}
public void setReferencedPropertyName(String propertyRef) {
this.referencedPropertyName = propertyRef==null ? null : propertyRef.intern();
}
public boolean isOptimisticLocked() {
return optimisticLocked;
}
public void setOptimisticLocked(boolean optimisticLocked) {
this.optimisticLocked = optimisticLocked;
}
public boolean isMap() {
return false;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public Properties getTypeParameters() {
return typeParameters;
}
public void setTypeParameters(Properties parameterMap) {
this.typeParameters = parameterMap;
}
public boolean[] getColumnInsertability() {
return ArrayHelper.EMPTY_BOOLEAN_ARRAY;
}
public boolean[] getColumnUpdateability() {
return ArrayHelper.EMPTY_BOOLEAN_ARRAY;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public String getElementNodeName() {
return elementNodeName;
}
public void setElementNodeName(String elementNodeName) {
this.elementNodeName = elementNodeName;
}
public boolean isEmbedded() {
return embedded;
}
public void setEmbedded(boolean embedded) {
this.embedded = embedded;
}
public boolean isSubselectLoadable() {
return subselectLoadable;
}
public void setSubselectLoadable(boolean subqueryLoadable) {
this.subselectLoadable = subqueryLoadable;
}
public boolean isMutable() {
return mutable;
}
public void setMutable(boolean mutable) {
this.mutable = mutable;
}
public boolean isExtraLazy() {
return extraLazy;
}
public void setExtraLazy(boolean extraLazy) {
this.extraLazy = extraLazy;
}
public boolean hasOrder() {
return orderBy!=null || manyToManyOrderBy!=null;
}
public void setComparatorClassName(String comparatorClassName) {
this.comparatorClassName = comparatorClassName;
}
public String getComparatorClassName() {
return comparatorClassName;
}
}