我正在为Camunda流程创建委托表达式,工作流程完美运行,但是当执行委托时,我的服务会创建对象,并且不会将其写入数据库。 这是代码:
@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更改为@GeneratedValue(strategy = GenerationType.SEQUENCE),因为在此之前,该服务从save()函数返回空ID
2.控制器提供的服务正常运行
答案 0 :(得分:0)
解决方案是不直接调用服务方法,而是通过@Transactional注释的私有方法来调用它们。原因是Hibernate在第一个调用中打开与数据库的连接,然后关闭它。该组件与数据库只有一个连接,因此在第一次调用后无法访问它。 @Transactional为该方法打开与数据库的新连接。