5

I have implemented the pre/post event listeners for Hibernate events with one caveat I haven't been able to overcome. I have a Departments entity that has a many-to-many relationship back to a Centers entity. When assigning a new Center to a Department, I simply invoke:

oDepartment.addCenter(oCenter);
entitySave(oDepartment);

When this happens, I would expect Hibernate to trigger the preUpdate and postUpdate events since I modified the entity. However, it's never triggered unless a column property is also updated. Updating only a relationship property does not seem to trigger the update events.

Looking through the current Hibernate session, I can't seem to find anything that would allow me to set some sort of flag that would trigger these events to fire.

Thoughts on a way I could get these events to fire?

Leigh
  • 28,765
  • 10
  • 55
  • 103
Tristan Lee
  • 1,210
  • 10
  • 17

2 Answers2

3

Hibernate doesn't fire pre/postUpdate events when a collection is changed. Instead it will trigger

  • preCollectionUpdate/preCollectionRemove/preCollectionRecreate
  • postCollectionUpdate/postCollectionRemove/postCollectionRecreate.

This is a known and old bug, you can check HHH-2616 and here.

Either you implement these listeners, or, as a workaround, you can force the postUpdate to be called changing another field on that entity.

0

One way to fire listeners is to add a @javax.persistence.Version field on your entity. More information here: https://stackoverflow.com/a/17073342/12039

Community
  • 1
  • 1
Nis
  • 560
  • 5
  • 18