Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify EJB entity property changes in @PreUpdate?

when persisting (updating) an entity you would call EntityManager.persist(entity) passing the complete entity. It is possible to intercept with @PreUpdate.

Does anyone have a recipe how to identify which properties have changed in this interceptor method ? Somehow comparing the old and the new entity ? Even better to implement a generic method instead of comparing field by field for each class.

Thanks for any input !

Sven

like image 916
javadude Avatar asked Jan 13 '11 04:01

javadude


1 Answers

First of all, persist() is for new objects (insert) not update. Any object changed in the persistence context will be automatically updated.

JPA does not provide any standard way to know what changed. So you either need to track changes yourself, or use a JPA provider specific API.

In EclipseLink if you use the EclipseLink DescriptorEventListener preUpdate event instead of the JPA one, you get an ObjectChangeSet attached to the DescriptorEvent which contains the changes.

Another way in JPA, if you are using weaving, is to cast your object to ChangeTracker and call _persistence_getPropertyChangeListener() then getObjectChangeSet().

If you are using TopLink Essentials the descriptor events also apply, but change tracking was not weaved.

like image 179
James Avatar answered Nov 16 '22 19:11

James