-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
AssociationAttribute.java
409 lines (349 loc) · 12.9 KB
/
AssociationAttribute.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
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.internal.source.annotations.attribute;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.DotName;
import org.hibernate.FetchMode;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.engine.FetchStyle;
import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.internal.source.annotations.attribute.type.AttributeTypeResolver;
import org.hibernate.metamodel.internal.source.annotations.attribute.type.AttributeTypeResolverImpl;
import org.hibernate.metamodel.internal.source.annotations.attribute.type.CompositeAttributeTypeResolver;
import org.hibernate.metamodel.internal.source.annotations.entity.EntityBindingContext;
import org.hibernate.metamodel.internal.source.annotations.util.EnumConversionHelper;
import org.hibernate.metamodel.internal.source.annotations.util.HibernateDotNames;
import org.hibernate.metamodel.internal.source.annotations.util.JPADotNames;
import org.hibernate.metamodel.internal.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.spi.source.MappingException;
/**
* Represents an association attribute.
*
* @author Hardy Ferentschik
*/
public class AssociationAttribute extends MappedAttribute {
private final boolean ignoreNotFound;
private final String referencedEntityType;
private final String mappedBy;
private final Set<CascadeType> cascadeTypes;
private final boolean isOptional;
private final boolean isLazy;
private final boolean isOrphanRemoval;
private final FetchMode fetchMode;
private final FetchStyle fetchStyle;
private final boolean mapsId;
private final String referencedIdAttributeName;
private final List<Column> joinColumnValues;
private final boolean definesExplicitJoinTable;
private AttributeTypeResolver resolver;
public static AssociationAttribute createAssociationAttribute(
String name,
Class<?> attributeType,
Nature attributeNature,
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
return new AssociationAttribute(
name,
attributeType,
attributeType,
attributeNature,
accessType,
annotations,
context
);
}
AssociationAttribute(
String name,
Class<?> attributeType,
Class<?> referencedAttributeType,
Nature attributeNature,
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
super( name, attributeType, attributeNature, accessType, annotations, context );
this.ignoreNotFound = determineNotFoundBehavior();
AnnotationInstance associationAnnotation = JandexHelper.getSingleAnnotation(
annotations,
attributeNature.getAnnotationDotName()
);
// using jandex we don't really care which exact type of annotation we are dealing with
this.referencedEntityType = determineReferencedEntityType( associationAnnotation, referencedAttributeType );
this.mappedBy = determineMappedByAttributeName( associationAnnotation );
this.isOptional = determineOptionality( associationAnnotation );
this.isLazy = determineIsLazy( associationAnnotation );
this.isOrphanRemoval = determineOrphanRemoval( associationAnnotation );
this.cascadeTypes = determineCascadeTypes( associationAnnotation );
this.joinColumnValues = determineJoinColumnAnnotations( annotations );
this.fetchMode = determineFetchMode();
this.fetchStyle = determineFetchStyle();
this.referencedIdAttributeName = determineMapsId();
this.mapsId = referencedIdAttributeName != null;
this.definesExplicitJoinTable = determineExplicitJoinTable( annotations );
}
public boolean isIgnoreNotFound() {
return ignoreNotFound;
}
public String getReferencedEntityType() {
return referencedEntityType;
}
public String getMappedBy() {
return mappedBy;
}
public Set<CascadeType> getCascadeTypes() {
return cascadeTypes;
}
public boolean isOrphanRemoval() {
return isOrphanRemoval;
}
public FetchMode getFetchMode() {
return fetchMode;
}
public FetchStyle getFetchStyle() {
return fetchStyle;
}
public String getReferencedIdAttributeName() {
return referencedIdAttributeName;
}
public boolean mapsId() {
return mapsId;
}
public List<Column> getJoinColumnValues() {
return joinColumnValues;
}
public boolean definesExplicitJoinTable() {
return definesExplicitJoinTable;
}
@Override
public AttributeTypeResolver getHibernateTypeResolver() {
if ( resolver == null ) {
resolver = getDefaultHibernateTypeResolver();
}
return resolver;
}
@Override
public boolean isLazy() {
return isLazy;
}
@Override
public boolean isOptional() {
return isOptional;
}
@Override
public boolean isInsertable() {
return true;
}
@Override
public boolean isUpdatable() {
return true;
}
@Override
public PropertyGeneration getPropertyGeneration() {
return PropertyGeneration.NEVER;
}
private AttributeTypeResolver getDefaultHibernateTypeResolver() {
return new CompositeAttributeTypeResolver( new AttributeTypeResolverImpl( this ) );
}
private boolean determineNotFoundBehavior() {
NotFoundAction action = NotFoundAction.EXCEPTION;
AnnotationInstance notFoundAnnotation = JandexHelper.getSingleAnnotation(
annotations(),
HibernateDotNames.NOT_FOUND
);
if ( notFoundAnnotation != null ) {
AnnotationValue actionValue = notFoundAnnotation.value( "action" );
if ( actionValue != null ) {
action = Enum.valueOf( NotFoundAction.class, actionValue.asEnum() );
}
}
return NotFoundAction.IGNORE.equals( action );
}
private boolean determineOptionality(AnnotationInstance associationAnnotation) {
boolean optional = true;
AnnotationValue optionalValue = associationAnnotation.value( "optional" );
if ( optionalValue != null ) {
optional = optionalValue.asBoolean();
}
return optional;
}
private boolean determineOrphanRemoval(AnnotationInstance associationAnnotation) {
boolean orphanRemoval = false;
AnnotationValue orphanRemovalValue = associationAnnotation.value( "orphanRemoval" );
if ( orphanRemovalValue != null ) {
orphanRemoval = orphanRemovalValue.asBoolean();
}
return orphanRemoval;
}
protected boolean determineIsLazy(AnnotationInstance associationAnnotation) {
FetchType fetchType = JandexHelper.getEnumValue( associationAnnotation, "fetch", FetchType.class );
return FetchType.LAZY == fetchType;
}
private String determineReferencedEntityType(AnnotationInstance associationAnnotation, Class<?> referencedAttributeType) {
// use the annotated attribute type as default target type
String targetTypeName = referencedAttributeType.getName();
// unless we have an explicit @Target
AnnotationInstance targetAnnotation = JandexHelper.getSingleAnnotation(
annotations(),
HibernateDotNames.TARGET
);
if ( targetAnnotation != null ) {
targetTypeName = targetAnnotation.value().asClass().name().toString();
}
AnnotationValue targetEntityValue = associationAnnotation.value( "targetEntity" );
if ( targetEntityValue != null ) {
targetTypeName = targetEntityValue.asClass().name().toString();
}
return targetTypeName;
}
private String determineMappedByAttributeName(AnnotationInstance associationAnnotation) {
String mappedBy = null;
AnnotationValue mappedByAnnotationValue = associationAnnotation.value( "mappedBy" );
if ( mappedByAnnotationValue != null ) {
mappedBy = mappedByAnnotationValue.asString();
}
return mappedBy;
}
private Set<CascadeType> determineCascadeTypes(AnnotationInstance associationAnnotation) {
Set<CascadeType> cascadeTypes = new HashSet<CascadeType>();
AnnotationValue cascadeValue = associationAnnotation.value( "cascade" );
if ( cascadeValue != null ) {
String[] cascades = cascadeValue.asEnumArray();
for ( String s : cascades ) {
cascadeTypes.add( Enum.valueOf( CascadeType.class, s ) );
}
}
return cascadeTypes;
}
private FetchMode determineFetchMode() {
FetchMode mode = FetchMode.DEFAULT;
AnnotationInstance fetchAnnotation = JandexHelper.getSingleAnnotation( annotations(), HibernateDotNames.FETCH );
if ( fetchAnnotation != null ) {
org.hibernate.annotations.FetchMode annotationFetchMode = JandexHelper.getEnumValue(
fetchAnnotation,
"value",
org.hibernate.annotations.FetchMode.class
);
mode = EnumConversionHelper.annotationFetchModeToHibernateFetchMode( annotationFetchMode );
}
return mode;
}
private FetchStyle determineFetchStyle() {
FetchStyle style = FetchStyle.SELECT;
AnnotationInstance fetchAnnotation = JandexHelper.getSingleAnnotation( annotations(), HibernateDotNames.FETCH );
if ( fetchAnnotation != null ) {
org.hibernate.annotations.FetchMode annotationFetchMode = JandexHelper.getEnumValue(
fetchAnnotation,
"value",
org.hibernate.annotations.FetchMode.class
);
style = EnumConversionHelper.annotationFetchModeToFetchStyle( annotationFetchMode );
}
return style;
}
private String determineMapsId() {
AnnotationInstance mapsIdAnnotation = JandexHelper.getSingleAnnotation( annotations(), JPADotNames.MAPS_ID );
if ( mapsIdAnnotation == null ) {
return null;
}
if ( !( Nature.MANY_TO_ONE.equals( getNature() ) || Nature.MANY_TO_ONE
.equals( getNature() ) ) ) {
throw new MappingException(
"@MapsId can only be specified on a many-to-one or one-to-one associations",
getContext().getOrigin()
);
}
return JandexHelper.getValue( mapsIdAnnotation, "value", String.class );
}
private List<Column> determineJoinColumnAnnotations(Map<DotName, List<AnnotationInstance>> annotations) {
ArrayList<Column> joinColumns = new ArrayList<Column>();
// single @JoinColumn
AnnotationInstance joinColumnAnnotation = JandexHelper.getSingleAnnotation(
annotations,
JPADotNames.JOIN_COLUMN
);
if ( joinColumnAnnotation != null ) {
joinColumns.add( new Column( joinColumnAnnotation ) );
}
// @JoinColumns
AnnotationInstance joinColumnsAnnotation = JandexHelper.getSingleAnnotation(
annotations,
JPADotNames.JOIN_COLUMNS
);
if ( joinColumnsAnnotation != null ) {
List<AnnotationInstance> columnsList = Arrays.asList(
JandexHelper.getValue( joinColumnsAnnotation, "value", AnnotationInstance[].class )
);
for ( AnnotationInstance annotation : columnsList ) {
joinColumns.add( new Column( annotation ) );
}
}
joinColumns.trimToSize();
return joinColumns;
}
private boolean determineExplicitJoinTable(Map<DotName, List<AnnotationInstance>> annotations) {
AnnotationInstance collectionTableAnnotation = JandexHelper.getSingleAnnotation(
annotations,
JPADotNames.COLLECTION_TABLE
);
AnnotationInstance joinTableAnnotation = JandexHelper.getSingleAnnotation(
annotations,
JPADotNames.JOIN_TABLE
);
// sanity checks
if ( collectionTableAnnotation != null && joinTableAnnotation != null ) {
throw new MappingException(
"@CollectionTable and JoinTable specified on the same attribute",
getContext().getOrigin()
);
}
if ( collectionTableAnnotation != null ) {
if ( JandexHelper.getSingleAnnotation( annotations, JPADotNames.ELEMENT_COLLECTION ) == null ) {
throw new MappingException(
"@CollectionTable annotation without a @ElementCollection",
getContext().getOrigin()
);
}
}
if ( joinTableAnnotation != null ) {
if ( JandexHelper.getSingleAnnotation( annotations, JPADotNames.ONE_TO_ONE ) == null
&& JandexHelper.getSingleAnnotation( annotations, JPADotNames.ONE_TO_MANY ) == null
&& JandexHelper.getSingleAnnotation( annotations, JPADotNames.MANY_TO_MANY ) == null ) {
throw new MappingException(
"@JoinTable annotation without an association",
getContext().getOrigin()
);
}
}
return collectionTableAnnotation != null || joinTableAnnotation != null;
}
}