java – Hibernate PreUpdateEventListener不起作用
发布时间:2020-12-15 02:08:35 所属栏目:Java 来源:网络整理
导读:我正在尝试在我的项目中实现Hibernate的PreUpdateEventListener,但没有任何反应. 我的configuration.xml文件: ?xml version="1.0" encoding="UTF-8"?!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://
我正在尝试在我的项目中实现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("EEEEUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUnn"); JOptionPane.showMessageDialog(null,"MyLoadListener"); } @Override public boolean onPreUpdate(PreUpdateEvent pue) { this.atencao(); return true; } } 结果:控制台没有显示任何内容. 解决方法
也许这不再相关,但您必须添加以下代码:
@PostConstruct protected void init(){ SessionFactoryImpl sF = emf.unwrap(SessionFactoryImpl.class); EventListenerRegistry registry = sF.getServiceRegistry().getService(EventListenerRegistry.class); registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener(this); } 在春天工作 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |