1. How does Spring data JPA determine whether an object is a new object?
Spring data provides three strategies:
Able 2.2. Options for detection whether a entity is new in Spring Data JPA
Id-property Inspection (default) |
By default Spring Data JPA inspects the id-property of the given Entity. If null The Id-property is and then the entity would be assumed as new, otherwise as not new. |
ImplementingPersistable |
If an entity implements Persistable the interface, Spring Data JPA would delegate the New-detection isNew to the -Method of the Entity. See Thejavadoc for details. |
ImplementingEntityInformation |
One can customize The entityinformation abstraction used in The simplejparepository implementation by creating a subclass Ofjparepositoryfactory and overriding The getentityinformation -method accordingly. One then have to register the custom implementation Ofjparepositoryfactory as a Spring bean. Note that this should is rarely necessary. See The javadoc for details. |
You can see that the default is to determine if there is an ID. You can then take other strategies, such as implementing an Persistable
interface, and then overriding the IsNew method.
2. Spring Data annotations
Spring Data JPA