EntitiyListener 运行 是否在同一线程中?

Does EntitiyListener run in same Thread?

我有一个注释为

的实体
@EntityListener(MyListener.class)

在 class MyListener 中,我有一个注释为

的方法
@PrePersist 

在这个方法中,我是否有对 Entitiy 进行更改的相同线程?或者它可以是另一个线程?我已经在对实体进行更改的线程上存储了一些信息,我想在侦听器中访问这些信息。 感谢您的帮助!

是的,它总是同一个线程。来自 JPA 规范 (v2.2):

An entity manager must not be shared among multiple concurrently executing threads, as the entity manager and persistence context are not required to be threadsafe.

实体管理器仅在单个线程中使用(或者您有错误),因此实体管理器也必须如此。