I did some research but was not able to find what is a differences between
JPA @EntityListeners , @PrePersist
and
Spring @RepositoryEventHandler, @HandleBeforeSave
Thanks.
@HandleBeforeSave
only works when an Entity is saved through a Spring Data repository. @PrePersist
will be trigger if you use EntityManager::persist()
and if you use JPARepository::save()
, since it calls persist.
The nice thing with @RepositoryEventHandler
+@HandleBeforeSave
is that your @HandleBeforeSave
method is defined inside a spring bean, so you can interact with other spring beans. @EntityListeners
and @PrePersist
can only access the state of the current Entity when operating in a J2SE environment. In J2EE you can @Inject
beans into a @EntityListeners
because the JPA subsystem and CDI are both managed by the same Container.
Actually after more searching I found this answer stackoverflow.com/a/31155291/1780517
It seems that there is also one VERY BIG different, @HandleBeforeSave called on Controller POST method and not on repository save. So @RepositoryEventHandler should be used only if you want handle events from controller (PUT, POST, GET with@HandleBeforeSave, @HandleBeforeCreate ..) and @EntityListeners should be used for repository method save,delete, update with @PreUpdate , @PreRemove and so on..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With