Hibernate PreUpdateEventListener不起作用

时间:2015-08-05 23:36:01

标签: java hibernate events listener

我试图在我的项目中实现Hibernate的PreUpdateEventListener,但没有任何反应。

我的configuration.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bd_brittos</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.show_sql">true</property>
        <mapping resource="hibernate/mapeamento/Compra.hbm.xml"/>
        <mapping resource="hibernate/mapeamento/CompraHasProduto.hbm.xml"/>
        <mapping resource="hibernate/mapeamento/Entidade.hbm.xml"/>
        <mapping resource="hibernate/mapeamento/Pesagem.hbm.xml"/>
        <mapping resource="hibernate/mapeamento/Produto.hbm.xml"/>
        <mapping resource="hibernate/mapeamento/Tabelapesagem.hbm.xml"/>
        <mapping resource="hibernate/mapeamento/Venda.hbm.xml"/>
        <mapping resource="hibernate/mapeamento/VendaHasProduto.hbm.xml"/>
        <mapping resource="hibernate/mapeamento/Contasreceber.hbm.xml"/>
        <mapping resource="hibernate/mapeamento/CaixadiarioHasContaspagar.hbm.xml"/>
        <mapping resource="hibernate/mapeamento/Contaspagar.hbm.xml"/>
        <mapping resource="hibernate/mapeamento/Caixadiario.hbm.xml"/>
        <mapping resource="hibernate/mapeamento/CaixadiarioHasContasreceber.hbm.xml"/>
        <mapping resource="hibernate/mapeamento/Conf.hbm.xml"/>
        <event type="pre-update">
            <listener class="br.com.areiasbrittos.persistencia.MyHibernateEventListener" />
        </event>    
    </session-factory>
</hibernate-configuration>

My MyHibernateEventListener类:

import javax.swing.JOptionPane;
import org.hibernate.event.spi.PreUpdateEvent;
import org.hibernate.event.spi.PreUpdateEventListener;

/**
 *
 * @author Maycon
 */
public class MyHibernateEventListener implements PreUpdateEventListener   {

    public MyHibernateEventListener() {
        this.atencao();
    }

    public void atencao() {
        System.out.println("EEEEUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU\n\n");
        JOptionPane.showMessageDialog(null, "MyLoadListener");
    }

    @Override
    public boolean onPreUpdate(PreUpdateEvent pue) {
        this.atencao();

        return true;
    }

}

结果:控制台没有显示任何内容。

1 个答案:

答案 0 :(得分:0)

也许这不再相关,但是您必须添加以下代码:

@PostConstruct
    protected void init(){
        SessionFactoryImpl sF = emf.unwrap(SessionFactoryImpl.class);
        EventListenerRegistry registry = sF.getServiceRegistry().getService(EventListenerRegistry.class);

        registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener(this);
    }

在春季工作