/
AssociationAttribute.java
335 lines (283 loc) · 10.5 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
/*
* 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.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.EnumConversionHelper;
import org.hibernate.metamodel.internal.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.internal.source.annotations.JPADotNames;
import org.hibernate.metamodel.internal.source.annotations.JandexHelper;
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.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;
// todo FetchMode is currently used in the persisters. This will probably get replaced bt FetchStyle and FetchTiming
private final FetchMode fetchMode;
private final FetchStyle fetchStyle;
private final boolean mapsId;
private final String referencedIdAttributeName;
private boolean isInsertable = true;
private boolean isUpdatable = true;
private AttributeTypeResolver resolver;
public static AssociationAttribute createAssociationAttribute(String name,
Class<?> attributeType,
AttributeNature attributeNature,
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
return new AssociationAttribute(
name,
attributeType,
attributeNature,
accessType,
annotations,
context
);
}
AssociationAttribute(String name,
Class<?> javaType,
AttributeNature attributeNature,
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
super( name, javaType, attributeNature, accessType, annotations, context );
this.ignoreNotFound = ignoreNotFound();
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 );
this.mappedBy = determineMappedByAttributeName( associationAnnotation );
this.isOptional = determineOptionality( associationAnnotation );
this.isLazy = determineFetchType( associationAnnotation );
this.isOrphanRemoval = determineOrphanRemoval( associationAnnotation );
this.cascadeTypes = determineCascadeTypes( associationAnnotation );
this.fetchMode = determineFetchMode();
this.fetchStyle = determineFetchStyle();
this.referencedIdAttributeName = determineMapsId();
this.mapsId = referencedIdAttributeName != null;
}
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;
}
@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 isInsertable;
}
@Override
public boolean isUpdatable() {
return isUpdatable;
}
@Override
public PropertyGeneration getPropertyGeneration() {
return PropertyGeneration.NEVER;
}
private AttributeTypeResolver getDefaultHibernateTypeResolver() {
return new CompositeAttributeTypeResolver( new AttributeTypeResolverImpl( this ) );
}
private boolean ignoreNotFound() {
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;
}
private boolean determineFetchType(AnnotationInstance associationAnnotation) {
boolean lazy = false;
AnnotationValue fetchValue = associationAnnotation.value( "fetch" );
if ( fetchValue != null ) {
FetchType fetchType = Enum.valueOf( FetchType.class, fetchValue.asEnum() );
if ( FetchType.LAZY.equals( fetchType ) ) {
lazy = true;
}
}
return lazy;
}
private String determineReferencedEntityType(AnnotationInstance associationAnnotation) {
// use the annotated attribute type as default target type
String targetTypeName = getAttributeType().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() {
String referencedIdAttributeName;
AnnotationInstance mapsIdAnnotation = JandexHelper.getSingleAnnotation( annotations(), JPADotNames.MAPS_ID );
if ( mapsIdAnnotation == null ) {
return null;
}
if ( !( AttributeNature.MANY_TO_ONE.equals( getAttributeNature() ) || AttributeNature.MANY_TO_ONE
.equals( getAttributeNature() ) ) ) {
throw new MappingException(
"@MapsId can only be specified on a many-to-one or one-to-one associations",
getContext().getOrigin()
);
}
referencedIdAttributeName = JandexHelper.getValue( mapsIdAnnotation, "value", String.class );
return referencedIdAttributeName;
}
}