我正在尝试创建H2触发器,该触发器应在执行插入查询之前触发。 我的实体看起来像这样:
@Entity
@Table(name="Notes")
public class Notes {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@Column(name="id")
private int id;
@Column(name="version")
private int version;
}
我已创建触发器:
CREATE TRIGGER update_date Before INSERT ON notes FOR EACH ROW CALL
TriggerBeforeInsert.class.getName()
无论何时插入新记录,我都希望将version字段设置为1。 这是触发器的代码:
public class TriggerBeforeInsert implements Trigger {
@Override
public void init(Connection connection, String s, String s1, String s2,
boolean b, int i) throws SQLException {
}
@Override
public void fire(Connection connection, Object[] oldRow, Object[] newRow)
throws SQLException {
newRow[1] = 1;
}
@Override
public void close() throws SQLException {
}
@Override
public void remove() throws SQLException {
}
}
当我在“ notes”表中放入新记录时,将调用触发器,但不幸的是,version字段不会更改该值。 仅当我更改id字段和version字段时,它才有效:
@Override
public void fire(Connection connection, Object[] oldRow, Object[] newRow)
throws SQLException {
newRow[0] = 2 // any value differs from the current value
newRow[1] = 1;
}
但这不是我期望的。我应该进行哪些更改才能使程序正常工作?