Hi,
I use H2 (most recent version 1.4.197) for Junits and it works fine.
Unfortunatelly, we started to use Views as a Union all from more tables and
views and instead of triggers for inserts / update which works great in
Oracle.
For H2, I use org.h2.tools.TriggerAdapter to implement such views /
triggers which also works fine. Except one thing - returning GeneratedKeys
from the instead of insert trigger. I did not find a way how to make it
work using public API. I hacked it temporarily using reflection (it is just
for junit) but I consider it a temporar workaround.
I spent a lot of time debugging the H2 code and it seems to me, that
problem can be here:
org.h2.schema.TriggerObject
line: 256 - 269:
try {
session.setAutoCommit(false);
triggerCallback.fire(c2, oldList, newList);
if (newListBackup != null) {
for (int i = 0; i < newList.length; i++) {
Object o = newList[i];
if (o != newListBackup[i]) {
Value v = DataType.convertToValue(session, o, Value.
UNKNOWN);
session.getGeneratedKeys().add(table.getColumn(i));
newRow.setValue(i, v);
}
}
}
} catch (Exception e) {
Obviously, you somehow handle the values replaced by trigger if they
changed (I changed them in my trigger of course saving the original
Object[] newRow from org.h2.api.Trigger and the ResultSet from the
TriggerAdapter)
Problem is, that GeneratedKeys are cleared inside the instead of trigger
after I perform my insert instead of the original one. and there is no
Table. So although this code sets Value to new Row and tableColumn, but the
table is not initialized.
Also, the consequent code does not call GeneratedKeys.confirmRow(...) so
when GeneratedKeys.getKeys(session) is invoked, it returns *return new
LocalResult()*; instead of GeneratedKeys by the trigger.
I do not know how exactly to fix this or if there is something I missed and
I can simply fix it in my code but to me it seems that maybe in
TriggerObject, when you find first changed attribute, you should
1. clear the session.getGeneratedKeys() and then
2. initialize the session.getGeneratedKeys().initialize(table).
3. Also confirmRow should be called somewhere at appropriate place (it was
not called)
Another way may be to pass original Table and Row to the trigger so the
trigger programmer can prepare the keys maunally.
Could you please advise?
Thanks
Jaroslav
--
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 post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.