/
CollectionMetadataGenerator.java
1011 lines (911 loc) · 37.7 KB
/
CollectionMetadataGenerator.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
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.envers.configuration.internal.metadata;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.persistence.JoinColumn;
import org.dom4j.Element;
import org.hibernate.MappingException;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.envers.ModificationStore;
import org.hibernate.envers.RelationTargetAuditMode;
import org.hibernate.envers.configuration.internal.metadata.reader.AuditedPropertiesReader;
import org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditedPropertiesReader;
import org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditingData;
import org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData;
import org.hibernate.envers.internal.EnversMessageLogger;
import org.hibernate.envers.internal.entities.EntityConfiguration;
import org.hibernate.envers.internal.entities.IdMappingData;
import org.hibernate.envers.internal.entities.PropertyData;
import org.hibernate.envers.internal.entities.mapper.CompositeMapperBuilder;
import org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper;
import org.hibernate.envers.internal.entities.mapper.PropertyMapper;
import org.hibernate.envers.internal.entities.mapper.SinglePropertyMapper;
import org.hibernate.envers.internal.entities.mapper.id.IdMapper;
import org.hibernate.envers.internal.entities.mapper.relation.BasicCollectionMapper;
import org.hibernate.envers.internal.entities.mapper.relation.CommonCollectionMapperData;
import org.hibernate.envers.internal.entities.mapper.relation.ListCollectionMapper;
import org.hibernate.envers.internal.entities.mapper.relation.MapCollectionMapper;
import org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData;
import org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData;
import org.hibernate.envers.internal.entities.mapper.relation.SortedMapCollectionMapper;
import org.hibernate.envers.internal.entities.mapper.relation.SortedSetCollectionMapper;
import org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper;
import org.hibernate.envers.internal.entities.mapper.relation.component.MiddleDummyComponentMapper;
import org.hibernate.envers.internal.entities.mapper.relation.component.MiddleEmbeddableComponentMapper;
import org.hibernate.envers.internal.entities.mapper.relation.component.MiddleMapKeyIdComponentMapper;
import org.hibernate.envers.internal.entities.mapper.relation.component.MiddleMapKeyPropertyComponentMapper;
import org.hibernate.envers.internal.entities.mapper.relation.component.MiddleRelatedComponentMapper;
import org.hibernate.envers.internal.entities.mapper.relation.component.MiddleSimpleComponentMapper;
import org.hibernate.envers.internal.entities.mapper.relation.component.MiddleStraightComponentMapper;
import org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.ListProxy;
import org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.MapProxy;
import org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.SetProxy;
import org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.SortedMapProxy;
import org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.SortedSetProxy;
import org.hibernate.envers.internal.entities.mapper.relation.query.OneAuditEntityQueryGenerator;
import org.hibernate.envers.internal.entities.mapper.relation.query.RelationQueryGenerator;
import org.hibernate.envers.internal.tools.MappingTools;
import org.hibernate.envers.internal.tools.ReflectionTools;
import org.hibernate.envers.internal.tools.StringTools;
import org.hibernate.envers.internal.tools.Tools;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.IndexedCollection;
import org.hibernate.mapping.ManyToOne;
import org.hibernate.mapping.OneToMany;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Selectable;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.Value;
import org.hibernate.type.BagType;
import org.hibernate.type.ComponentType;
import org.hibernate.type.ListType;
import org.hibernate.type.ManyToOneType;
import org.hibernate.type.MapType;
import org.hibernate.type.SetType;
import org.hibernate.type.SortedMapType;
import org.hibernate.type.SortedSetType;
import org.hibernate.type.Type;
import org.jboss.logging.Logger;
/**
* Generates metadata for a collection-valued property.
*
* @author Adam Warski (adam at warski dot org)
* @author HernпїЅn Chanfreau
* @author Chris Cranford
*/
public final class CollectionMetadataGenerator {
private static final EnversMessageLogger LOG = Logger.getMessageLogger(
EnversMessageLogger.class,
CollectionMetadataGenerator.class.getName()
);
private final AuditMetadataGenerator mainGenerator;
private final String propertyName;
private final Collection propertyValue;
private final CompositeMapperBuilder currentMapper;
private final String referencingEntityName;
private final EntityXmlMappingData xmlMappingData;
private final PropertyAuditingData propertyAuditingData;
private final EntityConfiguration referencingEntityConfiguration;
/**
* Null if this collection isn't a relation to another entity.
*/
private final String referencedEntityName;
/**
* @param mainGenerator Main generator, giving access to configuration and the basic mapper.
* @param propertyValue Value of the collection, as mapped by Hibernate.
* @param currentMapper Mapper, to which the appropriate {@link PropertyMapper} will be added.
* @param referencingEntityName Name of the entity that owns this collection.
* @param xmlMappingData In case this collection requires a middle table, additional mapping documents will
* be created using this object.
* @param propertyAuditingData Property auditing (meta-)data. Among other things, holds the name of the
* property that references the collection in the referencing entity, the user data for middle (join)
* table and the value of the <code>@MapKey</code> annotation, if there was one.
*/
public CollectionMetadataGenerator(
AuditMetadataGenerator mainGenerator,
Collection propertyValue,
CompositeMapperBuilder currentMapper,
String referencingEntityName,
EntityXmlMappingData xmlMappingData,
PropertyAuditingData propertyAuditingData) {
this.mainGenerator = mainGenerator;
this.propertyValue = propertyValue;
this.currentMapper = currentMapper;
this.referencingEntityName = referencingEntityName;
this.xmlMappingData = xmlMappingData;
this.propertyAuditingData = propertyAuditingData;
this.propertyName = propertyAuditingData.getName();
referencingEntityConfiguration = mainGenerator.getEntitiesConfigurations().get( referencingEntityName );
if ( referencingEntityConfiguration == null ) {
throw new MappingException( "Unable to read auditing configuration for " + referencingEntityName + "!" );
}
referencedEntityName = MappingTools.getReferencedEntityName( propertyValue.getElement() );
}
void addCollection() {
final Type type = propertyValue.getType();
final Value value = propertyValue.getElement();
final boolean oneToManyAttachedType = type instanceof BagType || type instanceof SetType || type instanceof MapType || type instanceof ListType;
final boolean inverseOneToMany = (value instanceof OneToMany) && (propertyValue.isInverse());
final boolean owningManyToOneWithJoinTableBidirectional = (value instanceof ManyToOne) && (propertyAuditingData.getRelationMappedBy() != null);
final boolean fakeOneToManyBidirectional = (value instanceof OneToMany) && (propertyAuditingData.getAuditMappedBy() != null);
if ( oneToManyAttachedType && (inverseOneToMany || fakeOneToManyBidirectional || owningManyToOneWithJoinTableBidirectional) ) {
// A one-to-many relation mapped using @ManyToOne and @OneToMany(mappedBy="...")
addOneToManyAttached( fakeOneToManyBidirectional );
}
else {
// All other kinds of relations require a middle (join) table.
addWithMiddleTable();
}
}
private MiddleIdData createMiddleIdData(IdMappingData idMappingData, String prefix, String entityName) {
return new MiddleIdData(
mainGenerator.getVerEntCfg(),
idMappingData,
prefix,
entityName,
mainGenerator.getEntitiesConfigurations().containsKey( entityName )
);
}
@SuppressWarnings({"unchecked"})
private void addOneToManyAttached(boolean fakeOneToManyBidirectional) {
LOG.debugf(
"Adding audit mapping for property %s.%s: one-to-many collection, using a join column on the referenced entity",
referencingEntityName,
propertyName
);
// check whether the property has an @IndexColumn or @OrderColumn because its part of an
// IndexedCollection mapping type.
final boolean indexed = ( propertyValue instanceof IndexedCollection ) && ( (IndexedCollection) propertyValue ).getIndex() != null;
final String mappedBy = getMappedBy( propertyValue );
final IdMappingData referencedIdMapping = mainGenerator.getReferencedIdMappingData(
referencingEntityName,
referencedEntityName,
propertyAuditingData,
false
);
final IdMappingData referencingIdMapping = referencingEntityConfiguration.getIdMappingData();
// Generating the id mappers data for the referencing side of the relation.
final MiddleIdData referencingIdData = createMiddleIdData(
referencingIdMapping,
mappedBy + "_",
referencingEntityName
);
// And for the referenced side. The prefixed mapper won't be used (as this collection isn't persisted
// in a join table, so the prefix value is arbitrary).
final MiddleIdData referencedIdData = createMiddleIdData(
referencedIdMapping,
null, referencedEntityName
);
// Generating the element mapping.
final MiddleComponentData elementComponentData = new MiddleComponentData(
new MiddleRelatedComponentMapper( referencedIdData ), 0
);
// Generating the index mapping, if an index exists. It can only exists in case a javax.persistence.MapKey
// annotation is present on the entity. So the middleEntityXml will be not be used. The queryGeneratorBuilder
// will only be checked for nullnes.
MiddleComponentData indexComponentData = addIndex( null, null );
// Generating the query generator - it should read directly from the related entity.
final RelationQueryGenerator queryGenerator = new OneAuditEntityQueryGenerator(
mainGenerator.getGlobalCfg(),
mainGenerator.getVerEntCfg(),
mainGenerator.getAuditStrategy(),
referencingIdData,
referencedEntityName,
referencedIdData,
isEmbeddableElementType(),
mappedBy,
isMappedByKey( propertyValue, mappedBy )
);
// Creating common mapper data.
final CommonCollectionMapperData commonCollectionMapperData = new CommonCollectionMapperData(
mainGenerator.getVerEntCfg(), referencedEntityName,
propertyAuditingData.getPropertyData(),
referencingIdData, queryGenerator
);
PropertyMapper fakeBidirectionalRelationMapper;
PropertyMapper fakeBidirectionalRelationIndexMapper;
if ( fakeOneToManyBidirectional || indexed ) {
// In case of a fake many-to-one bidirectional relation, we have to generate a mapper which maps
// the mapped-by property name to the id of the related entity (which is the owner of the collection).
final String auditMappedBy;
if ( fakeOneToManyBidirectional ) {
auditMappedBy = propertyAuditingData.getAuditMappedBy();
}
else {
auditMappedBy = propertyValue.getMappedByProperty();
}
// Creating a prefixed relation mapper.
final IdMapper relMapper = referencingIdMapping.getIdMapper().prefixMappedProperties(
MappingTools.createToOneRelationPrefix( auditMappedBy )
);
fakeBidirectionalRelationMapper = new ToOneIdMapper(
relMapper,
// The mapper will only be used to map from entity to map, so no need to provide other details
// when constructing the PropertyData.
new PropertyData( auditMappedBy, null, null, null ),
referencingEntityName, false
);
final String positionMappedBy;
if ( fakeOneToManyBidirectional ) {
positionMappedBy = propertyAuditingData.getPositionMappedBy();
}
else if ( indexed ) {
final Value indexValue = ( (IndexedCollection) propertyValue ).getIndex();
positionMappedBy = indexValue.getColumnIterator().next().getText();
}
else {
positionMappedBy = null;
}
// Checking if there's an index defined. If so, adding a mapper for it.
if ( positionMappedBy != null ) {
fakeBidirectionalRelationIndexMapper = new SinglePropertyMapper(
new PropertyData(
positionMappedBy,
null,
null,
null
)
);
// Also, overwriting the index component data to properly read the index.
indexComponentData = new MiddleComponentData(
new MiddleStraightComponentMapper( positionMappedBy ),
0
);
}
else {
fakeBidirectionalRelationIndexMapper = null;
}
}
else {
fakeBidirectionalRelationMapper = null;
fakeBidirectionalRelationIndexMapper = null;
}
// Checking the type of the collection and adding an appropriate mapper.
addMapper( commonCollectionMapperData, elementComponentData, indexComponentData );
// Storing information about this relation.
referencingEntityConfiguration.addToManyNotOwningRelation(
propertyName,
mappedBy,
referencedEntityName,
referencingIdData.getPrefixedMapper(),
fakeBidirectionalRelationMapper,
fakeBidirectionalRelationIndexMapper,
indexed
);
}
/**
* Adds mapping of the id of a related entity to the given xml mapping, prefixing the id with the given prefix.
*
* @param xmlMapping Mapping, to which to add the xml.
* @param prefix Prefix for the names of properties which will be prepended to properties that form the id.
* @param columnNameIterator Iterator over the column names that will be used for properties that form the id.
* @param relatedIdMapping Id mapping data of the related entity.
*/
@SuppressWarnings({"unchecked"})
private void addRelatedToXmlMapping(
Element xmlMapping,
String prefix,
MetadataTools.ColumnNameIterator columnNameIterator,
IdMappingData relatedIdMapping) {
final Element properties = (Element) relatedIdMapping.getXmlRelationMapping().clone();
MetadataTools.prefixNamesInPropertyElement( properties, prefix, columnNameIterator, true, true );
for ( Element idProperty : (java.util.List<Element>) properties.elements() ) {
xmlMapping.add( (Element) idProperty.clone() );
}
}
private String getMiddleTableName(Collection value, String entityName) {
// We check how Hibernate maps the collection.
if ( value.getElement() instanceof OneToMany && !value.isInverse() ) {
// This must be a @JoinColumn+@OneToMany mapping. Generating the table name, as Hibernate doesn't use a
// middle table for mapping this relation.
return StringTools.getLastComponent( entityName ) + "_" + StringTools.getLastComponent(
MappingTools.getReferencedEntityName(
value.getElement()
)
);
}
// Hibernate uses a middle table for mapping this relation, so we get it's name directly.
return value.getCollectionTable().getName();
}
@SuppressWarnings({"unchecked"})
private void addWithMiddleTable() {
LOG.debugf(
"Adding audit mapping for property %s.%s: collection with a join table",
referencingEntityName,
propertyName
);
// Generating the name of the middle table
String auditMiddleTableName;
String auditMiddleEntityName;
if ( !StringTools.isEmpty( propertyAuditingData.getJoinTable().name() ) ) {
auditMiddleTableName = propertyAuditingData.getJoinTable().name();
auditMiddleEntityName = propertyAuditingData.getJoinTable().name();
}
else {
final String middleTableName = getMiddleTableName( propertyValue, referencingEntityName );
auditMiddleTableName = mainGenerator.getVerEntCfg().getAuditTableName( null, middleTableName );
auditMiddleEntityName = mainGenerator.getVerEntCfg().getAuditEntityName( middleTableName );
}
LOG.debugf( "Using join table name: %s", auditMiddleTableName );
// Generating the XML mapping for the middle entity, only if the relation isn't inverse.
// If the relation is inverse, will be later checked by comparing middleEntityXml with null.
Element middleEntityXml;
if ( !propertyValue.isInverse() ) {
// Generating a unique middle entity name
auditMiddleEntityName = mainGenerator.getAuditEntityNameRegister().createUnique( auditMiddleEntityName );
// Registering the generated name
mainGenerator.getAuditEntityNameRegister().register( auditMiddleEntityName );
middleEntityXml = createMiddleEntityXml(
auditMiddleTableName,
auditMiddleEntityName,
propertyValue.getWhere()
);
}
else {
middleEntityXml = null;
}
// ******
// Generating the mapping for the referencing entity (it must be an entity).
// ******
// Getting the id-mapping data of the referencing entity (the entity that "owns" this collection).
final IdMappingData referencingIdMapping = referencingEntityConfiguration.getIdMappingData();
// Only valid for an inverse relation; null otherwise.
String mappedBy;
// The referencing prefix is always for a related entity. So it has always the "_" at the end added.
String referencingPrefixRelated;
String referencedPrefix;
if ( propertyValue.isInverse() ) {
// If the relation is inverse, then referencedEntityName is not null.
mappedBy = getMappedBy(
propertyValue.getCollectionTable(),
mainGenerator.getMetadata().getEntityBinding( referencedEntityName )
);
referencingPrefixRelated = mappedBy + "_";
referencedPrefix = StringTools.getLastComponent( referencedEntityName );
}
else {
mappedBy = null;
referencingPrefixRelated = StringTools.getLastComponent( referencingEntityName ) + "_";
referencedPrefix = referencedEntityName == null ? "element" : propertyName;
}
// Storing the id data of the referencing entity: original mapper, prefixed mapper and entity name.
final MiddleIdData referencingIdData = createMiddleIdData(
referencingIdMapping,
referencingPrefixRelated,
referencingEntityName
);
// Creating a query generator builder, to which additional id data will be added, in case this collection
// references some entities (either from the element or index). At the end, this will be used to build
// a query generator to read the raw data collection from the middle table.
final QueryGeneratorBuilder queryGeneratorBuilder = new QueryGeneratorBuilder(
mainGenerator.getGlobalCfg(),
mainGenerator.getVerEntCfg(),
mainGenerator.getAuditStrategy(),
referencingIdData,
auditMiddleEntityName,
isEmbeddableElementType()
);
// Adding the XML mapping for the referencing entity, if the relation isn't inverse.
if ( middleEntityXml != null ) {
// Adding related-entity (in this case: the referencing's entity id) id mapping to the xml.
addRelatedToXmlMapping(
middleEntityXml, referencingPrefixRelated,
MetadataTools.getColumnNameIterator( propertyValue.getKey().getColumnIterator() ),
referencingIdMapping
);
}
// ******
// Generating the element mapping.
// ******
final MiddleComponentData elementComponentData = addValueToMiddleTable(
propertyValue.getElement(),
middleEntityXml,
queryGeneratorBuilder,
referencedPrefix,
propertyAuditingData.getJoinTable().inverseJoinColumns()
);
// ******
// Generating the index mapping, if an index exists.
// ******
final MiddleComponentData indexComponentData = addIndex( middleEntityXml, queryGeneratorBuilder );
// ******
// Generating the property mapper.
// ******
// Building the query generator.
final RelationQueryGenerator queryGenerator = queryGeneratorBuilder.build( elementComponentData, indexComponentData );
// Creating common data
final CommonCollectionMapperData commonCollectionMapperData = new CommonCollectionMapperData(
mainGenerator.getVerEntCfg(),
auditMiddleEntityName,
propertyAuditingData.getPropertyData(),
referencingIdData,
queryGenerator
);
// Checking the type of the collection and adding an appropriate mapper.
addMapper( commonCollectionMapperData, elementComponentData, indexComponentData );
// ******
// Storing information about this relation.
// ******
storeMiddleEntityRelationInformation( mappedBy );
}
private MiddleComponentData addIndex(Element middleEntityXml, QueryGeneratorBuilder queryGeneratorBuilder) {
if ( propertyValue instanceof IndexedCollection ) {
final IndexedCollection indexedValue = (IndexedCollection) propertyValue;
final String mapKey = propertyAuditingData.getMapKey();
if ( mapKey == null ) {
// This entity doesn't specify a javax.persistence.MapKey. Mapping it to the middle entity.
return addValueToMiddleTable(
indexedValue.getIndex(),
middleEntityXml,
queryGeneratorBuilder,
"mapkey",
null
);
}
else {
final IdMappingData referencedIdMapping = mainGenerator.getEntitiesConfigurations()
.get( referencedEntityName ).getIdMappingData();
final int currentIndex = queryGeneratorBuilder == null ? 0 : queryGeneratorBuilder.getCurrentIndex();
if ( "".equals( mapKey ) ) {
// The key of the map is the id of the entity.
return new MiddleComponentData(
new MiddleMapKeyIdComponentMapper(
mainGenerator.getVerEntCfg(),
referencedIdMapping.getIdMapper()
),
currentIndex
);
}
else {
// The key of the map is a property of the entity.
return new MiddleComponentData(
new MiddleMapKeyPropertyComponentMapper(
mapKey,
propertyAuditingData.getAccessType()
),
currentIndex
);
}
}
}
else {
// No index - creating a dummy mapper.
return new MiddleComponentData( new MiddleDummyComponentMapper(), 0 );
}
}
/**
* @param value Value, which should be mapped to the middle-table, either as a relation to another entity,
* or as a simple value.
* @param xmlMapping If not <code>null</code>, xml mapping for this value is added to this element.
* @param queryGeneratorBuilder In case <code>value</code> is a relation to another entity, information about it
* should be added to the given.
* @param prefix Prefix for proeprty names of related entities identifiers.
* @param joinColumns Names of columns to use in the xml mapping, if this array isn't null and has any elements.
*
* @return Data for mapping this component.
*/
@SuppressWarnings({"unchecked"})
private MiddleComponentData addValueToMiddleTable(
Value value,
Element xmlMapping,
QueryGeneratorBuilder queryGeneratorBuilder,
String prefix,
JoinColumn[] joinColumns) {
final Type type = value.getType();
if ( type instanceof ManyToOneType ) {
final String prefixRelated = prefix + "_";
final String referencedEntityName = MappingTools.getReferencedEntityName( value );
final IdMappingData referencedIdMapping = mainGenerator.getReferencedIdMappingData(
referencingEntityName,
referencedEntityName,
propertyAuditingData,
true
);
// Adding related-entity (in this case: the referenced entities id) id mapping to the xml only if the
// relation isn't inverse (so when <code>xmlMapping</code> is not null).
if ( xmlMapping != null ) {
addRelatedToXmlMapping(
xmlMapping, prefixRelated,
joinColumns != null && joinColumns.length > 0
? MetadataTools.getColumnNameIterator( joinColumns )
: MetadataTools.getColumnNameIterator( value.getColumnIterator() ),
referencedIdMapping
);
}
// Storing the id data of the referenced entity: original mapper, prefixed mapper and entity name.
final MiddleIdData referencedIdData = createMiddleIdData(
referencedIdMapping,
prefixRelated,
referencedEntityName
);
// And adding it to the generator builder.
queryGeneratorBuilder.addRelation( referencedIdData );
return new MiddleComponentData(
new MiddleRelatedComponentMapper( referencedIdData ),
queryGeneratorBuilder.getCurrentIndex()
);
}
else if ( type instanceof ComponentType ) {
// Collection of embeddable elements.
final Component component = (Component) value;
final Class componentClass = ReflectionTools.loadClass(
component.getComponentClassName(),
mainGenerator.getClassLoaderService()
);
final MiddleEmbeddableComponentMapper componentMapper = new MiddleEmbeddableComponentMapper(
new MultiPropertyMapper(),
componentClass
);
final Element parentXmlMapping = xmlMapping.getParent();
final ComponentAuditingData auditData = new ComponentAuditingData();
final ReflectionManager reflectionManager = mainGenerator.getMetadata().getMetadataBuildingOptions().getReflectionManager();
new ComponentAuditedPropertiesReader(
ModificationStore.FULL,
new AuditedPropertiesReader.ComponentPropertiesSource( reflectionManager, component ),
auditData, mainGenerator.getGlobalCfg(), reflectionManager, ""
).read();
// Emulating first pass.
for ( String auditedPropertyName : auditData.getPropertyNames() ) {
final PropertyAuditingData nestedAuditingData = auditData.getPropertyAuditingData( auditedPropertyName );
mainGenerator.addValue(
parentXmlMapping,
component.getProperty( auditedPropertyName ).getValue(),
componentMapper,
prefix, xmlMappingData,
nestedAuditingData,
true,
true,
true
);
}
// Emulating second pass so that the relations can be mapped too.
for ( String auditedPropertyName : auditData.getPropertyNames() ) {
final PropertyAuditingData nestedAuditingData = auditData.getPropertyAuditingData( auditedPropertyName );
mainGenerator.addValue(
parentXmlMapping,
component.getProperty( auditedPropertyName ).getValue(),
componentMapper,
referencingEntityName,
xmlMappingData,
nestedAuditingData,
true,
false,
true
);
}
// Add an additional column holding a number to make each entry unique within the set.
// Embeddable properties may contain null values, so cannot be stored within composite primary key.
if ( propertyValue.isSet() ) {
final String setOrdinalPropertyName = mainGenerator.getVerEntCfg()
.getEmbeddableSetOrdinalPropertyName();
final Element ordinalProperty = MetadataTools.addProperty(
xmlMapping, setOrdinalPropertyName, "integer", true, true
);
MetadataTools.addColumn(
ordinalProperty, setOrdinalPropertyName, null, null, null, null, null, null, false
);
}
return new MiddleComponentData( componentMapper, 0 );
}
else {
// Last but one parameter: collection components are always insertable
final boolean mapped = mainGenerator.getBasicMetadataGenerator().addBasic(
xmlMapping,
new PropertyAuditingData(
prefix,
"field",
ModificationStore.FULL,
RelationTargetAuditMode.AUDITED,
null,
null,
false
),
value,
null,
true,
true
);
if ( mapped ) {
// Simple values are always stored in the first item of the array returned by the query generator.
return new MiddleComponentData(
new MiddleSimpleComponentMapper( mainGenerator.getVerEntCfg(), prefix ),
0
);
}
else {
mainGenerator.throwUnsupportedTypeException( type, referencingEntityName, propertyName );
// Impossible to get here.
throw new AssertionError();
}
}
}
private void addMapper(
CommonCollectionMapperData commonCollectionMapperData,
MiddleComponentData elementComponentData,
MiddleComponentData indexComponentData) {
final Type type = propertyValue.getType();
final boolean embeddableElementType = isEmbeddableElementType();
if ( type instanceof SortedSetType ) {
currentMapper.addComposite(
propertyAuditingData.getPropertyData(),
new SortedSetCollectionMapper(
commonCollectionMapperData,
TreeSet.class,
SortedSetProxy.class,
elementComponentData,
propertyValue.getComparator(),
embeddableElementType,
embeddableElementType
)
);
}
else if ( type instanceof SetType ) {
currentMapper.addComposite(
propertyAuditingData.getPropertyData(),
new BasicCollectionMapper<Set>(
commonCollectionMapperData,
HashSet.class,
SetProxy.class,
elementComponentData,
embeddableElementType,
embeddableElementType
)
);
}
else if ( type instanceof SortedMapType ) {
// Indexed collection, so <code>indexComponentData</code> is not null.
currentMapper.addComposite(
propertyAuditingData.getPropertyData(),
new SortedMapCollectionMapper(
commonCollectionMapperData,
TreeMap.class,
SortedMapProxy.class,
elementComponentData,
indexComponentData,
propertyValue.getComparator(),
embeddableElementType
)
);
}
else if ( type instanceof MapType ) {
// Indexed collection, so <code>indexComponentData</code> is not null.
currentMapper.addComposite(
propertyAuditingData.getPropertyData(),
new MapCollectionMapper<Map>(
commonCollectionMapperData,
HashMap.class,
MapProxy.class,
elementComponentData,
indexComponentData,
embeddableElementType
)
);
}
else if ( type instanceof BagType ) {
currentMapper.addComposite(
propertyAuditingData.getPropertyData(),
new BasicCollectionMapper<List>(
commonCollectionMapperData,
ArrayList.class,
ListProxy.class,
elementComponentData,
embeddableElementType,
embeddableElementType
)
);
}
else if ( type instanceof ListType ) {
// Indexed collection, so <code>indexComponentData</code> is not null.
currentMapper.addComposite(
propertyAuditingData.getPropertyData(),
new ListCollectionMapper(
commonCollectionMapperData,
elementComponentData,
indexComponentData,
embeddableElementType
)
);
}
else {
mainGenerator.throwUnsupportedTypeException( type, referencingEntityName, propertyName );
}
}
private void storeMiddleEntityRelationInformation(String mappedBy) {
// Only if this is a relation (when there is a referenced entity).
if ( referencedEntityName != null ) {
if ( propertyValue.isInverse() ) {
referencingEntityConfiguration.addToManyMiddleNotOwningRelation(
propertyName,
mappedBy,
referencedEntityName
);
}
else {
referencingEntityConfiguration.addToManyMiddleRelation( propertyName, referencedEntityName );
}
}
}
private Element createMiddleEntityXml(String auditMiddleTableName, String auditMiddleEntityName, String where) {
final String schema = mainGenerator.getSchema(
propertyAuditingData.getJoinTable().schema(),
propertyValue.getCollectionTable()
);
final String catalog = mainGenerator.getCatalog(
propertyAuditingData.getJoinTable().catalog(),
propertyValue.getCollectionTable()
);
final Element middleEntityXml = MetadataTools.createEntity(
xmlMappingData.newAdditionalMapping(),
new AuditTableData( auditMiddleEntityName, auditMiddleTableName, schema, catalog ), null, null
);
final Element middleEntityXmlId = middleEntityXml.addElement( "composite-id" );
// If there is a where clause on the relation, adding it to the middle entity.
if ( where != null ) {
middleEntityXml.addAttribute( "where", where );
}
middleEntityXmlId.addAttribute( "name", mainGenerator.getVerEntCfg().getOriginalIdPropName() );
// Adding the revision number as a foreign key to the revision info entity to the composite id of the
// middle table.
mainGenerator.addRevisionInfoRelation( middleEntityXmlId );
// Adding the revision type property to the entity xml.
mainGenerator.addRevisionType(
isEmbeddableElementType() ? middleEntityXmlId : middleEntityXml,
middleEntityXml,
isEmbeddableElementType()
);
// All other properties should also be part of the primary key of the middle entity.
return middleEntityXmlId;
}
/**
* Checks if the collection element is of {@link ComponentType} type.
*/
private boolean isEmbeddableElementType() {
return propertyValue.getElement().getType() instanceof ComponentType;
}
private String getMappedBy(Collection collectionValue) {
final PersistentClass referencedClass = getReferenceCollectionClass( collectionValue );
final ValueHolder valueHolder = new ValueHolder( collectionValue );
return getMappedBy( referencedClass, valueHolder );
}
private String getMappedBy(Table collectionTable, PersistentClass referencedClass) {
return getMappedBy( referencedClass, new ValueHolder( collectionTable ) );
}
private String getMappedBy(PersistentClass referencedClass, ValueHolder valueHolder) {
// If there's an @AuditMappedBy specified, returning it directly.
final String auditMappedBy = propertyAuditingData.getAuditMappedBy();
if ( auditMappedBy != null ) {
return auditMappedBy;
}
// searching in referenced class
String mappedBy = this.searchMappedBy( referencedClass, valueHolder );
if ( mappedBy == null ) {
LOG.debugf(
"Going to search the mapped by attribute for %s in superclasses of entity: %s",
propertyName,
referencedClass.getClassName()
);
PersistentClass tempClass = referencedClass;
while ( mappedBy == null && tempClass.getSuperclass() != null ) {
LOG.debugf( "Searching in superclass: %s", tempClass.getSuperclass().getClassName() );
mappedBy = this.searchMappedBy( tempClass.getSuperclass(), valueHolder );
tempClass = tempClass.getSuperclass();
}
}
if ( mappedBy == null ) {
throw new MappingException(
"Unable to read the mapped by attribute for " + propertyName + " in "
+ referencedClass.getClassName() + "!"
);
}
return mappedBy;
}
private String searchMappedBy(PersistentClass persistentClass, ValueHolder valueHolder) {
if ( valueHolder.getCollection() != null ) {
return searchMappedBy( persistentClass, valueHolder.getCollection() );
}
return searchMappedBy( persistentClass, valueHolder.getTable() );
}
@SuppressWarnings({"unchecked"})
private String searchMappedBy(PersistentClass referencedClass, Collection collectionValue) {
final Iterator<Property> assocClassProps = referencedClass.getPropertyIterator();
while ( assocClassProps.hasNext() ) {
final Property property = assocClassProps.next();
if ( Tools.iteratorsContentEqual(
property.getValue().getColumnIterator(),
collectionValue.getKey().getColumnIterator()
) ) {
return property.getName();
}
}
// HHH-7625
// Support ToOne relations with mappedBy that point to an @IdClass key property.
return searchMappedByKey( referencedClass, collectionValue );
}
@SuppressWarnings({"unchecked"})
private String searchMappedBy(PersistentClass referencedClass, Table collectionTable) {
final Iterator<Property> properties = referencedClass.getPropertyIterator();
while ( properties.hasNext() ) {
final Property property = properties.next();
if ( property.getValue() instanceof Collection ) {
// The equality is intentional. We want to find a collection property with the same collection table.
//noinspection ObjectEquality
if ( ( (Collection) property.getValue() ).getCollectionTable() == collectionTable ) {
return property.getName();
}
}
}
return null;
}
@SuppressWarnings({"unchecked"})
private String searchMappedByKey(PersistentClass referencedClass, Collection collectionValue) {
final Iterator<Value> assocIdClassProps = referencedClass.getKeyClosureIterator();
while ( assocIdClassProps.hasNext() ) {
final Value value = assocIdClassProps.next();
// make sure its a 'Component' because IdClass is registered as this type.
if ( value instanceof Component ) {
final Component component = (Component) value;
final Iterator<Property> componentPropertyIterator = component.getPropertyIterator();
while ( componentPropertyIterator.hasNext() ) {
final Property property = componentPropertyIterator.next();
final Iterator<Selectable> propertySelectables = property.getValue().getColumnIterator();
final Iterator<Selectable> collectionSelectables = collectionValue.getKey().getColumnIterator();
if ( Tools.iteratorsContentEqual( propertySelectables, collectionSelectables ) ) {
return property.getName();
}
}
}
}
return null;
}
private PersistentClass getReferenceCollectionClass(Collection collectionValue) {
PersistentClass referencedClass = null;
if ( collectionValue.getElement() instanceof OneToMany ) {
final OneToMany oneToManyValue = (OneToMany) collectionValue.getElement();
referencedClass = oneToManyValue.getAssociatedClass();
}
else if ( collectionValue.getElement() instanceof ManyToOne ) {
// Case for bi-directional relation with @JoinTable on the owning @ManyToOne side.
final ManyToOne manyToOneValue = (ManyToOne) collectionValue.getElement();
referencedClass = manyToOneValue.getMetadata().getEntityBinding( manyToOneValue.getReferencedEntityName() );
}
return referencedClass;
}
private boolean isMappedByKey(Collection collectionValue, String mappedBy) {
final PersistentClass referencedClass = getReferenceCollectionClass( collectionValue );
if ( referencedClass != null ) {
final String keyMappedBy = searchMappedByKey( referencedClass, collectionValue );
return mappedBy.equals( keyMappedBy );
}
return false;
}
private class ValueHolder {
private Collection collection;
private Table table;
public ValueHolder(Collection collection) {
this.collection = collection;
}
public ValueHolder(Table table) {
this.table = table;