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
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.
精彩评论