Jpa prepersist回调未调用父母 [英] Jpa prepersist callback not called on parent

查看:233
本文介绍了Jpa prepersist回调未调用父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码

@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");
    }
}

当我保存反馈实体时,我希望我会看到:执行onCreate1并执行onCreate2,但我见过只执行onCreate2

When i save Feedback entity i expect that i will see: Executed onCreate1 and Executed onCreate2, but i've seen only Executed onCreate2

我使用eclipselink v2.5.2

I use eclipselink v2.5.2

推荐答案

本书掌握Java持久性API需要注意以下内容:

The Book Mastering the Java persistence API notes the following:


继承回调方法

Inheriting Callback Methods

回调方法可能出现在任何实体或映射的超类上,无论是
abstract还是具体。规则很简单。 对于给定事件类型的每个
回调方法将根据其在层次结构中的位置以
的顺序调用,大多数通用类首先。

因此,如果在我们在图10-10中看到的Employee层次结构中,
Employee类包含名为
checkName()的PrePersist回调方法,而FullTimeEmployee还包含名为verifyPension的PrePersist回调
方法( ),当PrePersist事件发生时,将调用
checkName()方法,然后调用verifyPension()
方法。

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()都按此顺序调用。

Therefore if everything else in your original code was correct you should expect to see both onCreateOne() and onCreateTwo() called and in that order.

@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
    }
}

继续注意以下内容所以你应该能够完全按照要求进行设置。

It goes on to note the following so you should be able to set things up exactly as required.


我们也可以在CompanyEmployee映射的超类
上有一个方法我们想要应用于子类化的所有实体。如果我们
添加一个名为checkVacation()的PrePersist方法来验证
假期结转是否小于一定金额,它将在checkName()之后和verifyPension()之前执行
。如果我们在PartTimeEmployee类
上定义一个checkVacation()方法,它会变得更有趣
,因为兼职员工没有那么多假期。 使用PrePersist注释
重写方法会导致调用
PartTimeEmployee.checkVacation()方法,而不是在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
    }
}

这篇关于Jpa prepersist回调未调用父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆