The following table describes the strategies that Spring Data offers for detecting whether an entity is new:spring-doc.cn

Table 1. Options for detection whether an entity is new in Spring Data

@Id-Property inspection (the default)spring-doc.cn

By default, Spring Data inspects the identifier 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.spring-doc.cn

@Version-Property inspectionspring-doc.cn

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 identifier property.spring-doc.cn

Implementing Persistablespring-doc.cn

If an entity implements Persistable, Spring Data delegates the new detection to the isNew(…) method of the entity. See the Javadoc for details.spring-doc.cn

Note: Properties of Persistable will get detected and persisted if you use AccessType.PROPERTY. To avoid that, use @Transient.spring-doc.cn

Providing a custom EntityInformation implementationspring-doc.cn

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.spring-doc.cn