java - 未在父级上调用 Jpa prepersist 回调

标签 java jpa eclipselink

我的代码

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class SiteMessage implements Identifiable{
    @PrePersist
    public void onCreate1(){
        System.out.println("Executed onCreate1");
    }
}

@Entity
@Table(name = "feedback")
public class Feedback extends SiteMessage {
    @PrePersist
    public void onCreate2(){
        System.out.println("Executed onCreate2");
    }
}

当我保存反馈实体时,我希望我会看到:Executed onCreate1 和 Executed onCreate2,但我只看到 Executed onCreate2

我使用的是eclipselink v2.5.2

最佳答案

《掌握 Java 持久性 API》一书指出以下内容:

Inheriting Callback Methods

Callback methods may occur on any entity or mapped superclass, be it abstract or concrete. The rule is fairly simple. It is that every callback method for a given event type will be invoked in the order according to its place in the hierarchy, most general classes first. Thus, if in our Employee hierarchy that we saw in Figure 10-10 the Employee class contains a PrePersist callback method named checkName(), and FullTimeEmployee also contains a PrePersist callback method named verifyPension(), when the PrePersist event occurs, the checkName() method will get invoked, followed by the verifyPension() method.

因此,如果您原始代码中的其他所有内容都是正确的,您应该会看到 onCreateOne() 和 onCreateTwo() 都被调用并按此顺序调用。

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class SiteMessage implements Identifiable{
    @PrePersist
    public void onCreateOne(){
        System.out.println("Executed onCreate1"); //executes first
    }
}

@Entity
@Table(name = "feedback")
public class Feedback extends SiteMessage {
    @PrePersist
    public void onCreateTwo(){
        System.out.println("Executed onCreate2"); //executes second
    }
}

它继续注意以下内容,因此您应该能够完全按照要求进行设置。

We could also have a method on the CompanyEmployee mapped superclass that we want to apply to all the entities that subclassed it. If we add a PrePersist method named checkVacation() that verifies that the vacation carryover is less than a certain amount, it will be executed after checkName() and before verifyPension(). It gets more interesting if we define a checkVacation() method on the PartTimeEmployee class because part-time employees don’t get as much vacation. Annotating the overridden method with PrePersist would cause the PartTimeEmployee.checkVacation() method to be invoked instead of the one in CompanyEmployee

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class SiteMessage implements Identifiable{
    @PrePersist
    public void onCreate(){
        System.out.println("Executed onCreate1"); //will not execute
    }
}

@Entity
@Table(name = "feedback")
public class Feedback extends SiteMessage {
    @PrePersist
    public void onCreate(){
        System.out.println("Executed onCreate2"); //will execute
    }
}

关于java - 未在父级上调用 Jpa prepersist 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26060940/

相关文章:

java - JPA中使用WHERE查询实体

java - JPA 2 查询在 OR 子句上给出奇怪的行为

java - 如何从字符串中提取特定术语?

java - 使用 JndiObjectFactoryBean 在运行时设置动态模式

java - JPA 查询在 tomcat 中的每次循环迭代中变慢

jpa - 添加 EntityListeners 后 sun.reflect.annotation.TypeNotPresentExceptionProxy 异常

xml - 如何让 EclipseLink JAXB (MOXy) 不在 XML 输出中显示命名空间

java - 使用 Java 8 流 API 的累积和

java - 如何分配带有图像作为标签的 Richfaces 面板栏

java - 如何挂接到自动生成的 Java 方法?