From 2e2dc9394575cd19030b8d5b99221d09007aafec Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Thu, 11 Oct 2018 16:19:08 +0200 Subject: [PATCH] DATAJPA-767 - Improved documentation about state detection for entities. --- src/main/asciidoc/jpa.adoc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/asciidoc/jpa.adoc b/src/main/asciidoc/jpa.adoc index ac43c9d25..e529aa882 100644 --- a/src/main/asciidoc/jpa.adoc +++ b/src/main/asciidoc/jpa.adoc @@ -138,7 +138,12 @@ Saving an entity can be performed with the `CrudRepository.save(…)` method. It ==== Entity State-detection Strategies Spring Data JPA offers the following strategies to detect whether an entity is new or not: -* Id-Property inspection (*default*): By default Spring Data JPA inspects the identifier property of the given entity. If the identifier property is `null`, then the entity is assumed to be new. Otherwise, it is assumed to be not new. +* Version-Property and Id-Property inspection (*default*): + By default Spring Data JPA inspects first if there is a Version-property of non-primitive type. + If there is the entity is considered new if the value is `null`. + Without such a Version-property Spring Data JPA inspects the identifier property of the given entity. + If the identifier property is `null`, then the entity is assumed to be new. + Otherwise, it is assumed to be not new. * Implementing `Persistable`: If an entity implements `Persistable`, Spring Data JPA delegates the new detection to the `isNew(…)` method of the entity. See the link:$$http://docs.spring.io/spring-data/data-commons/docs/current/api/index.html?org/springframework/data/domain/Persistable.html$$[JavaDoc] for details. * Implementing `EntityInformation`: You can customize the `EntityInformation` abstraction used in the `SimpleJpaRepository` implementation by creating a subclass of `JpaRepositoryFactory` and overriding the `getEntityInformation(…)` method accordingly. You then have to register the custom implementation of `JpaRepositoryFactory` as a Spring bean. Note that this should be rarely necessary. See the link:$$http://docs.spring.io/spring-data/data-jpa/docs/current/api/index.html?org/springframework/data/jpa/repository/support/JpaRepositoryFactory.html$$[JavaDoc] for details.