[Solved] org.h2.jdbc. JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation

Hibernate throws ConstraintViolationException, when tries to insert a row or update particular column or delete records that violate underlying database integrity constraints.
Below is some common situation where ConstraintViolationException occurs:

  • Insert a record with primary key as null.
  • Insert/ Update primary key value is not unique.
  • Insert data value size is more than predefine column size.
  • The inserted record data type is not compatible with predefined column type.

To see a complete list of Database integrity constraints follow this link :

Database Integrity Constraints List

ConstraintViolationException Example

Here this exception is occurring because inserting same value of title again that causing the issue . As configured title is unique.

@Entity
public class Publication {
	@Id
	@GeneratedValue
	protected Long id;

	@NotNull
	@Size(max = 100)
	@Column(unique = true)
	private String title;

	private int year;
	//getter and setter methods
	}

Exception Stacktrace


org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation: 
"PUBLIC.UK_C5HPK0II0AH4H4EAAGM8HQUO0_INDEX_C ON PUBLIC.PUBLICATIONS(TITLE) VALUES 2"; SQL statement:
insert into publications (id, title, type, year) values (null, ?, ?, ?) [23505-199]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:457) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:427) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.message.DbException.get(DbException.java:205) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.message.DbException.get(DbException.java:181) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.index.BaseIndex.getDuplicateKeyException(BaseIndex.java:103) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.mvstore.db.MVSecondaryIndex.checkUnique(MVSecondaryIndex.java:220) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.mvstore.db.MVSecondaryIndex.add(MVSecondaryIndex.java:196) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.mvstore.db.MVTable.addRow(MVTable.java:546) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.command.dml.Insert.insertRows(Insert.java:180) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.command.dml.Insert.update(Insert.java:132) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.command.CommandContainer.update(CommandContainer.java:133) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.command.Command.executeUpdate(Command.java:267) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:200) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:154) ~[h2-1.4.199.jar:1.4.199]
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.2.0.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.2.0.jar:na]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]

Solutions

As title column configured is unique will not allow duplicate values. Because here unique integrity constraint is violating that’s why throwing JDBCIntegrityConstaintViolationException.

To resolve this issue pass primary key column value as unique and not NULL.

You would like to see

Follow below link to see more JDBC, Hibernate and JPA issues solutions.