1
1
/*
2
- * Copyright 2012-2014 the original author or authors.
2
+ * Copyright 2012-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
37
37
import javax .persistence .Version ;
38
38
import javax .persistence .metamodel .Metamodel ;
39
39
40
+ import org .springframework .core .annotation .AnnotationUtils ;
40
41
import org .springframework .data .annotation .AccessType .Type ;
41
42
import org .springframework .data .mapping .Association ;
42
43
import org .springframework .data .mapping .PersistentEntity ;
43
44
import org .springframework .data .mapping .model .AnnotationBasedPersistentProperty ;
44
45
import org .springframework .data .mapping .model .SimpleTypeHolder ;
46
+ import org .springframework .data .util .ClassTypeInformation ;
47
+ import org .springframework .data .util .TypeInformation ;
45
48
import org .springframework .util .Assert ;
46
49
47
50
/**
@@ -76,6 +79,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
76
79
77
80
private final Metamodel metamodel ;
78
81
private final Boolean usePropertyAccess ;
82
+ private final TypeInformation <?> associationTargetType ;
79
83
80
84
/**
81
85
* Creates a new {@link JpaPersistentPropertyImpl}
@@ -95,6 +99,26 @@ public JpaPersistentPropertyImpl(Metamodel metamodel, Field field, PropertyDescr
95
99
96
100
this .metamodel = metamodel ;
97
101
this .usePropertyAccess = detectPropertyAccess ();
102
+ this .associationTargetType = isAssociation () ? detectAssociationTargetType () : null ;
103
+ }
104
+
105
+ /*
106
+ * (non-Javadoc)
107
+ * @see org.springframework.data.mapping.model.AbstractPersistentProperty#getActualType()
108
+ */
109
+ @ Override
110
+ public Class <?> getActualType () {
111
+ return associationTargetType == null ? super .getActualType () : associationTargetType .getType ();
112
+ }
113
+
114
+ /*
115
+ * (non-Javadoc)
116
+ * @see org.springframework.data.mapping.model.AbstractPersistentProperty#getPersistentEntityType()
117
+ */
118
+ @ Override
119
+ public Iterable <? extends TypeInformation <?>> getPersistentEntityType () {
120
+ return associationTargetType == null ? super .getPersistentEntityType () : Collections
121
+ .singleton (associationTargetType );
98
122
}
99
123
100
124
/*
@@ -215,4 +239,24 @@ private Boolean detectPropertyAccess() {
215
239
access = findPropertyOrOwnerAnnotation (Access .class );
216
240
return access == null ? null : AccessType .PROPERTY .equals (access .value ());
217
241
}
242
+
243
+ /**
244
+ * Inspects the association annotations on the property and returns the target entity type if specified.
245
+ *
246
+ * @return
247
+ */
248
+ private TypeInformation <?> detectAssociationTargetType () {
249
+
250
+ for (Class <? extends Annotation > associationAnnotation : ASSOCIATION_ANNOTATIONS ) {
251
+
252
+ Annotation annotation = findAnnotation (associationAnnotation );
253
+ Object targetEntity = AnnotationUtils .getValue (annotation , "targetEntity" );
254
+
255
+ if (targetEntity != null && !void .class .equals (targetEntity )) {
256
+ return ClassTypeInformation .from ((Class <?>) targetEntity );
257
+ }
258
+ }
259
+
260
+ return null ;
261
+ }
218
262
}
0 commit comments