首页 > 解决方案 > Hibernate PostInsertEventListener.requiresPostCommitHanding 做什么?

问题描述

在文档 [1] 中它说:

这个监听器是否需要在事务挂钩之后注册?

这是否意味着,如果我们返回,监听器将在事务提交之前运行,如果我们返回false,则在事务提交之后运行true

[1] https://docs.jboss.org/hibernate/orm/5.4/javadocs/org/hibernate/event/spi/PostInsertEventListener.html

标签: javahibernate

解决方案


似乎,当PostInsertEventListener注册为时EventType.POST_INSERT,根本不调用该方法,并且始终在提交事务之前执行侦听器。

但是,如果侦听器注册为EventType.POST_COMMIT_INSERT实际调用的方法,并且如果它返回true,则将提交事务后调用侦听器。如果它返回,则根本不会调用false侦听器。

此外,如果侦听器是类型,则无论事务是否成功,都会调用PostInsertEventListener该方法。onPostInsert如果侦听器是类型的,则只会为成功的事务调用PostCommitInsertEventListener该方法。onPostInsert否则,onPostInsertCommitFailed调用该方法。


推荐阅读