16
16
package org .springframework .data .mapping ;
17
17
18
18
import java .lang .annotation .Annotation ;
19
+ import java .util .Iterator ;
19
20
20
21
import org .springframework .data .convert .EntityInstantiator ;
21
22
import org .springframework .data .util .TypeInformation ;
28
29
* @author Jon Brisbin
29
30
* @author Patryk Wasik
30
31
* @author Mark Paluch
32
+ * @author Christoph Strobl
31
33
*/
32
34
public interface PersistentEntity <T , P extends PersistentProperty <P >> {
33
35
34
36
/**
35
37
* The entity name including any package prefix.
36
38
*
37
- * @return must never return {@literal null}
39
+ * @return must never return {@literal null}.
38
40
*/
39
41
String getName ();
40
42
@@ -51,7 +53,7 @@ public interface PersistentEntity<T, P extends PersistentProperty<P>> {
51
53
* Returns whether the given {@link PersistentProperty} is referred to by a constructor argument of the
52
54
* {@link PersistentEntity}.
53
55
*
54
- * @param property
56
+ * @param property can be {@literal null}.
55
57
* @return true if the given {@link PersistentProperty} is referred to by a constructor argument or {@literal false}
56
58
* if not or {@literal null}.
57
59
*/
@@ -60,16 +62,16 @@ public interface PersistentEntity<T, P extends PersistentProperty<P>> {
60
62
/**
61
63
* Returns whether the given {@link PersistentProperty} is the id property of the entity.
62
64
*
63
- * @param property
64
- * @return
65
+ * @param property can be {@literal null}.
66
+ * @return {@literal true} given property is the entities id.
65
67
*/
66
68
boolean isIdProperty (PersistentProperty <?> property );
67
69
68
70
/**
69
71
* Returns whether the given {@link PersistentProperty} is the version property of the entity.
70
72
*
71
- * @param property
72
- * @return
73
+ * @param property can be {@literal null}.
74
+ * @return {@literal true} given property is used as version.
73
75
*/
74
76
boolean isVersionProperty (PersistentProperty <?> property );
75
77
@@ -81,6 +83,13 @@ public interface PersistentEntity<T, P extends PersistentProperty<P>> {
81
83
*/
82
84
P getIdProperty ();
83
85
86
+ /**
87
+ * Returns the id property of the {@link PersistentEntity}.
88
+ *
89
+ * @return the id property of the {@link PersistentEntity}.
90
+ * @throws IllegalStateException if {@link PersistentEntity} does not define an {@literal id} property.
91
+ * @since 2.0
92
+ */
84
93
default P getRequiredIdProperty () {
85
94
86
95
P property = getIdProperty ();
@@ -100,6 +109,14 @@ default P getRequiredIdProperty() {
100
109
*/
101
110
P getVersionProperty ();
102
111
112
+ /**
113
+ * Returns the version property of the {@link PersistentEntity}. Can be {@literal null} in case no version property is
114
+ * available on the entity.
115
+ *
116
+ * @return the version property of the {@link PersistentEntity}.
117
+ * @throws IllegalStateException if {@link PersistentEntity} does not define a {@literal version} property.
118
+ * @since 2.0
119
+ */
103
120
default P getRequiredVersionProperty () {
104
121
105
122
P property = getVersionProperty ();
@@ -114,17 +131,17 @@ default P getRequiredVersionProperty() {
114
131
/**
115
132
* Obtains a {@link PersistentProperty} instance by name.
116
133
*
117
- * @param name The name of the property
134
+ * @param name The name of the property. Can be {@literal null}.
118
135
* @return the {@link PersistentProperty} or {@literal null} if it doesn't exist.
119
136
*/
120
137
P getPersistentProperty (String name );
121
138
122
139
/**
123
140
* Returns the {@link PersistentProperty} with the given name.
124
141
*
125
- * @param name the name of the property, must not be {@literal null} or empty.
142
+ * @param name the name of the property. Can be {@literal null} or empty.
126
143
* @return the {@link PersistentProperty} with the given name.
127
- * @throws IllegalArgumentException in case no property with the given name exists.
144
+ * @throws IllegalStateException in case no property with the given name exists.
128
145
*/
129
146
default P getRequiredPersistentProperty (String name ) {
130
147
@@ -138,34 +155,47 @@ default P getRequiredPersistentProperty(String name) {
138
155
}
139
156
140
157
/**
141
- * Returns the property equipped with an annotation of the given type.
158
+ * Returns the first property equipped with an {@link Annotation} of the given type.
142
159
*
143
160
* @param annotationType must not be {@literal null}.
144
- * @return
161
+ * @return {@literal null} if no property found with given annotation type.
145
162
* @since 1.8
146
163
*/
147
- P getPersistentProperty (Class <? extends Annotation > annotationType );
164
+ default P getPersistentProperty (Class <? extends Annotation > annotationType ) {
165
+
166
+ Iterator <P > it = getPersistentProperties (annotationType ).iterator ();
167
+ return it .hasNext () ? it .next () : null ;
168
+ }
169
+
170
+ /**
171
+ * Returns all properties equipped with an {@link Annotation} of the given type.
172
+ *
173
+ * @param annotationType must not be {@literal null}.
174
+ * @return {@literal empty} {@link Iterator} if no match found. Never {@literal null}.
175
+ * @since 2.0
176
+ */
177
+ Iterable <P > getPersistentProperties (Class <? extends Annotation > annotationType );
148
178
149
179
/**
150
180
* Returns whether the {@link PersistentEntity} has an id property. If this call returns {@literal true},
151
181
* {@link #getIdProperty()} will return a non-{@literal null} value.
152
182
*
153
- * @return
183
+ * @return {@literal true} if entity has an {@literal id} property.
154
184
*/
155
185
boolean hasIdProperty ();
156
186
157
187
/**
158
188
* Returns whether the {@link PersistentEntity} has a version property. If this call returns {@literal true},
159
189
* {@link #getVersionProperty()} will return a non-{@literal null} value.
160
190
*
161
- * @return
191
+ * @return {@literal true} if entity has a {@literal version} property.
162
192
*/
163
193
boolean hasVersionProperty ();
164
194
165
195
/**
166
196
* Returns the resolved Java type of this entity.
167
197
*
168
- * @return The underlying Java class for this entity
198
+ * @return The underlying Java class for this entity. Never {@literal null}.
169
199
*/
170
200
Class <T > getType ();
171
201
@@ -194,6 +224,12 @@ default P getRequiredPersistentProperty(String name) {
194
224
195
225
void doWithProperties (SimplePropertyHandler handler );
196
226
227
+ /**
228
+ * Get {@link Iterable}
229
+ *
230
+ * @return
231
+ * @since 2.0
232
+ */
197
233
Iterable <P > getPersistentProperties ();
198
234
199
235
/**
@@ -211,7 +247,7 @@ default P getRequiredPersistentProperty(String name) {
211
247
* Looks up the annotation of the given type on the {@link PersistentEntity}.
212
248
*
213
249
* @param annotationType must not be {@literal null}.
214
- * @return
250
+ * @return {@literal null} if not found.
215
251
* @since 1.8
216
252
*/
217
253
<A extends Annotation > A findAnnotation (Class <A > annotationType );
@@ -220,7 +256,7 @@ default P getRequiredPersistentProperty(String name) {
220
256
* Checks whether the annotation of the given type is present on the {@link PersistentEntity}.
221
257
*
222
258
* @param annotationType must not be {@literal null}.
223
- * @return
259
+ * @return {@literal true} if {@link Annotation} of given type is present.
224
260
* @since 2.0
225
261
*/
226
262
<A extends Annotation > boolean isAnnotationPresent (Class <A > annotationType );
@@ -229,7 +265,7 @@ default P getRequiredPersistentProperty(String name) {
229
265
* Returns a {@link PersistentPropertyAccessor} to access property values of the given bean.
230
266
*
231
267
* @param bean must not be {@literal null}.
232
- * @return
268
+ * @return new {@link PersistentPropertyAccessor}.
233
269
* @since 1.10
234
270
*/
235
271
PersistentPropertyAccessor getPropertyAccessor (Object bean );
@@ -238,7 +274,7 @@ default P getRequiredPersistentProperty(String name) {
238
274
* Returns the {@link IdentifierAccessor} for the given bean.
239
275
*
240
276
* @param bean must not be {@literal null}.
241
- * @return
277
+ * @return new {@link IdentifierAccessor}.
242
278
* @since 1.10
243
279
*/
244
280
IdentifierAccessor getIdentifierAccessor (Object bean );
0 commit comments