Check if JTA is set to TRUE for the data source that Hibernate is using.
Postgres unique constraint not deferrable in hibernate
-
15-06-2023 - |
Question
I created an unique constraint in PostgreSQL database and set it to deferrable initially deferred
.
For example, I have unique key on column order_num
and I have two rows in database like this:
id order_num description
1 1 'something no 1'
2 2 'something no 2'
When I tried to do this script on database (even in this unlogical order), it worked fine:
begin transaction;
insert into some_table(id, order_num, description) values(3, 2, 'something no 3');
delete from some_table where id = 2;
end transaction;
But when I tried to do this from application (even in logical order, first delete, then insert) it said duplicate key value violates unique constraint
.
Does someone know what is problem with hibernate? How can I set deferrable on it?
EDIT:
Ok, unique constraint consists of id_ri_content
, id_structure_table_column
and element_order
.
When I run transaction from PG admin, log shows this:
2014-03-17 13:45:15 CET LOG: statement: begin transaction; insert into ri_content_cell(id_ri_content_cell, id_ri_content, id_ri_structure_table_column, element_order, content) values(118, 41, 226, 2, '3'); insert into ri_content_cell(id_ri_content_cell, id_ri_content, id_ri_structure_table_column, element_order, content) values(119, 41, 228, 2, 'Zika'); delete from ri_content_cell where id_ri_content_cell in (120,121); end transaction;
While running the same in hibernate from java session bean method produce this error log:
2014-03-17 13:54:25 CET LOG: execute : select nextval ('ri_content_cell_seq')
2014-03-17 13:54:25 CET LOG: execute : select nextval ('ri_content_cell_seq')
2014-03-17 13:54:25 CET LOG: execute : insert into ri_content_cell (content, element_order, id_ri_content, id_ri_structure_table_column, id_ri_content_cell) values ($1, $2, $3, $4, $5)
2014-03-17 13:54:25 CET DETAIL: parameters: $1 = '2', $2 = '2', $3 = '41', $4 = '226', $5 = '136'
2014-03-17 13:54:25 CET ERROR: duplicate key value violates unique constraint "uk_ri_content_cell"
2014-03-17 13:54:25 CET DETAIL: Key (id_ri_content, id_ri_structure_table_column, element_order)=(41, 226, 2) already exists.
Solution