首页 > 代码库 > Spring data jpa

Spring data jpa

1. Spring data jpa怎么去判断一个对象的是否为新对象呢?

 spring  data 提供了三种策略:

able 2.2. Options for detection whether an 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 the Id-Property is null, then the entity will be assumed as new, otherwise as not new.
ImplementingPersistableIf an entity implements the Persistable interface, Spring Data JPA will delegate the new-detection to the isNew - Method of the Entity. See theJavaDoc for details.
ImplementingEntityInformationOne can customize the EntityInformation abstraction used in the SimpleJpaRepository implementation by creating a subclass ofJpaRepositoryFactory and overriding the getEntityInformation-Method accordingly. One then has to register the custom implementation ofJpaRepositoryFactory as a Spring bean. Note that this should be rarely necessary. See the JavaDoc for details.

 可以看出默认是判断是否有id。然后也可以采取其他策略,比如实现Persistable接口,然后重写isNew方法。

 

2. Spring data注解

 

Spring data jpa