Hibernate ConstraintViolationException Java Origin: Application & API OSI layer 7 · Application Last updated 12 Oct 2026

org.hibernate.exception.ConstraintViolationException: could not execute statement

The database rejected an insert/update because of a constraint (unique, foreign key, not null).

Exact message you may see could not execute statement
Seen on: Java

Meaning

Hibernate wraps the JDBC error; the root cause at the bottom of the stack names the constraint (e.g. Duplicate entry, violates foreign key constraint).

Common causes

  • Duplicate unique value
  • Missing referenced row (FK)
  • NULL in a NOT NULL column
  • Bean Validation (jakarta.validation) annotations not checked before save

⚡ Quick fix

  1. Scroll to the root cause for the DB message
  2. Validate before saving
  3. Translate to a 409/422 response in your API

Detailed fix by platform

Java

  1. Spring: catch DataIntegrityViolationException at the service/controller boundary

How to diagnose

  1. Root cause — Which constraint?
  2. Data — Which value violates it?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

1022 — MySQL ERROR 1022 (23000): Can't write; duplicate key in table (MySQL)10221062 — MySQL ERROR 1062 (23000): Duplicate entry for key (MySQL)106223503 parent — ERROR: update or delete on table "users" violates foreign key constraint on table "orders" — Key (id)=(5) is still referenced (PostgreSQL)23503 parent23505 — PostgreSQL: ERROR: duplicate key value violates unique constraint (PostgreSQL)23505515 — SQL Server Msg 515: Cannot insert the value NULL into column 'X', table 'Y'; column does not allow nulls (SQL)515ALREADY_EXISTS — API Error: Resource already exists (ALREADY_EXISTS / resource_already_exists / 409) (API)ALREADY_EXISTSDbUpdateException — DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details. (.NET)DbUpdateExceptionforeign key order — Foreign key violation caused by insert order or circular references (SQL)foreign key orderHibernate…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.