I'm Trying to create an H2 Trigger which should trigger before an Insert
query is executed. My Entity looks like this:
@Entity
@Table(name="Notes")
public class Notes {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@Column(name="id")
private int id;
@Column(name="version")
private int version;
}
I've created trigger:
CREATE TRIGGER update_date Before INSERT ON notes FOR EACH ROW CALL
TriggerBeforeInsert.class.getName()
I would like to set the version field to 1 whenever a new record is
inserted. Here is the code for the Trigger:
public class TriggerBeforeUpdate 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 {
}
}
And when I put a new record in the "notes" table, the trigger is called but
unfortunately the version field does not change the value. It works only if
I changes the id field and the version field like this:
@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;
}
But this is not what I expects. What should I change to make the program
work?
--
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/h2-database/18b58127-913e-473f-be51-cd305527e061%40googlegroups.com.