[英]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 使用三种策略来确定实体是否是新的:
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.否则,假定它不是新的。
Implementing
Persistable
: If an entity implementsPersistable
, Spring Data JPA delegates the new detection to theisNew(…)
method of the entity.实现
Persistable
:如果实体实现Persistable
,则 Spring 数据 JPA 将新检测委托给实体的 isNewisNew(…)
方法。 See the JavaDoc for details.有关详细信息,请参阅JavaDoc 。
Implementing
EntityInformation
: You can customize theEntityInformation
abstraction used in theSimpleJpaRepository
implementation by creating a subclass ofJpaRepositoryFactory
and overriding thegetEntityInformation(…)
method accordingly.实现
EntityInformation
:您可以通过创建JpaRepositoryFactory
的子类并相应地覆盖getEntityInformation(…)
方法来自定义SimpleJpaRepository
实现中使用的EntityInformation
抽象。 You then have to register the custom implementation ofJpaRepositoryFactory
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.
希望能帮助到你。
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.