I have an entity which is persisted in an Oracle schema. On DB insert, a trigger generates an entity ID by using a DB sequence. Using the trigger is mandatory since it also updates another table beside the ID generating sequence (kind of log table) which is important for legacy components.
How to configure the Hibernate id generator in my entity mapping?
Looking around in similar Stackoverflow questions, I found some solutions which do not fit in my case:
- Using the sequence directly:
<generator class="sequence">...</generator>
. This is not possible because the rest of trigger code would not be executed - Using
<generator class="select">...</generator>
which means that after inserting, Hibernate selects the entity using another unique property (according to Hibernate-3.3 manual 5.1.4.6. ). This is also not possible here because there is no other unique column and using a set of properties is not supported. - Using
<generator class="assigned">...</generator>
and setting a fake ID before callingsave()
. This id would be ignored by the DB trigger and the resulting DB row would have the correct ID. However, my Java instance wouldn't have and this solution appeals ugly since it makes assumptions on the implementation of the trigger.
Is there a good solution for this problem?