我在PostgreSQL数据库中创建了一个唯一约束,并将其设置为deferrable initially deferred
。
例如,我在列order_num
上有唯一键,我在数据库中有两行,如下所示:
id order_num description
1 1 'something no 1'
2 2 'something no 2'
当我尝试在数据库上执行此脚本时(即使按照这种非逻辑顺序),它运行良好:
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;
但是当我尝试从应用程序执行此操作时(即使按逻辑顺序,先删除,然后插入),它说duplicate key value violates unique constraint
。
有人知道hibernate有什么问题吗?我怎样才能设定延期?
编辑:
好的,唯一约束包括id_ri_content
,id_structure_table_column
和element_order
。
当我从PG管理员运行事务时,日志显示:
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;
虽然在java会话bean方法的hibernate中运行相同的操作会产生此错误日志:
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.
答案 0 :(得分:0)
检查Hibernate正在使用的数据源的JTA是否设置为TRUE。