Search code examples
jpacallbackejb

notifying entity call back methods after bulk query in jpa


How does jpa entity call back method work? are they only notified when doing single jpa operations like em.merge() and em.create() ? or they will be notified if i use em.createQuery().executeUpdate()? (also for namedQueries)

Can i use them instead of real database triggers?


Solution

  • I did not find a direct statement, but from the quote below (JPA 2.1 spec, Chapter 4.10 Bulk Update and Delete Operations), I understand that in a BULK update or delete, the listeners are not called, because the persistence context won't see the changed entities:

    [In a bulk update or bulk delete] The persistence context is not synchronized with the result of the bulk update or delete.

    So the answer is: only in single JPA operations will the listeners be changed.

    A listener is not the same as a database trigger: a constraint of JPA listeners is that you should not change relationships or other entities inside them (although some JPA providers may support this).