Skip to content

Adds a new snippet for is-new-state-detection #2352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.5.0-SNAPSHOT</version>
<version>2.5.0-state-detection-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
30 changes: 30 additions & 0 deletions src/main/asciidoc/is-new-state-detection.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[[is-new-state-detection]]
= Entity State Detection Strategies

The following table describes the strategies that Spring Data offers for detecting whether an entity is new:

.Options for detection whether an entity is new in Spring Data
[options = "autowidth",cols="1,1"]
|===
|Id-Property inspection (the default)
|By default, Spring Data inspects the version property of the given entity.
If the identifier property is `null` or `0` in case of primitive types, then the entity is assumed to be new.
Otherwise, it is assumed to not be new.

|Version-Property inspection
|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.
If the version property is present but has a different value, the entity is considered to not be new.
If no version property is present Spring Data falls back to inspection of the Id-Property.

|Implementing `Persistable`
|If an entity implements `Persistable`, Spring Data delegates the new detection to the `isNew(…)` method of the entity.
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.

_Note: Properties of `Persistable` will get detected and persisted if you use `AccessType.PROPERTY`.
To avoid that, use `@Transient`._

|Implementing `EntityInformation`
|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.
You then have to register the custom implementation of module specific repository factory as a Spring bean.
Note that this should rarely be necessary.
|===