Skip to content
Advertisement

Integrity constraint violation: unique constraint or index violation HSQLDB

For example if I add new Department(new BigInteger("50"), "ODD", "SPB"), all work, it values are insert into database. But if I want again insert for example new Department(new BigInteger("50"), "ODDMOD", "SPBMOD"), appear java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: unique constraint or index violation; SYS_PK_10092 table: DEPARTMENT. I know that can not insert values with the same primary key, but how can update values if primary key exists or other solutions?

Advertisement

Answer

You want an upsert here:

A MERGE behaves by doing an insert if the department id does not already exist in the table. Otherwise it will do an update. Note that if you shift to JPA/Hibernate from pure JDBC, the JPA save() method can upsert automatically for you under the hood.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement