gpt4 book ai didi

java - JPA - 提交后如何调用 Web 服务的方法

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:42 25 4
gpt4 key购买 nike

抱歉,这可能非常简单,但我是 JPA 新手,我不确定在哪里插入需要在新记录提交到数据库后调用的方法的函数调用。我有以下内容:

@POST
@Override
@Consumes({"application/xml", "application/json"})
@Path("...")
@RolesAllowed("...")
public void create(LearningActivity entity){
super.create(entity);
}

上述过程完成后,我需要调用自定义方法myMethod()。我以为我可以在 super.create(entity) 之后立即添加它,但显然直到整个 create() 方法执行后提交过程才完成。我还尝试将 @PostPersist 添加到底层实体,但这仍然是在提交记录之前。

如有任何帮助,我们将不胜感激。

最佳答案

I'm not sure where to insert a function call for a method that needs to be called after a new record is committed to a database

听起来您需要使用事件监听器,有一个名为 PostPersist 的事件,每次将实体提交到数据库时都会执行该事件,您可以使用上面的代码进行配置。

@Entity
public class X{
@PostPersist
public void myMethod(){
//Do anything before the commit of the transaction that save a new entity of type X
}

@PostUpdate
public void myMethod2(){
//This code will run each time you update and save an entity of type X
}
}

PostPersist在提交完成后调用,PrePersist在调用persist方法时执行。

关于java - JPA - 提交后如何调用 Web 服务的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23232774/

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