You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Polishing of annotation model for object creators.
Move to @PersistenceCreator as canonical annotation to explicitly express constructors and methods to be used to create domain object instances from persistence operations. Removed @factorymethod as it's not needed anymore. @PersistenceConstructor is now deprecated.
Renamed EntityCreatorMetadata(Support|Discoverer) to InstanceCreatorMetadata(Support|Discoverer) to avoid further manifestation of the notion of an entity in the metamodel as it's not used to only handle entities.
Issue #2476.
Copy file name to clipboardExpand all lines: src/main/asciidoc/object-mapping.adoc
+7-7
Original file line number
Diff line number
Diff line change
@@ -17,9 +17,9 @@ This means we need two fundamental steps:
17
17
Spring Data automatically tries to detect a persistent entity's constructor to be used to materialize objects of that type.
18
18
The resolution algorithm works as follows:
19
19
20
-
1. If there is a single static factory method annotated with `@FactoryMethod` then it is used.
20
+
1. If there is a single static factory method annotated with `@PersistenceCreator` then it is used.
21
21
2. If there is a single constructor, it is used.
22
-
3. If there are multiple constructors and exactly one is annotated with `@PersistenceConstructor`, it is used.
22
+
3. If there are multiple constructors and exactly one is annotated with `@PersistenceCreator`, it is used.
23
23
4. If there's a no-argument constructor, it is used.
24
24
Other constructors will be ignored.
25
25
@@ -205,9 +205,9 @@ Even if the intent is that the calculation should be preferred, it's important t
205
205
<4> The `comment` property is mutable is populated by setting its field directly.
206
206
<5> The `remarks` properties are mutable and populated by setting the `comment` field directly or by invoking the setter method for
207
207
<6> The class exposes a factory method and a constructor for object creation.
208
-
The core idea here is to use factory methods instead of additional constructors to avoid the need for constructor disambiguation through `@PersistenceConstructor`.
208
+
The core idea here is to use factory methods instead of additional constructors to avoid the need for constructor disambiguation through `@PersistenceCreator`.
209
209
Instead, defaulting of properties is handled within the factory method.
210
-
If you want Spring Data to use the factory method for object instantiation, annotate it with `@FactoryMethod`.
210
+
If you want Spring Data to use the factory method for object instantiation, annotate it with `@PersistenceCreator`.
211
211
212
212
[[mapping.general-recommendations]]
213
213
== General recommendations
@@ -217,7 +217,7 @@ Also, this avoids your domain objects to be littered with setter methods that al
217
217
If you need those, prefer to make them package protected so that they can only be invoked by a limited amount of co-located types.
218
218
Constructor-only materialization is up to 30% faster than properties population.
219
219
* _Provide an all-args constructor_ -- Even if you cannot or don't want to model your entities as immutable values, there's still value in providing a constructor that takes all properties of the entity as arguments, including the mutable ones, as this allows the object mapping to skip the property population for optimal performance.
220
-
* _Use factory methods instead of overloaded constructors to avoid ``@PersistenceConstructor``_ -- With an all-argument constructor needed for optimal performance, we usually want to expose more application use case specific constructors that omit things like auto-generated identifiers etc.
220
+
* _Use factory methods instead of overloaded constructors to avoid ``@PersistenceCreator``_ -- With an all-argument constructor needed for optimal performance, we usually want to expose more application use case specific constructors that omit things like auto-generated identifiers etc.
221
221
It's an established pattern to rather use static factory methods to expose these variants of the all-args constructor.
222
222
* _Make sure you adhere to the constraints that allow the generated instantiator and property accessor classes to be used_ --
223
223
* _For identifiers to be generated, still use a final field in combination with an all-arguments persistence constructor (preferred) or a `with…` method_ --
@@ -307,14 +307,14 @@ data class Person(val id: String, val name: String)
307
307
----
308
308
====
309
309
310
-
The class above compiles to a typical class with an explicit constructor.We can customize this class by adding another constructor and annotate it with `@PersistenceConstructor` to indicate a constructor preference:
310
+
The class above compiles to a typical class with an explicit constructor.We can customize this class by adding another constructor and annotate it with `@PersistenceCreator` to indicate a constructor preference:
311
311
312
312
====
313
313
[source,kotlin]
314
314
----
315
315
data class Person(var id: String, val name: String) {
0 commit comments