OK, let's test it with JDBC:
import java.sql.*;
import org.h2.api.Trigger;

public class TestTrigger {
    public static class T implements Trigger {
        @Override
        public void init(Connection conn, String schemaName, String 
triggerName, String tableName, boolean before,
                int type) throws SQLException {}

        @Override
        public void fire(Connection conn, Object[] oldRow, Object[] newRow) 
throws SQLException {
            newRow[1] = 1;
        }

        @Override
        public void close() throws SQLException {}
        @Override
        public void remove() throws SQLException {}
    }
    public static void main(String[] args) throws Exception {
        try (Connection c = DriverManager.getConnection("jdbc:h2:mem:1")) {
            Statement s = c.createStatement();
            s.execute("CREATE TABLE NOTES(" //
                    + "ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY 
KEY, " //
                    + "VERSION INT)");
            s.execute("CREATE TRIGGER T1 BEFORE INSERT ON NOTES FOR EACH 
ROW CALL \"" + T.class.getName() + '"');
            s.executeUpdate("INSERT INTO NOTES() VALUES ()");
            s.executeUpdate("INSERT INTO NOTES(VERSION) VALUES (5)");
            c.setAutoCommit(false);
            s.executeUpdate("INSERT INTO NOTES() VALUES ()");
            s.executeUpdate("INSERT INTO NOTES(VERSION) VALUES (10)");
            c.commit();
            try (ResultSet rs = s.executeQuery("TABLE NOTES")) {
                while (rs.next()) {
                    System.out.printf("ID=%d, VERSION=%d%n", rs.getLong(1), 
rs.getInt(2));
                }
            }
        }
    }
}
With the latest version of H2 (1.4.199) it prints
ID=1, VERSION=1
ID=2, VERSION=1
ID=3, VERSION=1
ID=4, VERSION=1
So such trigger works as expected.

Because you're using some implementation of JPA your problem seems to be 
related with it and not with H2.

-- 
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/9854ef44-b694-40ae-a6e2-4a3fe7c2701d%40googlegroups.com.

Reply via email to