Spring-data-jpa-duplicate-key-value-violates-unique-constraint
Passing a detached entity to the save() method can sometimes lead JPA to treat it as a new record (attempting an INSERT ) rather than an update, causing a primary key collision.
Use a repository method like existsByEmail(String email) before attempting a save. While this doesn't solve high-concurrency race conditions, it eliminates the majority of "honest" mistakes. Passing a detached entity to the save() method
Spring then catches this vendor-specific SQL exception and wraps it in a DataIntegrityViolationException . This abstraction is helpful for maintaining database-agnostic code, but it requires the developer to look at the "Root Cause" in the stack trace to identify which specific constraint was violated. Common Triggers in Spring Data JPA Passing a detached entity to the save() method