gpt4 book ai didi

java - 为什么我的服务在从组件调用它们时不写入数据库?

转载 作者:行者123 更新时间:2023-12-02 03:33:36 25 4
gpt4 key购买 nike

我正在为 Camunda 流程创建委托(delegate)表达式,工作流程工作完美,但是当它执行委托(delegate)时,我的服务会创建对象,但不会将它们写入数据库。这是代码:

@Component
public class CreateNewROAction implements JavaDelegate {

private final ROactionService rOactionService;
private final RiskService riskService;
private Logger logger = LoggerFactory.getLogger(CreateNewROAction.class);
public CreateNewROAction(ROactionService rOactionService, RiskService riskService) {
this.rOactionService = rOactionService;
this.riskService = riskService;
}

@Override
public void execute(DelegateExecution delegateExecution) throws Exception {

String processInstanceId = delegateExecution.getProcessInstanceId();
String riskId = delegateExecution.getVariable("riskId").toString();
Risk risk = riskService.findById(Long.parseLong(riskId));
ROaction rOaction = new ROaction();
rOaction.setProcessInstanceId(processInstanceId);
rOaction = rOactionService.save(rOaction);
logger.info("roAction object: " + rOaction.toString());
risk.setAction(rOaction);
risk = riskService.update(risk, risk.getId());
logger.info("risk object: " + risk.toString());
delegateExecution.setVariable("id", rOaction.getId());

}
}

记录器显示了我预期的结果:

roAction object: ROaction(id=4, description=null, status=null, responsible=null, deadline=null, AchievementDate=null, effectivenessEvaluationDate=null, effectivenessEvaluation=null, processInstanceId=819, createdAt=Wed Jul 03 15:08:19 CET 2019, updatedAt=Wed Jul 03 15:08:19 CET 2019)
risk object: Risk(id=4, description=null, probability=0, impact=0, cause=null, consequence=null, createdAt=2019-07-03 15:07:04.975, updatedAt=2019-07-03 15:07:04.975, processes=[], action=ROaction(id=4, description=null, status=null, responsible=null, deadline=null, AchievementDate=null, effectivenessEvaluationDate=null, effectivenessEvaluation=null, processInstanceId=819, createdAt=Wed Jul 03 15:08:19 CET 2019, updatedAt=Wed Jul 03 15:08:19 CET 2019), processInstanceId=801)

但是数据库是空的。
备注:
1. 我必须将实体的 id 更改为 @GenerateValue(strategy = GenerationType.SEQUENCE) 因为在此之前服务从 save() 函数返回 null Id
2. Controller 的服务运行良好

最佳答案

解决办法是不直接调用服务方法,而是通过@Transactional注解的私有(private)方法来调用。原因是Hibernate在第一次调用时打开与数据库的连接,然后关闭它。该组件只有一个与数据库的连接,因此在第一次调用后无法访问它。 @Transactional 为该方法打开与数据库的新连接。

关于java - 为什么我的服务在从组件调用它们时不写入数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56872375/

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