hibernate auditions generating same rev id in for multiple insert in one table

1.1k Views Asked by At

I am using hibernate auditions with hibernate version 3.5. while it is working fine when I try to insert single record in one table in one transation but the problem is when a "BATCH" runs and it inserts multiple records in one table in single transation then single "rev id" is being generated for audit tables which causes "integrityconstraintsviolation".

As it is the normal behavior of hibernate that all the insert queries are fired at the end of tansaction(When it flushes) but at this time only one query is being fired for "rev id" generation.

 select hibernate_sequence.nextval from dual;

Kindly tell me whether is it a bug in audit or i am missing something ?? Thanks in advance

2

There are 2 best solutions below

5
On

A revid always span modifications in multiple tables!

Yes, the inserts are called at the end of the transaction, but: If you use optimistic transaction isolation, a transaction can read the uncommitted state of another transactions that are currently active but not yet committed.

If you become a integrityconstraintsviolation the column rev_id is unfortunatelly configured "unique". This is a wrong database schema! Correct the schema by removing the Uniqueness of the column rev_id!

The rev_id-Column in the global-hibernate-relationtable must be unique because it is a Primary-Key! (the global-hibernate-relationtable usually contains also a timestamp).

0
On

Not sure if still you have this issue :). In my case I had same problem, and solution was to setting on audit table composite primary key on "rev" and "id" columns. Below example Liquibase script:

<addPrimaryKey
    columnNames="id, rev"
    constraintName="t_order_aud_pk"
    tableName="t_order_aud"/>