假设以下代码片段使用@PrePersist和@PreUpdate注释以及Joined-type继承: @Entity@Inheritance(strategy=InheritanceType.JOINED)public abstract class A { ... @PrePersist private void prePersist() { ... } @PreUpdate private void p
@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() { ... } }
问题:我们可以依赖回调方法的任何执行顺序吗?
例如,当持久化A类和B类时,B中的prePersist方法在A中的prePersist方法之前执行或反之?
我们可以假设B中的prePersist将在A级持久化之前执行吗?
是.首先将执行超类回调.引发事件时,将按以下顺序执行侦听器:
@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