此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Data Commons 3.4.4spring-doc.cadn.net.cn

实体状态检测策略

下表描述了 Spring Data 提供的用于检测实体是否为新实体的策略:spring-doc.cadn.net.cn

表 1.用于检测实体是否为 Spring Data 中的新实体的选项

@Id-Property inspection(默认)spring-doc.cadn.net.cn

默认情况下, Spring Data 检查给定实体的 identifier 属性。 如果 identifier 属性为null0如果是基元类型,则假定实体是 new。 否则,将假定它不是 new。spring-doc.cadn.net.cn

@Version-物业检查spring-doc.cadn.net.cn

如果带有@Version存在且null,或者 version 属性为基元类型0该实体被视为新实体。 如果 version 属性存在但具有不同的值,则认为该实体不是新实体。 如果不存在version属性,则 Spring Data 将回退到对标识符属性的检查。spring-doc.cadn.net.cn

实施Persistablespring-doc.cadn.net.cn

如果实体实现Persistable中,Spring Data 将新的检测委托给isNew(…)实体的方法。 有关详细信息,请参阅 Javadocspring-doc.cadn.net.cn

注意:的属性Persistable将被检测到并持久化AccessType.PROPERTY. 为避免这种情况,请使用@Transient.spring-doc.cadn.net.cn

提供自定义EntityInformation实现spring-doc.cadn.net.cn

您可以自定义EntityInformation通过创建特定于模块的 repository factory 的子类并覆盖getEntityInformation(…)方法。 然后,您必须将特定于模块的存储库工厂的自定义实现注册为 Spring Bean。 请注意,这很少是必需的。spring-doc.cadn.net.cn