本文介绍了@PostPersist不叫... @PrePersist是...为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用@PrePersist的JPA @Entity类,现在已经有一段时间了。今天,我想添加一些功能,我需要该实体的ID。该ID在数据库中由HIBERNATE_SEQUENCE持久化期间生成。它通常在 .persist(entity)之后设置

I have a JPA @Entity class which uses a @PrePersist for quite a while now. Today I wanted to add some functionality where I need the ID of that entity. This ID is generated during persist by a HIBERNATE_SEQUENCE in the database. It is usually set after em.persist(entity).

由于某些未知原因, @PrePersist 方法触发......而 @PostPersist 完全不会触发:

For some unknown reason the @PrePersist method is triggered... while @PostPersist simply never fires:

@Entity
public class MyEntity {

    @PrePersist
    protected void onCreate() {
        System.out.println("ExtendedEntity.onCreate()");
    }

    @PostPersist
    protected void afterCreate() {
        System.out.println("ExtendedEntity.afterCreate()");
    }
}

我正在使用JBoss v4.2环境Java v7 +,Hibernate v3.3.1.GA和Seam v2.2.2.Final ...

I'm using a JBoss v4.2 environment with Java v7+, Hibernate v3.3.1.GA and Seam v2.2.2.Final...

@PostPersist是否有任何隐藏需求触发?

推荐答案

对于其他人... Hibernate事件侦听器似乎会干扰JPA持久性事件...删除以下内容后来自 persistence.xml 的行会触发@PostPersist回调。

For everybody else... Hibernate event listeners seem to interfere with JPA persistence events... after removing the following lines from my persistence.xml the @PostPersist callback is triggered.

<property name="hibernate.ejb.event.pre-insert"  value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.pre-update"  value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.pre-delete"  value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-insert" value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-update" value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-delete" value="my.hibernate.events.listeners.Listener" />

我们甚至不再使用它们......它们从未完全被禁用。

We don't even use these anymore... they have just never been disabled completely.

这篇关于@PostPersist不叫... @PrePersist是...为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:45