@OneToOne mapping with unique=true throws "caused a duplicate key value in a unique or primary key constraint'

98 Views Asked by At

The following mapping works fine in Hibernate 3.x.

@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, optional = true)
@JoinColumn(name = "PAYLOAD_FILTER", unique = true)
private PayloadFilterExpression payloadFilter;

enter image description here

The same mapping throws the following error in Hibernate 5.x.

Table NMS_DAMPEN_CONFIG has a one-to-one relationship with NMS_PAYLOAD_FILTER_EXPR table. Column PAYLOAD_FILTER is populated based on index col value of NMS_PAYLOAD_FILTER_EXPR

2023-04-10 01:35:02.749 ERROR [org.hibernate.engine.jdbc.batch.internal.BatchingBatch] HHH000315:
 Exception executing batch [java.sql.BatchUpdateException: The statement was aborted because it would
 have caused a duplicate key value in a unique or primary key constraint or unique index identified by
 'UK_HJWHE1QKYLIS421SH7BTMYPH1' defined on 'NMS_DAMPEN_CONFIG'.], SQL: insert into NMS_DAMPEN_CONFIG
 (UUID, VERSION, ENABLE, HR_INTERVAL, MIN_INTERVAL, PAYLOAD_FILTER, SEC_INTERVAL, ID) values (?, ?, ?, ?, ?, ?, ?, ?)

We tried changing the annotations to see if the inserts in the dependent table goes through, but that did not help. For example, tried Cascade type "MERGE" and also a combo of persist and merge.

We tried relaxing the unique key in order to check the inserts and it works fine. But the concern is the upgrades might fail. So we don't want to change the existing mappings.

0

There are 0 best solutions below