Java – hibernate preupdateeventlistener does not work
•
Java
I am trying to implement hibernate's preupdateeventlistener in my project, but there is no response
My configuration XML file:
<?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 class:
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; } }
Result: nothing is displayed on the console
Solution
This may no longer be relevant, but you must add the following code:
@postconstruct protected void init(){ SessionFactoryImpl sF = emf.unwrap(SessionFactoryImpl.class); EventListenerRegistry registry = sF.getServiceRegistry().getService(EventListenerRegistry.class); registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener(this); }
Work in spring
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码