Skip to content

Manually set IDs with @GeneratedValue cause exception in Spring Boot 3.4 or above / Hibernate 6.6.x #3910

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
PrashantSainiSDE opened this issue Jun 6, 2025 · 1 comment
Labels
for: external-project For an external project and not something we can fix

Comments

@PrashantSainiSDE
Copy link

Description

Starting with Spring Boot 3.4 (which brings Hibernate 6.6.x), manually assigning an ID to an entity annotated with @GeneratedValue causes a runtime exception (ObjectOptimisticLockingFailureException), whereas in Spring Boot 3.3.2 (Hibernate 6.5.x), the same code executes without error — Hibernate would simply ignore the manually set ID.

This appears to be a regression or at least a behavioral change that may impact existing test code and data seeding scripts.


Sample Code to Reproduce

@Entity
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String email;
    private Long salary;
}

// In test or data setup
Employee employee = Employee.builder()
    .id(1L) // Manually setting ID
    .name("Test")
    .email("test@example.com")
    .salary(100L)
    .build();

employeeRepository.save(employee);

Behavior

✅ Expected (Spring Boot 3.3.x / Hibernate 6.5.x)
Manually set ID is ignored.

Entity is saved with generated ID without any exception.

❌ Actual (Spring Boot 3.4.x / Hibernate 6.6.x)
Exception is thrown:

org.springframework.orm.ObjectOptimisticLockingFailureException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.app.testing.entities.Employee#1]

Environment

  • Spring Boot: 3.4.x
  • spring-data-jpa: latest (as managed by Spring Boot 3.4)
  • Hibernate: 6.6.x
  • JDK: 21

Question

Is this change in behavior intentional due to Hibernate upgrade?
If so, could it be noted in release notes or migration guide?
Or is this a candidate for backward-compatible support or configuration toggle?

Thanks for your time!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 6, 2025
@mp911de
Copy link
Member

mp911de commented Jun 10, 2025

This is a Hibernate change, see the Hibernate forums for a discussion around that topic.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Jun 10, 2025
@mp911de mp911de added for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 10, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

3 participants