我有一个Oracle数据库模式的HSQL版本来执行单元测试。
我需要使用当前时间戳更新更新列。
我实现的触发器在没有hsql引擎投诉的情况下被加载,但是当我尝试更新行时它在运行时崩溃。
以下是一个示例测试用例,您可以在使用spring和junit配置的项目中运行:
public class UtSqlTriggerTest {
@Test public void testTrigger() throws SQLException {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
resourceDatabasePopulator.addScript(new InMemoryResource(
"CREATE TABLE TEST (ID NUMERIC NOT NULL PRIMARY KEY,DATA VARCHAR(200), LAST_UPDATE TIMESTAMP);\n" +
"CREATE TRIGGER updTimestamp AFTER UPDATE OF DATA ON TEST\n" +
"REFERENCING NEW AS newrow OLD AS oldrow\n" +
"FOR EACH ROW\n" +
"SET newrow.LAST_UPDATE = current_timestamp;"));
Connection connection = DriverManager.getConnection("jdbc:hsqldb:file:/opt/db/testdb;shutdown=true", "SA", "");
resourceDatabasePopulator.populate(connection);
JdbcTemplate tjdbc = new JdbcTemplate(new SingleConnectionDataSource(connection, true));
tjdbc.update("INSERT INTO TEST(ID, DATA) VALUES (0, 'HELLO')");
tjdbc.update("UPDATE TEST SET DATA = 'HELLO WORLD' WHERE ID = 0");
tjdbc.queryForObject("SELECT LAST_UPDATE FROM TEST WHERE ID = 0", Date.class);
}
}
这个触发器出了什么问题?为什么它生成了ArrayIndexOutOfBoundException?
CREATE TRIGGER updateDateAjoutFichier AFTER UPDATE OF DATA ON TEST
REFERENCING NEW AS newrow OLD AS oldrow
FOR EACH ROW
SET newrow.LAST_UPDATE = current_timestamp;
答案 0 :(得分:3)
在SQL客户端中运行CREATE TRIGGER时,您将看到以下错误消息:
尝试分配给不可更新的列:LAST_UPDATE [SQL State = 0U000,DB Errorcode = -2500]
这是因为您尝试修改AFTER
触发器中的列。只能在BEFORE
触发器中更改列值。所以你应该使用:
CREATE TRIGGER updTimestamp BEFORE UPDATE OF DATA ON TEST
REFERENCING NEW AS newrow OLD AS oldrow
FOR EACH ROW
SET newrow.LAST_UPDATE = current_timestamp;