gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:13:16 25 4
gpt4 key购买 nike

我的代码

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com