简体   繁体   English

Hiberante Listener:EventType.POST_INSERT 和 EventType.POST_COMMIT_INSERT 的区别

[英]Hiberante Listener: difference between EventType.POST_INSERT and EventType.POST_COMMIT_INSERT

Request要求

I need to excute a piece of code after every insert in some tables.我需要在某些表中每次插入后执行一段代码。

I don't want to edit every part of the application looking for all places where the insertion is executed, I would like to use the Hibernate Listener.我不想编辑应用程序的每个部分来查找执行插入的所有位置,我想使用 Hibernate 侦听器。

Question问题

I don't understand the difference between the EventType s POST_INSERT and POST_COMMIT_INSERT .我不明白EventTypePOST_INSERTPOST_COMMIT_INSERT之间的区别。 Both refer to the listener PostInsertEventListener , but I would like to know which of these I should use, and what are the differences.两者都指的是侦听PostInsertEventListener ,但我想知道我应该使用其中的哪一个,有什么区别。 Both seems will be excecuted after the record is written in the database, maybe one of these is executed only on some special case?两者似乎都会在记录写入数据库后执行,也许其中一个只在某些特殊情况下执行?

Other details其他详情

I can't (and I don't want to) use a SQL trigger on the tables.我不能(也不想)在表格上使用 SQL 触发器。

You would be better off using a org.hibernate.Interceptor or a JPA @PostPersist listener.您最好使用org.hibernate.Interceptor或 JPA @PostPersist侦听器。 The event listener infrastructure is designed for much more sophisticated usecases than yours.事件侦听器基础结构专为比您的用例复杂得多的用例而设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM