JPA @PrePersist and @PreUpdate when using inheritance

Assume the following code snippet that uses @PrePersist and @PreUpdate annotations and inherited inheritance:

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class A {
    ...

    @PrePersist
    private void prePersist() {
        ...
    }

    @PreUpdate
    private void preUpdate() {
        ...
    }
}

@Entity
@DiscriminatorValue("B")
public class B extends A {
    ...

    @PrePersist
    private void prePersist() {
        ...
    }

    @PreUpdate
    private void preUpdate() {
        ...
    }
}

Question: Can we rely on any order of execution of callback methods?

For example, if persisting classes A and B use the prePersist method in B before the prePersist method in or vice versa?

Is it possible to assume that prePersist in B will be executed before class A is saved?

+4
source share
1 answer

Yes. The superclass callbacks will be executed first.

When an event is raised, listeners are executed in the following order:

@EntityListeners for a given entity or superclass in the array order

Entity listeners for the superclasses (highest first)

Entity Listeners for the entity

Callbacks of the superclasses (highest first)

Callbacks of the entity

: " "

https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/listeners.html

+5

All Articles