Search code examples
javaspring-data-jpaentitylisteners

Can I define the Entity Listeners in the Same Entity Class?


E.g., will the below code run properly? Or do I have to define a separate Entity Listener for each and every entity class?

@Entity
@EntityListeners(value = Abc.class)
public class Abc{
    ...
    @PreUpdate
    public void doPreUpdate(){
    //do something
    }
    ...
}

Solution

  • You don't even need the @EntityListeners annotation. It will work just fine with just the @PreUpdate annotated method.

    From javadoc for @PreUpdate:

    This annotation may be applied to methods of an entity class, a mapped superclass, or a callback listener class.