|
| 1 | +[[is-new-state-detection]] |
| 2 | += Entity State Detection Strategies |
| 3 | + |
| 4 | +The following table describes the strategies that Spring Data offers for detecting whether an entity is new: |
| 5 | + |
| 6 | +.Options for detection whether an entity is new in Spring Data |
| 7 | +[options = "autowidth",cols="1,1"] |
| 8 | +|=== |
| 9 | +|Id-Property inspection (the default) |
| 10 | +|By default, Spring Data inspects the version property of the given entity. |
| 11 | +If the identifier property is `null` or `0` in case of primitive types, then the entity is assumed to be new. |
| 12 | +Otherwise, it is assumed to not be new. |
| 13 | + |
| 14 | +|Version-Property inspection |
| 15 | +|If a property annotated with `@Version` is present and `null`, or in case of a version property of primitive type `0` the entity is considered new. |
| 16 | +If the version property is present but has a different value, the entity is considered to not be new. |
| 17 | +If no version property is present Spring Data falls back to inspection of the Id-Property. |
| 18 | + |
| 19 | +|Implementing `Persistable` |
| 20 | +|If an entity implements `Persistable`, Spring Data delegates the new detection to the `isNew(…)` method of the entity. |
| 21 | +See the link:https://docs.spring.io/spring-data/data-commons/docs/current/api/index.html?org/springframework/data/domain/Persistable.html[Javadoc] for details. |
| 22 | + |
| 23 | +_Note: Properties of `Persistable` will get detected and persisted if you use `AccessType.PROPERTY`. |
| 24 | +To avoid that, use `@Transient`._ |
| 25 | + |
| 26 | +|Implementing `EntityInformation` |
| 27 | +|You can customize the `EntityInformation` abstraction used in the repository base implementation by creating a subclass of the module specific repository factory and overriding the `getEntityInformation(…)` method. |
| 28 | +You then have to register the custom implementation of module specific repository factory as a Spring bean. |
| 29 | +Note that this should rarely be necessary. |
| 30 | +|=== |
0 commit comments