These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]

Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate Listener not working when changes made through DB
PostPosted: Sat Sep 03, 2016 2:45 am 

Joined: Sat Sep 03, 2016 2:32 am
Posts: 3
I am new to Hibernate. I have found out that we can have event listener in Hibernate which will notify us of changes made on DB. What I am interested in is Will the Java code be called if I am not doing any DB changes through Java application but only through DB. Here is what I have coded::

Hibernate CFG file and Corresponding HBM file
  <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
  <property name="hibernate.connection.url"></property>
  <property name="hibernate.connection.username"></property>
  <property name="hibernate.connection.password"></property>
  <property name="hibernate.connection.pool_size"></property>
  <property name="show_sql">true</property>
  <property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
  <!-- Mapping files -->
  <mapping resource="hbm/ps.hbm.xml"/>

    <listener type="post-insert" class="com.Dummy"/>

<class name="com.Bean" table="Demo">
    <id name="id" type="string" column="ID" />
    <property name="name" type="string" column="NAME"></property>

Listener Class implementing PostInsertListener::
import org.hibernate.Session;
import org.hibernate.event.PostInsertEvent;
import org.hibernate.event.PostInsertEventListener;

public class Dummy implements PostInsertEventListener
    private static final long serialVersionUID = 1L;

    public void onPostInsert(PostInsertEvent event) {
        // TODO Auto-generated method stub
        System.out.println(event.getEntity() instanceof Bean);
        System.out.println("Inside onPostInsert");
        Session session = event.getSession();

Main Class::
public class Run {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Configuration cfg = new Configuration().configure();
        SessionFactory factory = cfg.buildSessionFactory();
        Session session =     factory.openSession();
        Transaction tx = session.beginTransaction();
        Bean bean= new Bean();
        System.out.println("Before Insert");
        System.out.println("After Save");
        System.out.println("After Commit");
        catch(Exception e){

The code works magic when i run the main class Inserts into DB as it should call the onPostInsert method as expected but if i run the application and make changes from DB ie insert a column the Console doesnt print anything. Is there something I am missing?? I am using SQL server...are there any compatibility issue with SQL server?? It is a plain java application nothing much

 Post subject: Re: Hibernate Listener not working when changes made through DB
PostPosted: Sat Sep 03, 2016 5:27 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Hibernate listeners work only when you do changes through the Hibernate API. If you need to intercept any DB statement (from a console or from other apps), you need to use database triggers.

Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.