Search code examples
javajakarta-eejpaentitylisteners

Exclude field in JPA Entity Listener


I have an entity class in my Enterprise Java application that has an entity listener attached to it:

@Entity
@EntityListeners(ChangeListener.class)
public class MyEntity {

   @Id
   private long id;

   private String name;

   private Integer result;

   private Boolean dirty;

   ...
}

However, I would like it so that the entity listener got triggered for all fields except the boolean one. Is there any way exclude a field from triggering the entity listener without making it transient?

I'm using Java EE 5 with Hibernate.


Solution

  • There is some kind of mixing of concepts here. EntityListeners are not notified about changes in attribute values - not for single attribute, neither for all attributes.

    For reason they are called lifecycle callbacks. They are triggered by following lifecycle events of entity:

    • persist (pre/post)
    • load (post)
    • update(pre/post)
    • remove (pre/post)

    For each one of them there is matching annotation. So answer is that it is not possible to limit this functionality by type of persistent attributes.