简体   繁体   English

使用 CrudRepository.save(Entity) 使用 spring-data-jdbc 插入具有预定义 @ID 字段的实体

[英]Using CrudRepository.save(Entity) to insert an Entity with predefined @ID field using spring-data-jdbc

I'm using CrudRepository.save(Entity) to insert an Entity with @ID already populated.我正在使用CrudRepository.save(Entity)插入一个已填充@ID的实体。

I'm getting the following exception.我收到以下异常。

Caused by: org.springframework.dao.IncorrectUpdateSemanticsDataAccessException: Failed to update entity [com.comanyname.repository.Entiy.class]. Id [ed384316-9c22-4b63-90fd-ec02fdac2b86] not found in database.

I can understand the issue as @ID field is already populated, AggregateChangeExecutor here is treating this action as DbAction.Update.我可以理解这个问题,因为@ID字段已经填充, 这里的 AggregateChangeExecutor 将此操作视为 DbAction.Update。

So, I wanted to know if there is any way I can use CrudRepository.save(Entity) to insert a new entity with a predefined @ID field as I've wanted to use @DomainEvent hook on CrudRepository.save(Entity).所以,我想知道是否有任何方法可以使用CrudRepository.save(Entity)插入具有预定义@ID字段的新实体,因为我想在 CrudRepository.save(Entity) 上使用@DomainEvent挂钩。

I kind of feel that this a bug in spring-data-jdbc .我觉得这是spring-data-jdbc中的一个错误。 Just wanted an opinion/solution here.只是想在这里发表意见/解决方案。

According to the Spring Data Refrence Documetnation , the underlying JPA EntiyManager uses three strategies to determine if an entity is new:根据 Spring 数据参考文档,底层 JPA EntiyManager 使用三种策略来确定实体是否是新的:

  1. Version-Property and Id-Property inspection (default): By default Spring Data JPA inspects first if there is a Version-property of non-primitive type.版本属性和 Id 属性检查(默认):默认情况下 Spring 数据 JPA 首先检查是否存在非原始类型的版本属性。 If there is the entity is considered new if the value is null .如果值为null ,则该实体被视为新实体。 Without such a Version-property Spring Data JPA inspects the identifier property of the given entity.如果没有这样的版本属性 Spring 数据 JPA 检查给定实体的标识符属性。 If the identifier property is null , then the entity is assumed to be new.如果标识符属性是null ,则假定实体是新的。 Otherwise, it is assumed to be not new.否则,假定它不是新的。

  2. Implementing Persistable : If an entity implements Persistable , Spring Data JPA delegates the new detection to the isNew(…) method of the entity.实现Persistable :如果实体实现Persistable ,则 Spring 数据 JPA 将新检测委托给实体的 isNew isNew(…)方法。 See the JavaDoc for details.有关详细信息,请参阅JavaDoc

  3. 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.实现EntityInformation :您可以通过创建JpaRepositoryFactory的子类并相应地覆盖getEntityInformation(…)方法来自定义SimpleJpaRepository实现中使用的EntityInformation抽象。 You then have to register the custom implementation of JpaRepositoryFactory as a Spring bean.然后,您必须将 JpaRepositoryFactory 的自定义实现注册为JpaRepositoryFactory bean。 Note that this should be rarely necessary.请注意,这应该很少需要。 See the JavaDoc for details.有关详细信息,请参阅JavaDoc

In your case, using the second strategy might be the most useful.在您的情况下,使用第二种策略可能是最有用的。 Hope it helps.希望能帮助到你。

Edit编辑

It was pointed out to me, that the question was not about JPA, but spring-data-jdbc.有人向我指出,问题不是关于 JPA,而是关于 spring-data-jdbc。 This is correct but does not change the answer since the same behaviour is documented in Spring Data JDBC - Reference Documentation .这是正确的,但不会改变答案,因为Spring 数据 JDBC - 参考文档中记录了相同的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 spring-data-jdbc 插入不可变记录 - How to insert an immutable record using spring-data-jdbc spring-data-jdbc:包含具有 1-n 关系的实体的查询 - spring-data-jdbc: query containing entity with a 1-n relation 带有存储库保存方法更新ID的spring-data-jdbc更新 - spring-data-jdbc update with repository save methods updates ID 有没有办法使用Spring Data的CrudRepository#save()方法保存带有预定义@EmbeddedId值的@Entity? - Is there a way to save an @Entity with pre-defined @EmbeddedId value using Spring Data's CrudRepository#save() method? 使用 CrudRepository 保存带有外键的实体对象 - Save entity object with foreign key using CrudRepository 如何在 Spring 数据 JDBC 的 CrudRepository 的查询中引用实体? - How to reference entity in Query in CrudRepository in Spring Data JDBC? 保存与 spring-data-jdbc 具有一对多关系的实体返回空列表 - Saving entity with one-to-many relations with spring-data-jdbc returns empty list CrudRepository.save 是否为 spring 数据 1.13.18.RELEASE 返回 null - Does CrudRepository.save return null for spring data 1.13.18.RELEASE 为什么 Spring-data-jdbc 不保存我的 Car 对象? - Why does Spring-data-jdbc not save my Car object? 使用spring-data-jdbc进行审核? - Auditing with spring-data-jdbc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM