Jav*_*avi 3 postgresql triggers hibernate plpgsql rowcount
我有一个使用Hibernate的应用程序,我必须包含一个触发器,将所有在表中修改或删除的行复制到历史表中.包含PostgreSQL触发器后,应用程序无法正常工作并发出此错误:
org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException:批量更新从update [0]返回意外的行数; 实际行数:0; 预期:1; 嵌套异常是org.hibernate.StaleStateException:批量更新从update [0]返回意外的行数; 实际行数:0; 预期:1
谷歌搜索了一段时间之后我发现这个错误是因为Hibernate通过sql update检查受影响的行,并且返回的行不是预期的行,因为触发器也进行了更新.我已经看到可以通过关闭触发器上的rowcount来解决这个问题.但PostgreSQL似乎没有'set nocount on'的替代品.
如何在PostgreSQL的触发器中解决问题,如下所示?
谢谢
CREATE OR REPLACE FUNCTION my_trigger_fnc() RETURNS TRIGGER AS $my_trigger_fnc$
DECLARE
nowDate timestamp := now();
BEGIN
INSERT INTO historial_table (
id,
date_now,
id_mytable,
--some other fields
...
)
VALUES (
nextVal('historial_table_seq'),
nowDate,
OLD.id_mytable
--some other fields
...
);
RETURN NEW;
END;
$my_trigger_fnc$ LANGUAGE plpgsql;
CREATE TRIGGER my_trigger BEFORE UPDATE OR DELETE
ON my_table FOR EACH ROW
EXECUTE PROCEDURE my_trigger_fnc();
Run Code Online (Sandbox Code Playgroud)
更新:表格就像这样:
CREATE TABLE historial_table(
id integer,
date_now timestamp NOT NULL,
id_mytable integer NOT NULL,
nserie character varying(255),
idstore integer,
idmodel integer,
automatic boolean,
broken boolean,
idAlb integer,
idInc integer,
id_item integer,
date_assign timestamp,
PRIMARY KEY (id)
);
CREATE TABLE my_table(
id_mytable integer NOT NULL,
nserie character varying(255),
idstore integer,
idmodel integer,
automatic boolean,
broken boolean,
idAlb integer,
idInc integer,
id_item integer,
date_assign timestamp,
PRIMARY KEY (id_mytable)
);
Run Code Online (Sandbox Code Playgroud)
"返回NEW;" 对DELETE持怀疑态度.可能会混淆rowcount(NULL ==零?) http://developer.postgresql.org/pgdocs/postgres/plpgsql-trigger.html(非常通用;不打算侮辱你......)TG_OP上的开关可能是什么你需要.
| 归档时间: |
|
| 查看次数: |
2826 次 |
| 最近记录: |