【问题标题】:Hibernate PreUpdateEventListener doesn`t work休眠 PreUpdateEventListener 不起作用
【发布时间】:2015-10-28 22:30:19
【问题描述】:

我正在尝试在我的项目中实现 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>

我的 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;
    }

}

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

【问题讨论】:

    标签: java hibernate events listener


    【解决方案1】:

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

    @PostConstruct
        protected void init(){
            SessionFactoryImpl sF = emf.unwrap(SessionFactoryImpl.class);
            EventListenerRegistry registry = sF.getServiceRegistry().getService(EventListenerRegistry.class);
    
            registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener(this);
        }
    

    在春天工作

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 2013-09-25
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多