目前与我的工作@PostPersist和@PostUpdate,而在这些触发器我坚持附加实体.问题是,这些触发器是否在同一个交易中,如果不是,是否可以强行执行?
对我而言,它就是这样的.当我查看日志时,事务不存在(它在触发器启动之前提交),这阻止了我(没有REQUIRES_NEW 注入bean的持久方法)保存数据库中的其他实体.
REQUIRED属性完全被忽略,MANDATORY属性不会抛出异常.
可能是JUnit的问题(因为我处于开发阶段并且没有测试完整环境中的行为)?
如果无法在此触发器上扩展事务,如何确保如果在@PostPersist和之前发生回滚@PostUpdate,那么这些操作也将被回滚.
我正在尝试RepositoryEventListener在春季启动应用程序中工作,但我想我做错了什么...
这是侦听器中的代码
@SuppressWarnings("rawtypes")
public class BeforeSaveEventListener extends AbstractRepositoryEventListener {
@Override
public void onBeforeSave(Object customer) {
throw new RuntimeException("++++ BEFORE SAVE EVENT ++++");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的应用程序类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.addListeners(new BeforeSaveEventListener());
springApplication.run(args);
}
}
Run Code Online (Sandbox Code Playgroud)
在保存操作中,我可以看到触发了以下事件:
Current Event is org.springframework.data.rest.core.event.BeforeCreateEvent received!
Current Event is org.springframework.data.rest.core.event.AfterCreateEvent received!
Current Event is org.springframework.web.context.support.ServletRequestHandledEvent received!
Run Code Online (Sandbox Code Playgroud)
因此,没有看到“ BeforeSave”事件……也许是文档中不推荐使用的东西,或者spring-boot机制可能有所不同?