Hibernate version: 3.3.0 CR1
Name and version of the database you are using: MS SQL Server 2005
Mapping documents: (Irrelevant bits removed)
Code:
<hibernate-mapping>
<session-factory>
<event type="post-insert">
<listener class="com.x.y.z.HibernateEventListener"/>
</event>
<event type="pre-delete">
<listener class="com.x.y.z.HibernateEventListener"/>
</event>
</session-factory>
</hibernate-mapping>
Description:
I'm using post-insert and pre-delete events to synchronize some external data with one of my mapped entities. I am creating new entities by instantiating them then calling persist(). I am running into problems where the post-insert event is triggered and then, later on in the same transaction if something causes an error and the transaction must be rolled back, the pre-delete event is not triggered. When this happens my data goes all out of sync. I.e.:
1. Instantiate new mapped entity.
2. Call persist().
3. Hibernate generates INSERT statements, post-insert happens - sync external data.
4. Do some other stuff that causes an error.
5. Error causes transaction to be rolled back.
6. Since transaction is rolled back, INSERT statements don't apply, but also pre-delete event is not fired. External data goes out of sync.
It is hard for me to do the external data sync without using hibernate events because:
1. I don't always explicitly delete the mapped entities -- they are frequently deleted by delete-orphan cascades.
2. When stuff fails in #4 above, I don't have enough information conveniently available to know what needs to be done to clean up -- only hibernate knows which entities it's rolling back.
What is the cleanest and most convenient way to do this? Is there some sort of event that is triggered when uncommitted mapped entities are rolled back? Is there some way to configure pre-delete (or even post-delete would be acceptable) to fire when a transaction is rolled back?
Thanks,
Jason