~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/mapping/Collection.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// $Id: Collection.java 11495 2007-05-09 03:52:56Z steve.ebersole@jboss.com $
 
2
package org.hibernate.mapping;
 
3
 
 
4
import java.util.Comparator;
 
5
import java.util.HashMap;
 
6
import java.util.HashSet;
 
7
import java.util.Iterator;
 
8
import java.util.Properties;
 
9
 
 
10
import org.hibernate.FetchMode;
 
11
import org.hibernate.MappingException;
 
12
import org.hibernate.engine.Mapping;
 
13
import org.hibernate.engine.ExecuteUpdateResultCheckStyle;
 
14
import org.hibernate.type.CollectionType;
 
15
import org.hibernate.type.Type;
 
16
import org.hibernate.type.TypeFactory;
 
17
import org.hibernate.util.ArrayHelper;
 
18
import org.hibernate.util.EmptyIterator;
 
19
import org.hibernate.util.ReflectHelper;
 
20
 
 
21
/**
 
22
 * Mapping for a collection. Subclasses specialize to particular collection styles.
 
23
 * 
 
24
 * @author Gavin King
 
25
 */
 
26
public abstract class Collection implements Fetchable, Value, Filterable {
 
27
 
 
28
        public static final String DEFAULT_ELEMENT_COLUMN_NAME = "elt";
 
29
        public static final String DEFAULT_KEY_COLUMN_NAME = "id";
 
30
 
 
31
        private KeyValue key;
 
32
        private Value element;
 
33
        private Table collectionTable;
 
34
        private String role;
 
35
        private boolean lazy;
 
36
        private boolean extraLazy;
 
37
        private boolean inverse;
 
38
        private boolean mutable = true;
 
39
        private boolean subselectLoadable;
 
40
        private String cacheConcurrencyStrategy;
 
41
        private String cacheRegionName;
 
42
        private String orderBy;
 
43
        private String where;
 
44
        private String manyToManyWhere;
 
45
        private String manyToManyOrderBy;
 
46
        private PersistentClass owner;
 
47
        private String referencedPropertyName;
 
48
        private String nodeName;
 
49
        private String elementNodeName;
 
50
        private boolean sorted;
 
51
        private Comparator comparator;
 
52
        private String comparatorClassName;
 
53
        private boolean orphanDelete;
 
54
        private int batchSize = -1;
 
55
        private FetchMode fetchMode;
 
56
        private boolean embedded = true;
 
57
        private boolean optimisticLocked = true;
 
58
        private Class collectionPersisterClass;
 
59
        private String typeName;
 
60
        private Properties typeParameters;
 
61
        private final java.util.Map filters = new HashMap();
 
62
        private final java.util.Map manyToManyFilters = new HashMap();
 
63
        private final java.util.Set synchronizedTables = new HashSet();
 
64
 
 
65
        private String customSQLInsert;
 
66
        private boolean customInsertCallable;
 
67
        private ExecuteUpdateResultCheckStyle insertCheckStyle;
 
68
        private String customSQLUpdate;
 
69
        private boolean customUpdateCallable;
 
70
        private ExecuteUpdateResultCheckStyle updateCheckStyle;
 
71
        private String customSQLDelete;
 
72
        private boolean customDeleteCallable;
 
73
        private ExecuteUpdateResultCheckStyle deleteCheckStyle;
 
74
        private String customSQLDeleteAll;
 
75
        private boolean customDeleteAllCallable;
 
76
        private ExecuteUpdateResultCheckStyle deleteAllCheckStyle;
 
77
 
 
78
        private String loaderName;
 
79
 
 
80
        protected Collection(PersistentClass owner) {
 
81
                this.owner = owner;
 
82
        }
 
83
 
 
84
        public boolean isSet() {
 
85
                return false;
 
86
        }
 
87
 
 
88
        public KeyValue getKey() {
 
89
                return key;
 
90
        }
 
91
 
 
92
        public Value getElement() {
 
93
                return element;
 
94
        }
 
95
 
 
96
        public boolean isIndexed() {
 
97
                return false;
 
98
        }
 
99
 
 
100
        public Table getCollectionTable() {
 
101
                return collectionTable;
 
102
        }
 
103
 
 
104
        public void setCollectionTable(Table table) {
 
105
                this.collectionTable = table;
 
106
        }
 
107
 
 
108
        public boolean isSorted() {
 
109
                return sorted;
 
110
        }
 
111
 
 
112
        public Comparator getComparator() {
 
113
                if ( comparator == null && comparatorClassName != null ) {
 
114
                        try {
 
115
                                setComparator( (Comparator) ReflectHelper.classForName( comparatorClassName ).newInstance() );
 
116
                        }
 
117
                        catch ( Exception e ) {
 
118
                                throw new MappingException(
 
119
                                                "Could not instantiate comparator class [" + comparatorClassName
 
120
                                                + "] for collection " + getRole()  
 
121
                                );
 
122
                        }
 
123
                }
 
124
                return comparator;
 
125
        }
 
126
 
 
127
        public boolean isLazy() {
 
128
                return lazy;
 
129
        }
 
130
 
 
131
        public void setLazy(boolean lazy) {
 
132
                this.lazy = lazy;
 
133
        }
 
134
 
 
135
        public String getRole() {
 
136
                return role;
 
137
        }
 
138
 
 
139
        public abstract CollectionType getDefaultCollectionType() throws MappingException;
 
140
 
 
141
        public boolean isPrimitiveArray() {
 
142
                return false;
 
143
        }
 
144
 
 
145
        public boolean isArray() {
 
146
                return false;
 
147
        }
 
148
 
 
149
        public boolean hasFormula() {
 
150
                return false;
 
151
        }
 
152
 
 
153
        public boolean isOneToMany() {
 
154
                return element instanceof OneToMany;
 
155
        }
 
156
 
 
157
        public boolean isInverse() {
 
158
                return inverse;
 
159
        }
 
160
 
 
161
        public String getOwnerEntityName() {
 
162
                return owner.getEntityName();
 
163
        }
 
164
 
 
165
        public String getOrderBy() {
 
166
                return orderBy;
 
167
        }
 
168
 
 
169
        public void setComparator(Comparator comparator) {
 
170
                this.comparator = comparator;
 
171
        }
 
172
 
 
173
        public void setElement(Value element) {
 
174
                this.element = element;
 
175
        }
 
176
 
 
177
        public void setKey(KeyValue key) {
 
178
                this.key = key;
 
179
        }
 
180
 
 
181
        public void setOrderBy(String orderBy) {
 
182
                this.orderBy = orderBy;
 
183
        }
 
184
 
 
185
        public void setRole(String role) {
 
186
                this.role = role==null ? null : role.intern();
 
187
        }
 
188
 
 
189
        public void setSorted(boolean sorted) {
 
190
                this.sorted = sorted;
 
191
        }
 
192
 
 
193
        public void setInverse(boolean inverse) {
 
194
                this.inverse = inverse;
 
195
        }
 
196
 
 
197
        public PersistentClass getOwner() {
 
198
                return owner;
 
199
        }
 
200
 
 
201
        public void setOwner(PersistentClass owner) {
 
202
                this.owner = owner;
 
203
        }
 
204
 
 
205
        public String getWhere() {
 
206
                return where;
 
207
        }
 
208
 
 
209
        public void setWhere(String where) {
 
210
                this.where = where;
 
211
        }
 
212
 
 
213
        public String getManyToManyWhere() {
 
214
                return manyToManyWhere;
 
215
        }
 
216
 
 
217
        public void setManyToManyWhere(String manyToManyWhere) {
 
218
                this.manyToManyWhere = manyToManyWhere;
 
219
        }
 
220
 
 
221
        public String getManyToManyOrdering() {
 
222
                return manyToManyOrderBy;
 
223
        }
 
224
 
 
225
        public void setManyToManyOrdering(String orderFragment) {
 
226
                this.manyToManyOrderBy = orderFragment;
 
227
        }
 
228
 
 
229
        public boolean isIdentified() {
 
230
                return false;
 
231
        }
 
232
 
 
233
        public boolean hasOrphanDelete() {
 
234
                return orphanDelete;
 
235
        }
 
236
 
 
237
        public void setOrphanDelete(boolean orphanDelete) {
 
238
                this.orphanDelete = orphanDelete;
 
239
        }
 
240
 
 
241
        public int getBatchSize() {
 
242
                return batchSize;
 
243
        }
 
244
 
 
245
        public void setBatchSize(int i) {
 
246
                batchSize = i;
 
247
        }
 
248
 
 
249
        public FetchMode getFetchMode() {
 
250
                return fetchMode;
 
251
        }
 
252
 
 
253
        public void setFetchMode(FetchMode fetchMode) {
 
254
                this.fetchMode = fetchMode;
 
255
        }
 
256
 
 
257
        public void setCollectionPersisterClass(Class persister) {
 
258
                this.collectionPersisterClass = persister;
 
259
        }
 
260
 
 
261
        public Class getCollectionPersisterClass() {
 
262
                return collectionPersisterClass;
 
263
        }
 
264
 
 
265
        public void validate(Mapping mapping) throws MappingException {
 
266
                if ( getKey().isCascadeDeleteEnabled() && ( !isInverse() || !isOneToMany() ) ) {
 
267
                        throw new MappingException(
 
268
                                "only inverse one-to-many associations may use on-delete=\"cascade\": " 
 
269
                                + getRole() );
 
270
                }
 
271
                if ( !getKey().isValid( mapping ) ) {
 
272
                        throw new MappingException(
 
273
                                "collection foreign key mapping has wrong number of columns: "
 
274
                                + getRole()
 
275
                                + " type: "
 
276
                                + getKey().getType().getName() );
 
277
                }
 
278
                if ( !getElement().isValid( mapping ) ) {
 
279
                        throw new MappingException( 
 
280
                                "collection element mapping has wrong number of columns: "
 
281
                                + getRole()
 
282
                                + " type: "
 
283
                                + getElement().getType().getName() );
 
284
                }
 
285
 
 
286
                checkColumnDuplication();
 
287
                
 
288
                if ( elementNodeName!=null && elementNodeName.startsWith("@") ) {
 
289
                        throw new MappingException("element node must not be an attribute: " + elementNodeName );
 
290
                }
 
291
                if ( elementNodeName!=null && elementNodeName.equals(".") ) {
 
292
                        throw new MappingException("element node must not be the parent: " + elementNodeName );
 
293
                }
 
294
                if ( nodeName!=null && nodeName.indexOf('@')>-1 ) {
 
295
                        throw new MappingException("collection node must not be an attribute: " + elementNodeName );
 
296
                }
 
297
        }
 
298
 
 
299
        private void checkColumnDuplication(java.util.Set distinctColumns, Iterator columns)
 
300
                        throws MappingException {
 
301
                while ( columns.hasNext() ) {
 
302
                        Selectable s = (Selectable) columns.next();
 
303
                        if ( !s.isFormula() ) {
 
304
                                Column col = (Column) s;
 
305
                                if ( !distinctColumns.add( col.getName() ) ) {
 
306
                                        throw new MappingException( "Repeated column in mapping for collection: "
 
307
                                                + getRole()
 
308
                                                + " column: "
 
309
                                                + col.getName() );
 
310
                                }
 
311
                        }
 
312
                }
 
313
        }
 
314
 
 
315
        private void checkColumnDuplication() throws MappingException {
 
316
                HashSet cols = new HashSet();
 
317
                checkColumnDuplication( cols, getKey().getColumnIterator() );
 
318
                if ( isIndexed() ) {
 
319
                        checkColumnDuplication( cols, ( (IndexedCollection) this )
 
320
                                .getIndex()
 
321
                                .getColumnIterator() );
 
322
                }
 
323
                if ( isIdentified() ) {
 
324
                        checkColumnDuplication( cols, ( (IdentifierCollection) this )
 
325
                                .getIdentifier()
 
326
                                .getColumnIterator() );
 
327
                }
 
328
                if ( !isOneToMany() ) {
 
329
                        checkColumnDuplication( cols, getElement().getColumnIterator() );
 
330
                }
 
331
        }
 
332
 
 
333
        public Iterator getColumnIterator() {
 
334
                return EmptyIterator.INSTANCE;
 
335
        }
 
336
 
 
337
        public int getColumnSpan() {
 
338
                return 0;
 
339
        }
 
340
 
 
341
        public Type getType() throws MappingException {
 
342
                return getCollectionType();
 
343
        }
 
344
 
 
345
        public CollectionType getCollectionType() {
 
346
                if ( typeName == null ) {
 
347
                        return getDefaultCollectionType();
 
348
                }
 
349
                else {
 
350
                        return TypeFactory.customCollection( typeName, typeParameters, role, referencedPropertyName, isEmbedded() );
 
351
                }
 
352
        }
 
353
 
 
354
        public boolean isNullable() {
 
355
                return true;
 
356
        }
 
357
 
 
358
        public boolean isAlternateUniqueKey() {
 
359
                return false;
 
360
        }
 
361
 
 
362
        public Table getTable() {
 
363
                return owner.getTable();
 
364
        }
 
365
 
 
366
        public void createForeignKey() {
 
367
        }
 
368
 
 
369
        public boolean isSimpleValue() {
 
370
                return false;
 
371
        }
 
372
 
 
373
        public boolean isValid(Mapping mapping) throws MappingException {
 
374
                return true;
 
375
        }
 
376
 
 
377
        private void createForeignKeys() throws MappingException {
 
378
                // if ( !isInverse() ) { // for inverse collections, let the "other end" handle it
 
379
                if ( referencedPropertyName == null ) {
 
380
                        getElement().createForeignKey();
 
381
                        key.createForeignKeyOfEntity( getOwner().getEntityName() );
 
382
                }
 
383
                // }
 
384
        }
 
385
 
 
386
        abstract void createPrimaryKey();
 
387
 
 
388
        public void createAllKeys() throws MappingException {
 
389
                createForeignKeys();
 
390
                if ( !isInverse() ) createPrimaryKey();
 
391
        }
 
392
 
 
393
        public String getCacheConcurrencyStrategy() {
 
394
                return cacheConcurrencyStrategy;
 
395
        }
 
396
 
 
397
        public void setCacheConcurrencyStrategy(String cacheConcurrencyStrategy) {
 
398
                this.cacheConcurrencyStrategy = cacheConcurrencyStrategy;
 
399
        }
 
400
 
 
401
        public void setTypeUsingReflection(String className, String propertyName) {
 
402
        }
 
403
 
 
404
        public String getCacheRegionName() {
 
405
                return cacheRegionName == null ? role : cacheRegionName;
 
406
        }
 
407
 
 
408
        public void setCacheRegionName(String cacheRegionName) {
 
409
                this.cacheRegionName = cacheRegionName;
 
410
        }
 
411
 
 
412
 
 
413
 
 
414
        public void setCustomSQLInsert(String customSQLInsert, boolean callable, ExecuteUpdateResultCheckStyle checkStyle) {
 
415
                this.customSQLInsert = customSQLInsert;
 
416
                this.customInsertCallable = callable;
 
417
                this.insertCheckStyle = checkStyle;
 
418
        }
 
419
 
 
420
        public String getCustomSQLInsert() {
 
421
                return customSQLInsert;
 
422
        }
 
423
 
 
424
        public boolean isCustomInsertCallable() {
 
425
                return customInsertCallable;
 
426
        }
 
427
 
 
428
        public ExecuteUpdateResultCheckStyle getCustomSQLInsertCheckStyle() {
 
429
                return insertCheckStyle;
 
430
        }
 
431
 
 
432
        public void setCustomSQLUpdate(String customSQLUpdate, boolean callable, ExecuteUpdateResultCheckStyle checkStyle) {
 
433
                this.customSQLUpdate = customSQLUpdate;
 
434
                this.customUpdateCallable = callable;
 
435
                this.updateCheckStyle = checkStyle;
 
436
        }
 
437
 
 
438
        public String getCustomSQLUpdate() {
 
439
                return customSQLUpdate;
 
440
        }
 
441
 
 
442
        public boolean isCustomUpdateCallable() {
 
443
                return customUpdateCallable;
 
444
        }
 
445
 
 
446
        public ExecuteUpdateResultCheckStyle getCustomSQLUpdateCheckStyle() {
 
447
                return updateCheckStyle;
 
448
        }
 
449
 
 
450
        public void setCustomSQLDelete(String customSQLDelete, boolean callable, ExecuteUpdateResultCheckStyle checkStyle) {
 
451
                this.customSQLDelete = customSQLDelete;
 
452
                this.customDeleteCallable = callable;
 
453
                this.deleteCheckStyle = checkStyle;
 
454
        }
 
455
 
 
456
        public String getCustomSQLDelete() {
 
457
                return customSQLDelete;
 
458
        }
 
459
 
 
460
        public boolean isCustomDeleteCallable() {
 
461
                return customDeleteCallable;
 
462
        }
 
463
 
 
464
        public ExecuteUpdateResultCheckStyle getCustomSQLDeleteCheckStyle() {
 
465
                return deleteCheckStyle;
 
466
        }
 
467
 
 
468
        public void setCustomSQLDeleteAll(String customSQLDeleteAll, boolean callable, ExecuteUpdateResultCheckStyle checkStyle) {
 
469
                this.customSQLDeleteAll = customSQLDeleteAll;
 
470
                this.customDeleteAllCallable = callable;
 
471
                this.deleteAllCheckStyle = checkStyle;
 
472
        }
 
473
 
 
474
        public String getCustomSQLDeleteAll() {
 
475
                return customSQLDeleteAll;
 
476
        }
 
477
 
 
478
        public boolean isCustomDeleteAllCallable() {
 
479
                return customDeleteAllCallable;
 
480
        }
 
481
 
 
482
        public ExecuteUpdateResultCheckStyle getCustomSQLDeleteAllCheckStyle() {
 
483
                return deleteAllCheckStyle;
 
484
        }
 
485
 
 
486
        public void addFilter(String name, String condition) {
 
487
                filters.put( name, condition );
 
488
        }
 
489
 
 
490
        public java.util.Map getFilterMap() {
 
491
                return filters;
 
492
        }
 
493
 
 
494
        public void addManyToManyFilter(String name, String condition) {
 
495
                manyToManyFilters.put( name, condition );
 
496
        }
 
497
 
 
498
        public java.util.Map getManyToManyFilterMap() {
 
499
                return manyToManyFilters;
 
500
        }
 
501
 
 
502
        public String toString() {
 
503
                return getClass().getName() + '(' + getRole() + ')';
 
504
        }
 
505
 
 
506
        public java.util.Set getSynchronizedTables() {
 
507
                return synchronizedTables;
 
508
        }
 
509
 
 
510
        public String getLoaderName() {
 
511
                return loaderName;
 
512
        }
 
513
 
 
514
        public void setLoaderName(String name) {
 
515
                this.loaderName = name==null ? null : name.intern();
 
516
        }
 
517
 
 
518
        public String getReferencedPropertyName() {
 
519
                return referencedPropertyName;
 
520
        }
 
521
 
 
522
        public void setReferencedPropertyName(String propertyRef) {
 
523
                this.referencedPropertyName = propertyRef==null ? null : propertyRef.intern();
 
524
        }
 
525
 
 
526
        public boolean isOptimisticLocked() {
 
527
                return optimisticLocked;
 
528
        }
 
529
 
 
530
        public void setOptimisticLocked(boolean optimisticLocked) {
 
531
                this.optimisticLocked = optimisticLocked;
 
532
        }
 
533
 
 
534
        public boolean isMap() {
 
535
                return false;
 
536
        }
 
537
 
 
538
        public String getTypeName() {
 
539
                return typeName;
 
540
        }
 
541
 
 
542
        public void setTypeName(String typeName) {
 
543
                this.typeName = typeName;
 
544
        }
 
545
 
 
546
        public Properties getTypeParameters() {
 
547
                return typeParameters;
 
548
        }
 
549
 
 
550
        public void setTypeParameters(Properties parameterMap) {
 
551
                this.typeParameters = parameterMap;
 
552
        }
 
553
 
 
554
        public boolean[] getColumnInsertability() {
 
555
                return ArrayHelper.EMPTY_BOOLEAN_ARRAY;
 
556
        }
 
557
 
 
558
        public boolean[] getColumnUpdateability() {
 
559
                return ArrayHelper.EMPTY_BOOLEAN_ARRAY;
 
560
        }
 
561
 
 
562
        public String getNodeName() {
 
563
                return nodeName;
 
564
        }
 
565
 
 
566
        public void setNodeName(String nodeName) {
 
567
                this.nodeName = nodeName;
 
568
        }
 
569
 
 
570
        public String getElementNodeName() {
 
571
                return elementNodeName;
 
572
        }
 
573
 
 
574
        public void setElementNodeName(String elementNodeName) {
 
575
                this.elementNodeName = elementNodeName;
 
576
        }
 
577
 
 
578
        public boolean isEmbedded() {
 
579
                return embedded;
 
580
        }
 
581
 
 
582
        public void setEmbedded(boolean embedded) {
 
583
                this.embedded = embedded;
 
584
        }
 
585
 
 
586
        public boolean isSubselectLoadable() {
 
587
                return subselectLoadable;
 
588
        }
 
589
        
 
590
 
 
591
        public void setSubselectLoadable(boolean subqueryLoadable) {
 
592
                this.subselectLoadable = subqueryLoadable;
 
593
        }
 
594
 
 
595
        public boolean isMutable() {
 
596
                return mutable;
 
597
        }
 
598
 
 
599
        public void setMutable(boolean mutable) {
 
600
                this.mutable = mutable;
 
601
        }
 
602
 
 
603
        public boolean isExtraLazy() {
 
604
                return extraLazy;
 
605
        }
 
606
 
 
607
        public void setExtraLazy(boolean extraLazy) {
 
608
                this.extraLazy = extraLazy;
 
609
        }
 
610
        
 
611
        public boolean hasOrder() {
 
612
                return orderBy!=null || manyToManyOrderBy!=null;
 
613
        }
 
614
 
 
615
        public void setComparatorClassName(String comparatorClassName) {
 
616
                this.comparatorClassName = comparatorClassName;         
 
617
        }
 
618
        
 
619
        public String getComparatorClassName() {
 
620
                return comparatorClassName;
 
621
        }
 
622
}
 
 
b'\\ No newline at end of file'