8

I'm using CrudRepository.save(Entity) to insert an Entity with @ID already populated.

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.

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).

I kind of feel that this a bug in spring-data-jdbc. Just wanted an opinion/solution here.

user3740148
  • 117
  • 1
  • 7

1 Answers1

11

According to the Spring Data Refrence Documetnation, the underlying JPA EntiyManager uses three strategies to determine if an entity is new:

  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. If there is the entity is considered new if the value is null. Without such a Version-property Spring Data JPA inspects the identifier property of the given entity. If the identifier property is null, then the entity is assumed to be new. 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. See the JavaDoc for details.

  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. You then have to register the custom implementation of JpaRepositoryFactory as a Spring bean. Note that this should be rarely necessary. See the JavaDoc for details.

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. This is correct but does not change the answer since the same behaviour is documented in Spring Data JDBC - Reference Documentation.

Bernard
  • 333
  • 2
  • 8
  • 1
    the question is not about JPA, iit is about spring-data-jdbc. It has no underlying JPA – Eugene Dec 02 '20 at 12:45
  • I agree, during my research, I probably had multiple tabs open and used the wrong one to answer the question. Nevertheless, this doesn't invalidate the answers since the same behaviour is documented in the [Spring Data JPA - Reference Documentation](https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/#jdbc.entity-persistence.state-detection-strategies). But for the answer to be precise, I'll add it to the original answer. – Bernard Dec 03 '20 at 15:51