-
Notifications
You must be signed in to change notification settings - Fork 161
/
AggregateObjectMapping.java
2246 lines (2060 loc) · 99.7 KB
/
AggregateObjectMapping.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation from Oracle TopLink
// 07/19/2011-2.2.1 Guy Pelletier
// - 338812: ManyToMany mapping in aggregate object violate integrity constraint on deletion
// 08/01/2012-2.5 Chris Delahunt
// - 371950: Metadata caching
// 10/25/2012-2.5 Guy Pelletier
// - 374688: JPA 2.1 Converter support
// 09 Jan 2013-2.5 Gordon Yorke
// - 397772: JPA 2.1 Entity Graph Support
// 02/11/2013-2.5 Guy Pelletier
// - 365931: @JoinColumn(name="FK_DEPT",insertable = false, updatable = true) causes INSERT statement to include this data value that it is associated with
// 06/03/2013-2.5.1 Guy Pelletier
// - 402380: 3 jpa21/advanced tests failed on server with
// "java.lang.NoClassDefFoundError: org/eclipse/persistence/testing/models/jpa21/advanced/enums/Gender"
// 10/19/2016-2.6 Will Dazey
// - 506168: Make sure nestedTranslation map is new reference when cloned
// 03/22/2018-2.7.2 Lukas Jungmann
// - 441498: @ElementCollection on Map<@Embeddable,String> cause NullPointerException when @Embeddable has a FK
package org.eclipse.persistence.mappings;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.descriptors.FetchGroupManager;
import org.eclipse.persistence.descriptors.changetracking.AttributeChangeTrackingPolicy;
import org.eclipse.persistence.descriptors.changetracking.DeferredChangeDetectionPolicy;
import org.eclipse.persistence.descriptors.changetracking.ObjectChangeTrackingPolicy;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.eclipse.persistence.exceptions.DescriptorException;
import org.eclipse.persistence.exceptions.QueryException;
import org.eclipse.persistence.expressions.Expression;
import org.eclipse.persistence.internal.descriptors.DescriptorIterator;
import org.eclipse.persistence.internal.descriptors.ObjectBuilder;
import org.eclipse.persistence.internal.expressions.SQLSelectStatement;
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.internal.helper.DatabaseTable;
import org.eclipse.persistence.internal.identitymaps.CacheKey;
import org.eclipse.persistence.internal.queries.ContainerPolicy;
import org.eclipse.persistence.internal.queries.EntityFetchGroup;
import org.eclipse.persistence.internal.queries.JoinedAttributeManager;
import org.eclipse.persistence.internal.queries.MappedKeyMapContainerPolicy;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.internal.sessions.AggregateChangeRecord;
import org.eclipse.persistence.internal.sessions.ChangeRecord;
import org.eclipse.persistence.internal.sessions.MergeManager;
import org.eclipse.persistence.internal.sessions.ObjectChangeSet;
import org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet;
import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl;
import org.eclipse.persistence.logging.SessionLog;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.mappings.foundation.AbstractTransformationMapping;
import org.eclipse.persistence.mappings.foundation.MapKeyMapping;
import org.eclipse.persistence.mappings.querykeys.DirectQueryKey;
import org.eclipse.persistence.mappings.querykeys.QueryKey;
import org.eclipse.persistence.queries.DeleteObjectQuery;
import org.eclipse.persistence.queries.FetchGroup;
import org.eclipse.persistence.queries.FetchGroupTracker;
import org.eclipse.persistence.queries.ObjectBuildingQuery;
import org.eclipse.persistence.queries.ObjectLevelReadQuery;
import org.eclipse.persistence.queries.ReadAllQuery;
import org.eclipse.persistence.queries.ReadObjectQuery;
import org.eclipse.persistence.queries.ReadQuery;
import org.eclipse.persistence.queries.WriteObjectQuery;
import org.eclipse.persistence.sessions.DatabaseRecord;
import org.eclipse.persistence.sessions.Project;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* <p><b>Purpose</b>:Two objects can be considered to be related by aggregation if there is a strict
* 1:1 relationship between the objects. This means that the target (child or owned) object
* cannot exist without the source (parent) object.
* <p>
* In TopLink, it also means the data for the owned object is stored in the same table as
* the parent.
*
* @author Sati
* @since TOPLink/Java 1.0
*/
public class AggregateObjectMapping extends AggregateMapping implements RelationalMapping, MapKeyMapping, EmbeddableMapping {
/**
* If <em>all</em> the fields in the database row for the aggregate object are NULL,
* then, by default, the mapping will place a null in the appropriate source object
* (as opposed to an aggregate object filled with nulls).
* To change this behavior, set the value of this variable to false. Then the mapping
* will build a new instance of the aggregate object that is filled with nulls
* and place it in the source object.
*/
protected boolean isNullAllowed;
protected DatabaseTable aggregateKeyTable = null;
/** Map the name of a field in the aggregate descriptor to a field in the source table. */
/* 322233 - changed to store the source DatabaseField to hold Case and other colunm info*/
protected Map<String, DatabaseField> aggregateToSourceFields;
/**
* Map of nested attributes that need to apply an override name to their
* a nested aggregate mapping's database field. Aggregate to source fields
* map is the existing EclipseLink functionality and works well when all
* embeddable mappings have unique database fields. This map adds specific
* attribute to database field override.
* @see #addFieldTranslation
*/
protected Map<String, Object[]> nestedFieldTranslations;
/**
* List of many to many mapping overrides to apply at initialize time to
* their cloned aggregate mappings.
*/
protected List<ManyToManyMapping> overrideManyToManyMappings;
/**
* List of unidirectional one to many mapping overrides to apply at
* initialize time to their cloned aggregate mappings.
*/
protected List<UnidirectionalOneToManyMapping> overrideUnidirectionalOneToManyMappings;
/**
* List of converters to apply at initialize time to their cloned aggregate mappings.
*/
protected Map<String, Converter> converters;
/**
* List of maps id mappings that need to be set to read only at initialize
* time on their cloned aggregate mappings.
*/
protected List<DatabaseMapping> mapsIdMappings;
/**
* List of map id attributes that need thir mapping to be set to read only at initialize
* time on their cloned aggregate mappings.
*/
protected List<String> mapsIdMappingAttributes;
/**
* Default constructor.
*/
public AggregateObjectMapping() {
aggregateToSourceFields = new HashMap<>(5);
nestedFieldTranslations = new HashMap<>();
mapsIdMappings = new ArrayList<>();
mapsIdMappingAttributes = new ArrayList<>();
overrideManyToManyMappings = new ArrayList<>();
overrideUnidirectionalOneToManyMappings = new ArrayList<>();
converters = new HashMap<>();
isNullAllowed = true;
}
/**
* INTERNAL:
*/
@Override
public boolean isRelationalMapping() {
return true;
}
/**
* INTERNAL:
* Used when initializing queries for mappings that use a Map
* Called when the selection query is being initialized to add the fields for the map key to the query
*/
@Override
public void addAdditionalFieldsToQuery(ReadQuery selectionQuery, Expression baseExpression){
for (DatabaseField field : getReferenceDescriptor().getAllFields()) {
if (selectionQuery.isObjectLevelReadQuery()) {
((ObjectLevelReadQuery)selectionQuery).addAdditionalField(baseExpression.getField(field));
} else if (selectionQuery.isDataReadQuery()) {
((SQLSelectStatement) selectionQuery.getSQLStatement()).addField(baseExpression.getField(field));
}
}
}
/**
* Add a converter to be applied to a mapping of the aggregate descriptor.
*/
@Override
public void addConverter(Converter converter, String attributeName) {
converters.put(attributeName, converter);
}
/**
* INTERNAL:
* Used when initializing queries for mappings that use a Map
* Called when the insert query is being initialized to ensure the fields for the map key are in the insert query
*/
@Override
public void addFieldsForMapKey(AbstractRecord joinRow){
for (DatabaseMapping mapping : getReferenceDescriptor().getMappings()) {
if (!mapping.isReadOnly()) {
for (DatabaseField field : mapping.getFields()) {
if (field.isUpdatable()){
joinRow.put(field, null);
}
}
}
}
}
/**
* PUBLIC:
* Add a field name translation that maps from a field name in the
* source table to a field name in the aggregate descriptor.
*/
public void addFieldNameTranslation(String sourceFieldName, String aggregateFieldName) {
// 322233 - changed to store the sourceField instead of sourceFieldName
addFieldTranslation(new DatabaseField(sourceFieldName), aggregateFieldName);
}
/**
* PUBLIC:
* Add a field translation that maps from a field in the
* source table to a field name in the aggregate descriptor.
*/
@Override
public void addFieldTranslation(DatabaseField sourceField, String aggregateFieldName) {
//AggregateObjectMapping does not seem to support Aggregates on multiple tables
String unQualifiedAggregateFieldName = aggregateFieldName.substring(aggregateFieldName.lastIndexOf('.') + 1);// -1 is returned for no ".".
getAggregateToSourceFields().put(unQualifiedAggregateFieldName, sourceField);
}
/**
* INTERNAL:
* In JPA users may specify a maps id mapping on a shared embeddable
* descriptor. These mappings need to be set to read-only at initialize
* time, after the reference descriptor is cloned.
*/
public void addMapsIdMapping(DatabaseMapping mapping) {
mapsIdMappings.add(mapping);
addMapsIdMappingAttribute(mapping.getAttributeName());
}
/**
* INTERNAL:
* In JPA users may specify a maps id attribute mapping on a shared embeddable
* descriptor. Mappings for these attributes need to be set to read-only
* at initialize time, after the reference descriptor is cloned.
*/
public void addMapsIdMappingAttribute(String attribute) {
mapsIdMappingAttributes.add(attribute);
}
/**
* INTERNAL:
* Add a nested field translation that maps from a field in the source table
* to a field name in a nested aggregate descriptor. These are handled
* slightly different that regular field translations in that they are
* unique based on the attribute name. It solves the case where multiple
* nested embeddables have mappings to similarly named default columns.
*/
@Override
public void addNestedFieldTranslation(String attributeName, DatabaseField sourceField, String aggregateFieldName) {
// Aggregate field name is redundant here as we will look up the field
// through the attribute name. This method signature is to satisfy the
// Embeddable interface. AggregateCollectionMapping uses the aggregate
// field name.
nestedFieldTranslations.put(attributeName, new Object[]{sourceField, aggregateFieldName});
}
/**
* INTERNAL:
* In JPA users may specify overrides to apply to a many to many mapping
* on a shared embeddable descriptor. These settings are applied at
* initialize time, after the reference descriptor is cloned.
*/
@Override
public void addOverrideManyToManyMapping(ManyToManyMapping mapping) {
overrideManyToManyMappings.add(mapping);
}
/**
* INTERNAL:
* In JPA users may specify overrides to apply to a unidirectional one to
* many mapping on a shared embeddable descriptor. These settings are
* applied at initialize time, after the reference descriptor is cloned.
*/
@Override
public void addOverrideUnidirectionalOneToManyMapping(UnidirectionalOneToManyMapping mapping) {
overrideUnidirectionalOneToManyMappings.add(mapping);
}
/**
* INTERNAL:
* For mappings used as MapKeys in MappedKeyContainerPolicy. Add the target of this mapping to the deleted
* objects list if necessary
* <p>
* This method is used for removal of private owned relationships.
* AggregateObjectMappings are dealt with in their parent delete, so this is a no-op.
*
*/
@Override
public void addKeyToDeletedObjectsList(Object object, Map deletedObjects){
}
/**
* INTERNAL:
* Return whether all the aggregate fields in the specified
* row are NULL.
*/
protected boolean allAggregateFieldsAreNull(AbstractRecord databaseRow) {
List<DatabaseField> fields = getReferenceFields();
int size = fields.size();
for (int index = 0; index < size; index++) {
DatabaseField field = fields.get(index);
Object value = databaseRow.get(field);
if (value != null) {
return false;
}
}
return true;
}
/**
* PUBLIC:
* If <em>all</em> the fields in the database row for the aggregate object are NULL,
* then, by default, the mapping will place a null in the appropriate source object
* (as opposed to an aggregate object filled with nulls). This behavior can be
* explicitly set by calling #allowNull().
* To change this behavior, call #dontAllowNull(). Then the mapping
* will build a new instance of the aggregate object that is filled with nulls
* and place it in the source object.
* In either situation, when writing, the mapping will place a NULL in all the
* fields in the database row for the aggregate object.
* <p>
* Note: Any aggregate that has a relationship mapping automatically does not allow
* null.
*/
public void allowNull() {
setIsNullAllowed(true);
}
/**
* INTERNAL:
* Return whether the query's backup object has an attribute
* value of null.
*/
protected boolean backupAttributeValueIsNull(WriteObjectQuery query) {
if (query.getSession().isUnitOfWork()) {
Object backupAttributeValue = getAttributeValueFromObject(query.getBackupClone());
if (backupAttributeValue == null) {
return true;
}
}
return false;
}
/**
* INTERNAL:
* Clone and prepare the selection query as a nested batch read query.
* This is used for nested batch reading.
*/
public ObjectBuildingQuery prepareNestedQuery(ObjectBuildingQuery sourceQuery) {
if (sourceQuery.isObjectLevelReadQuery()) {
ObjectLevelReadQuery objectQuery = (ObjectLevelReadQuery)sourceQuery;
ObjectLevelReadQuery nestedObjectQuery = objectQuery.getAggregateQuery(this);
if (nestedObjectQuery != null) {
return nestedObjectQuery;
}
nestedObjectQuery = objectQuery;
String attributeName = getAttributeName();
if ((objectQuery.isPartialAttribute(attributeName))) {
// A nested query must be built to pass to the descriptor that looks like the real query execution would.
nestedObjectQuery = (ObjectLevelReadQuery)objectQuery.clone();
// Must cascade the nested partial/join expression and filter the nested ones.
if (objectQuery.hasPartialAttributeExpressions()) {
nestedObjectQuery.setPartialAttributeExpressions(extractNestedExpressions(objectQuery.getPartialAttributeExpressions(), nestedObjectQuery.getExpressionBuilder()));
}
}
if (objectQuery.isAttributeBatchRead(this.descriptor, attributeName)) {
if (nestedObjectQuery == objectQuery) {
// A nested query must be built to pass to the descriptor that looks like the real query execution would.
nestedObjectQuery = (ObjectLevelReadQuery)nestedObjectQuery.clone();
}
// Must carry over properties for batching to work.
nestedObjectQuery.setProperties(objectQuery.getProperties());
// Computed nested batch attribute expressions.
nestedObjectQuery.getBatchFetchPolicy().setAttributeExpressions(extractNestedExpressions(objectQuery.getBatchReadAttributeExpressions(), nestedObjectQuery.getExpressionBuilder()));
nestedObjectQuery.computeBatchReadAttributes();
}
FetchGroup parentQueryFetchGroup = sourceQuery.getExecutionFetchGroup(this.descriptor);
if (parentQueryFetchGroup != null) {
if (nestedObjectQuery == objectQuery) {
// A nested query must be built to pass to the descriptor that looks like the real query execution would.
nestedObjectQuery = (ObjectLevelReadQuery)nestedObjectQuery.clone();
}
FetchGroup targetFetchGroup = parentQueryFetchGroup.getGroup(getAttributeName());
if (targetFetchGroup != null && sourceQuery.getDescriptor().hasFetchGroupManager()) {
//if the parent object has a fetchgroup manager then aggregates can support a fetchgroup manager
nestedObjectQuery.setFetchGroup(targetFetchGroup);
} else {
targetFetchGroup = null;
nestedObjectQuery.setFetchGroup(null);
nestedObjectQuery.setFetchGroupName(null);
}
}
if (nestedObjectQuery != sourceQuery) {
objectQuery.setAggregateQuery(this, nestedObjectQuery);
return nestedObjectQuery;
}
}
return sourceQuery;
}
/**
* INTERNAL:
* Build and return an aggregate object from the specified row.
* If a null value is allowed and all the appropriate fields in the row are NULL, return a null.
* If an aggregate is referenced by the target object, return it (maintain identity)
* Otherwise, simply create a new aggregate object and return it.
*/
public Object buildAggregateFromRow(AbstractRecord databaseRow, Object targetObject, CacheKey cacheKey, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, boolean buildShallowOriginal, AbstractSession executionSession, boolean targetIsProtected) throws DatabaseException {
if (databaseRow.hasSopObject()) {
Object sopAggregate = getAttributeValueFromObject(databaseRow.getSopObject());
if ((targetObject != null) && (targetObject != databaseRow.getSopObject())) {
setAttributeValueInObject(targetObject, sopAggregate);
}
return sopAggregate;
}
// check for all NULLs
if (isNullAllowed() && allAggregateFieldsAreNull(databaseRow)) {
return null;
}
// maintain object identity (even if not refreshing) if target object references the aggregate
// if aggregate is not referenced by the target object, construct a new aggregate
Object aggregate = null;
ClassDescriptor descriptor = getReferenceDescriptor();
boolean refreshing = true;
if (targetObject != null){
if (descriptor.hasInheritance()) {
Class<?> newAggregateClass = descriptor.getInheritancePolicy().classFromRow(databaseRow, executionSession);
descriptor = getReferenceDescriptor(newAggregateClass, executionSession);
aggregate = getMatchingAttributeValueFromObject(databaseRow, targetObject, executionSession, descriptor);
if ((aggregate != null) && (aggregate.getClass() != newAggregateClass)) {
// if the class has changed out from underneath us, we cannot preserve object identity
// build a new instance of the *new* class
aggregate = descriptor.getObjectBuilder().buildNewInstance();
refreshing = false;
}
} else {
aggregate = getMatchingAttributeValueFromObject(databaseRow, targetObject, executionSession, descriptor);
}
}
// Build a new aggregate if the target object does not reference an existing aggregate.
// EL Bug 474956 - build a new aggregate if the the target object references an existing aggregate, and
// the passed cacheKey is null from the invalidation of the target object in the IdentityMap.
if (aggregate == null || (aggregate != null && cacheKey == null)) {
aggregate = descriptor.getObjectBuilder().buildNewInstance();
refreshing = false;
}
ObjectBuildingQuery nestedQuery = prepareNestedQuery(sourceQuery);
FetchGroup targetFetchGroup = null;
if (nestedQuery.isObjectLevelReadQuery()) {
targetFetchGroup = ((ObjectLevelReadQuery)nestedQuery).getFetchGroup();
if (refreshing && descriptor.hasFetchGroupManager()) {
descriptor.getFetchGroupManager().unionEntityFetchGroupIntoObject(aggregate, descriptor.getFetchGroupManager().getEntityFetchGroup(targetFetchGroup), executionSession, true);
//merge fetchgroup into aggregate fetchgroup that may have been there from previous read.
}
}
if (buildShallowOriginal) {
descriptor.getObjectBuilder().buildAttributesIntoShallowObject(aggregate, databaseRow, nestedQuery);
} else if (executionSession.isUnitOfWork()) {
descriptor.getObjectBuilder().buildAttributesIntoWorkingCopyClone(aggregate, buildWrapperCacheKeyForAggregate(cacheKey, targetIsProtected), nestedQuery, joinManager, databaseRow, (UnitOfWorkImpl)executionSession, refreshing);
} else {
descriptor.getObjectBuilder().buildAttributesIntoObject(aggregate, buildWrapperCacheKeyForAggregate(cacheKey, targetIsProtected), databaseRow, nestedQuery, joinManager, nestedQuery.getExecutionFetchGroup(descriptor), refreshing, executionSession);
}
if ((targetFetchGroup != null) && descriptor.hasFetchGroupManager() && cacheKey != null
&& !refreshing && sourceQuery.shouldMaintainCache() && !sourceQuery.shouldStoreBypassCache()) {
// Set the fetch group to the domain object, after built.
EntityFetchGroup entityFetchGroup = descriptor.getFetchGroupManager().getEntityFetchGroup(targetFetchGroup);
if (entityFetchGroup != null) {
entityFetchGroup = (EntityFetchGroup)entityFetchGroup.clone();
entityFetchGroup.setRootEntity((FetchGroupTracker) cacheKey.getObject());
entityFetchGroup.setOnEntity(aggregate, executionSession);
}
}
return aggregate;
}
/**
* INTERNAL:
* Wrap the aggregate represented by this mapping in a cachekey so it can be processed my
* methods down the stack.
* @param owningCacheKey - the cache key holding the object to extract the aggregate from
*/
protected CacheKey buildWrapperCacheKeyForAggregate(CacheKey owningCacheKey, boolean targetIsProtected) {
if (!this.descriptor.getCachePolicy().isProtectedIsolation()) {
return owningCacheKey;
}
if (!targetIsProtected || this.isMapKeyMapping || (owningCacheKey == null)) {
return owningCacheKey;
}
CacheKey aggregateKey = owningCacheKey;
Object object = owningCacheKey.getObject();
if (owningCacheKey.getObject() != null) {
Object aggregate = getAttributeValueFromObject(object);
aggregateKey = new CacheKey(null, aggregate, null);
aggregateKey.setProtectedForeignKeys(owningCacheKey.getProtectedForeignKeys());
aggregateKey.setRecord(owningCacheKey.getRecord());
aggregateKey.setIsolated(owningCacheKey.isIsolated());
aggregateKey.setReadTime(owningCacheKey.getReadTime());
}
return aggregateKey;
}
/**
* INTERNAL:
* Write null values for all aggregate fields into the parent row.
*/
protected void writeNullReferenceRow(AbstractRecord record) {
List<DatabaseField> fields = getReferenceFields();
int size = fields.size();
boolean nullInserted = false;
for (int index = 0; index < size; index++) {
DatabaseField field = fields.get(index);
// EL Bug 393520
if (!field.isReadOnly() && (field.isUpdatable() || field.isInsertable())) {
record.put(field, null);
nullInserted = true;
}
}
if (size > 0 && nullInserted) {
// EL Bug 319759 - if a field is null, then the update call cache should not be used
record.setNullValueInFields(true);
}
}
/**
* INTERNAL:
* Used to allow object level comparisons.
* In the case of an Aggregate which has no primary key must do an attribute
* by attribute comparison.
*/
@Override
public Expression buildObjectJoinExpression(Expression expression, Object value, AbstractSession session) {
Expression attributeByAttributeComparison = null;
Expression join = null;
Object attributeValue = null;
// value need not be unwrapped as it is an aggregate, nor should it
// influence a call to getReferenceDescriptor.
ClassDescriptor referenceDescriptor = getReferenceDescriptor();
if ((value != null) && !referenceDescriptor.getJavaClass().isInstance(value)) {
throw QueryException.incorrectClassForObjectComparison(expression, value, this);
}
for (DatabaseMapping mapping: referenceDescriptor.getMappings()) {
if (value == null) {
attributeValue = null;
} else {
attributeValue = mapping.getAttributeValueFromObject(value);
}
join = expression.get(mapping.getAttributeName()).equal(attributeValue);
if (attributeByAttributeComparison == null) {
attributeByAttributeComparison = join;
} else {
attributeByAttributeComparison = attributeByAttributeComparison.and(join);
}
}
return attributeByAttributeComparison;
}
/**
* INTERNAL:
* Used to allow object level comparisons.
*/
@Override
public Expression buildObjectJoinExpression(Expression expression, Expression argument, AbstractSession session) {
Expression attributeByAttributeComparison = null;
//Enumeration mappingsEnum = getSourceToTargetKeyFields().elements();
for (DatabaseMapping mapping: getReferenceDescriptor().getMappings()) {
String attributeName = mapping.getAttributeName();
Expression join = expression.get(attributeName).equal(argument.get(attributeName));
if (attributeByAttributeComparison == null) {
attributeByAttributeComparison = join;
} else {
attributeByAttributeComparison = attributeByAttributeComparison.and(join);
}
}
return attributeByAttributeComparison;
}
/**
* INTERNAL:
* Write the aggregate values into the parent row.
*/
protected void writeToRowFromAggregate(AbstractRecord abstractRecord, Object object, Object attributeValue, AbstractSession session, WriteType writeType) throws DescriptorException {
if (attributeValue == null) {
if (this.isNullAllowed) {
writeNullReferenceRow(abstractRecord);
} else {
throw DescriptorException.nullForNonNullAggregate(object, this);
}
} else {
if (!session.isClassReadOnly(attributeValue.getClass())) {
getObjectBuilder(attributeValue, session).buildRow(abstractRecord, attributeValue, session, writeType);
}
}
}
/**
* INTERNAL:
* Write the aggregate values into the parent row for shallow insert.
*/
protected void writeToRowFromAggregateForShallowInsert(AbstractRecord abstractRecord, Object object, Object attributeValue, AbstractSession session) throws DescriptorException {
if (attributeValue == null) {
if (this.isNullAllowed) {
writeNullReferenceRow(abstractRecord);
} else {
throw DescriptorException.nullForNonNullAggregate(object, this);
}
} else {
if (!session.isClassReadOnly(attributeValue.getClass())) {
getObjectBuilder(attributeValue, session).buildRowForShallowInsert(abstractRecord, attributeValue, session);
}
}
}
/**
* INTERNAL:
* Write the aggregate values into the parent row for update after shallow insert.
*/
protected void writeToRowFromAggregateForUpdateAfterShallowInsert(AbstractRecord abstractRecord, Object object, Object attributeValue, AbstractSession session, DatabaseTable table) throws DescriptorException {
if (attributeValue == null) {
if (!this.isNullAllowed) {
throw DescriptorException.nullForNonNullAggregate(object, this);
}
} else {
if (!session.isClassReadOnly(attributeValue.getClass()) && !isPrimaryKeyMapping()) {
getObjectBuilder(attributeValue, session).buildRowForUpdateAfterShallowInsert(abstractRecord, attributeValue, session, table);
}
}
}
/**
* INTERNAL:
* Write the aggregate values into the parent row for update before shallow delete.
*/
protected void writeToRowFromAggregateForUpdateBeforeShallowDelete(AbstractRecord abstractRecord, Object object, Object attributeValue, AbstractSession session, DatabaseTable table) throws DescriptorException {
if (attributeValue == null) {
if (!this.isNullAllowed) {
throw DescriptorException.nullForNonNullAggregate(object, this);
}
} else {
if (!session.isClassReadOnly(attributeValue.getClass()) && !isPrimaryKeyMapping()) {
getObjectBuilder(attributeValue, session).buildRowForUpdateBeforeShallowDelete(abstractRecord, attributeValue, session, table);
}
}
}
/**
* INTERNAL:
* Build and return a database row built with the values from
* the specified attribute value.
*/
protected void writeToRowFromAggregateWithChangeRecord(AbstractRecord abstractRecord, ChangeRecord changeRecord, ObjectChangeSet objectChangeSet, AbstractSession session, WriteType writeType) throws DescriptorException {
if (objectChangeSet == null) {
if (this.isNullAllowed) {
writeNullReferenceRow(abstractRecord);
} else {
Object object = ((ObjectChangeSet)changeRecord.getOwner()).getUnitOfWorkClone();
throw DescriptorException.nullForNonNullAggregate(object, this);
}
} else {
if (!session.isClassReadOnly(objectChangeSet.getClassType(session))) {
getReferenceDescriptor(objectChangeSet.getClassType(session), session).getObjectBuilder().buildRowWithChangeSet(abstractRecord, objectChangeSet, session, writeType);
}
}
}
/**
* INTERNAL:
* Build and return a database row built with the changed values from
* the specified attribute value.
*/
protected void writeToRowFromAggregateForUpdate(AbstractRecord abstractRecord, WriteObjectQuery query, Object attributeValue) throws DescriptorException {
if (attributeValue == null) {
if (this.isNullAllowed) {
if (backupAttributeValueIsNull(query)) {
// both attributes are null - no update required
} else {
writeNullReferenceRow(abstractRecord);
}
} else {
throw DescriptorException.nullForNonNullAggregate(query.getObject(), this);
}
} else if ((query.getBackupClone() != null) && ((getMatchingBackupAttributeValue(query, attributeValue) == null) || !(attributeValue.getClass().equals(getMatchingBackupAttributeValue(query, attributeValue).getClass())))) {
getObjectBuilder(attributeValue, query.getSession()).buildRow(abstractRecord, attributeValue, query.getSession(), WriteType.UPDATE);
} else {
if (!query.getSession().isClassReadOnly(attributeValue.getClass())) {
WriteObjectQuery clonedQuery = (WriteObjectQuery)query.clone();
clonedQuery.setObject(attributeValue);
if (query.getSession().isUnitOfWork()) {
Object backupAttributeValue = getMatchingBackupAttributeValue(query, attributeValue);
if (backupAttributeValue == null) {
backupAttributeValue = getObjectBuilder(attributeValue, query.getSession()).buildNewInstance();
}
clonedQuery.setBackupClone(backupAttributeValue);
}
getObjectBuilder(attributeValue, query.getSession()).buildRowForUpdate(abstractRecord, clonedQuery);
}
}
}
/**
* INTERNAL:
* Clone the attribute from the original and assign it to the clone.
*/
@Override
public void buildClone(Object original, CacheKey cacheKey, Object clone, Integer refreshCascade, AbstractSession cloningSession) {
Object attributeValue = getAttributeValueFromObject(original);
Object aggregateClone = buildClonePart(original, clone, cacheKey, attributeValue, refreshCascade, cloningSession);
if (aggregateClone != null && cloningSession.isUnitOfWork()) {
ClassDescriptor descriptor = getReferenceDescriptor(aggregateClone, cloningSession);
descriptor.getObjectChangePolicy().setAggregateChangeListener(clone, aggregateClone, (UnitOfWorkImpl)cloningSession, descriptor, getAttributeName());
}
setAttributeValueInObject(clone, aggregateClone);
}
/**
* INTERNAL:
* Build a clone of the given element in a unitOfWork
*/
@Override
public Object buildElementClone(Object attributeValue, Object parent, CacheKey parentCacheKey, Integer refreshCascade, AbstractSession cloningSession, boolean isExisting, boolean isFromSharedCache){
Object aggregateClone = buildClonePart(attributeValue, parent, parentCacheKey, refreshCascade, cloningSession, !isExisting);
if (aggregateClone != null && cloningSession.isUnitOfWork()) {
ClassDescriptor descriptor = getReferenceDescriptor(aggregateClone, cloningSession);
descriptor.getObjectChangePolicy().setAggregateChangeListener(parent, aggregateClone, (UnitOfWorkImpl)cloningSession, descriptor, getAttributeName());
}
return aggregateClone;
}
/**
* INTERNAL:
* Set the change listener in the aggregate.
*/
@Override
public void setChangeListener(Object clone, PropertyChangeListener listener, UnitOfWorkImpl uow) {
Object attributeValue = getAttributeValueFromObject(clone);
if (attributeValue != null) {
ClassDescriptor descriptor = getReferenceDescriptor(attributeValue, uow);
descriptor.getObjectChangePolicy().setAggregateChangeListener(clone, attributeValue, uow, descriptor, getAttributeName());
}
}
/**
* INTERNAL:
* A combination of readFromRowIntoObject and buildClone.
* <p>
* buildClone assumes the attribute value exists on the original and can
* simply be copied.
* <p>
* readFromRowIntoObject assumes that one is building an original.
* <p>
* Both of the above assumptions are false in this method, and actually
* attempts to do both at the same time.
* <p>
* Extract value from the row and set the attribute to this value in the
* working copy clone.
* In order to bypass the shared cache when in transaction a UnitOfWork must
* be able to populate working copies directly from the row.
*/
@Override
public void buildCloneFromRow(AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object clone, CacheKey sharedCacheKey, ObjectBuildingQuery sourceQuery, UnitOfWorkImpl unitOfWork, AbstractSession executionSession) {
// This method is a combination of buildggregateFromRow and buildClonePart on the super class.
// None of buildClonePart used, as not an orignal new object, nor do we worry about creating heavy clones for aggregate objects.
// Ensure that the shared CacheKey is passed, as this will be set to null for a refresh of an invalid object.
Object clonedAttributeValue = buildAggregateFromRow(databaseRow, clone, sharedCacheKey, joinManager, sourceQuery, false, executionSession, true);
if (clonedAttributeValue != null) {
ClassDescriptor descriptor = getReferenceDescriptor(clonedAttributeValue, unitOfWork);
descriptor.getObjectChangePolicy().setAggregateChangeListener(clone, clonedAttributeValue, unitOfWork, descriptor, getAttributeName());
}
setAttributeValueInObject(clone, clonedAttributeValue);
}
/**
* INTERNAL:
* Builds a shallow original object. Only direct attributes and primary
* keys are populated. In this way the minimum original required for
* instantiating a working copy clone can be built without placing it in
* the shared cache (no concern over cycles).
*/
@Override
public void buildShallowOriginalFromRow(AbstractRecord databaseRow, Object original, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, AbstractSession executionSession) {
Object aggregate = buildAggregateFromRow(databaseRow, original, null, joinManager, sourceQuery, true, executionSession, true);// shallow only.
setAttributeValueInObject(original, aggregate);
}
/**
* INTERNAL:
* Certain key mappings favor different types of selection query. Return the appropriate
* type of selectionQuery
*/
@Override
public ReadQuery buildSelectionQueryForDirectCollectionKeyMapping(ContainerPolicy containerPolicy){
ReadAllQuery query = new ReadAllQuery();
query.setReferenceClass(referenceClass);
query.setDescriptor(getReferenceDescriptor());
query.setContainerPolicy(containerPolicy);
return query;
}
/**
* INTERNAL:
* Build and return a "template" database row with all the fields
* set to null.
*/
protected AbstractRecord buildTemplateInsertRow(AbstractSession session) {
AbstractRecord result = getReferenceDescriptor().getObjectBuilder().buildTemplateInsertRow(session);
@SuppressWarnings({"unchecked"})
List<DatabaseMapping> processedMappings = (List<DatabaseMapping>) ((ArrayList<DatabaseMapping>) getReferenceDescriptor().getMappings()).clone();
if (getReferenceDescriptor().hasInheritance()) {
for (ClassDescriptor child : getReferenceDescriptor().getInheritancePolicy().getChildDescriptors()) {
for (DatabaseMapping mapping : child.getMappings()) {
// Only write mappings once.
if (!processedMappings.contains(mapping)) {
mapping.writeInsertFieldsIntoRow(result, session);
processedMappings.add(mapping);
}
}
}
}
return result;
}
/**
* INTERNAL:
* Cascade discover and persist new objects during commit to the map key
*/
@Override
public void cascadeDiscoverAndPersistUnregisteredNewObjects(Object object, Map newObjects, Map unregisteredExistingObjects, Map visitedObjects, UnitOfWorkImpl uow, boolean getAttributeValueFromObject, Set cascadeErrors){
ObjectBuilder builder = getReferenceDescriptor(object.getClass(), uow).getObjectBuilder();
builder.cascadeDiscoverAndPersistUnregisteredNewObjects(object, newObjects, unregisteredExistingObjects, visitedObjects, uow, cascadeErrors);
}
/**
* INTERNAL:
* Cascade perform delete through mappings that require the cascade
*/
@Override
public void cascadePerformRemoveIfRequired(Object object, UnitOfWorkImpl uow, Map visitedObjects, boolean getAttributeValueFromObject) {
Object objectReferenced = null;
if (getAttributeValueFromObject){
//objects referenced by this mapping are not registered as they have
// no identity, however mappings from the referenced object may need cascading.
objectReferenced = getAttributeValueFromObject(object);
} else {
objectReferenced = object;
}
if ((objectReferenced == null)) {
return;
}
if (!visitedObjects.containsKey(objectReferenced)) {
visitedObjects.put(objectReferenced, objectReferenced);
ObjectBuilder builder = getReferenceDescriptor(objectReferenced.getClass(), uow).getObjectBuilder();
builder.cascadePerformRemove(objectReferenced, uow, visitedObjects);
}
}
/**
* INTERNAL:
* Cascade perform delete through mappings that require the cascade
*/
@Override
public void cascadePerformRemoveIfRequired(Object object, UnitOfWorkImpl uow, Map visitedObjects) {
cascadePerformRemoveIfRequired(object, uow, visitedObjects, true);
}
/**
* INTERNAL:
* Cascade perform removal of orphaned private owned objects from the UnitOfWorkChangeSet
*/
@Override
public void cascadePerformRemovePrivateOwnedObjectFromChangeSetIfRequired(Object object, UnitOfWorkImpl uow, Map visitedObjects) {
Object attributeValue = getAttributeValueFromObject(object);
if (attributeValue == null) {
return;
}
if (!visitedObjects.containsKey(attributeValue)) {
visitedObjects.put(attributeValue, attributeValue);
ObjectBuilder builder = getReferenceDescriptor(attributeValue, uow).getObjectBuilder();
// cascade perform remove any related objects via ObjectBuilder for an aggregate object
builder.cascadePerformRemovePrivateOwnedObjectFromChangeSet(attributeValue, uow, visitedObjects);
}
}
/**
* INTERNAL:
* Cascade registerNew for Create through mappings that require the cascade
*/
@Override
public void cascadeRegisterNewIfRequired(Object object, UnitOfWorkImpl uow, Map visitedObjects, boolean getAttributeValueFromObject) {
Object objectReferenced = null;
//aggregate objects are not registered but their mappings should be.
if (getAttributeValueFromObject){
objectReferenced = getAttributeValueFromObject(object);
} else {
objectReferenced = object;
}
if ((objectReferenced == null)) {
return;
}
if (!visitedObjects.containsKey(objectReferenced)) {
visitedObjects.put(objectReferenced, objectReferenced);
ObjectBuilder builder = getReferenceDescriptor(objectReferenced.getClass(), uow).getObjectBuilder();
builder.cascadeRegisterNewForCreate(objectReferenced, uow, visitedObjects);
}
}
/**
* INTERNAL:
* Cascade registerNew for Create through mappings that require the cascade
*/
@Override
public void cascadeRegisterNewIfRequired(Object object, UnitOfWorkImpl uow, Map visitedObjects) {
cascadeRegisterNewIfRequired(object, uow, visitedObjects, true);
}
/**
* INTERNAL:
* Clone the aggregate to source field names. AggregateCollectionMapping
* needs each nested embedded mapping to have its own list of aggregate
* to source field names so that it can apply nested override names to
* shared aggregate object mappings.
*/
@Override
public Object clone() {
AggregateObjectMapping mappingObject = (AggregateObjectMapping) super.clone();
Map<String, DatabaseField> aggregateToSourceFields = new HashMap<>(getAggregateToSourceFields());
mappingObject.setAggregateToSourceFields(aggregateToSourceFields);
Map<String, Object[]> nestedTranslations = new HashMap<>(getNestedFieldTranslations());
mappingObject.setNestedFieldTranslations(nestedTranslations);
return mappingObject;
}
/**
* INTERNAL:
* Return the fields handled by the mapping.
*/
@Override
protected List<DatabaseField> collectFields() {
return new ArrayList<>(getReferenceFields());
}
/**
* INTERNAL:
* Aggregate order by all their fields by default.
*/
@Override
public List<Expression> getOrderByNormalizedExpressions(Expression base) {
List<Expression> orderBys = new ArrayList<>(this.fields.size());
for (DatabaseField field : this.fields) {
orderBys.add(base.getField(field));
}
return orderBys;
}
/**
* INTERNAL:
* This method is used to store the FK fields that can be cached that correspond to noncacheable mappings
* the FK field values will be used to re-issue the query when cloning the shared cache entity
*/
@Override
public void collectQueryParameters(Set<DatabaseField> record){
for (DatabaseMapping mapping : getReferenceDescriptor().getMappings()){
if ((mapping.isForeignReferenceMapping() && !mapping.isCacheable()) || (mapping.isAggregateObjectMapping() && mapping.getReferenceDescriptor().hasNoncacheableMappings())){
mapping.collectQueryParameters(record);