Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

886
Views
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.

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!