title: Hibernate event listeners for JPA callbacks
body: <p>How can I enable the Hibernate event listeners, that handle JPA callbacks?</p>
  
  <p>Currently I am using using Hibernate 4 with SessionFactory configuration, but JPA callbacks are not running properly, when I persist an object.</p>
  
  <p>Any suggestion are most welcome.</p>
  
  <h3>Source code</h3>
  
  <p><strong>Temp Entity class:</strong></p>
  
  <pre><code>package com.esp.entity;
  
  import javax.persistence.Entity;
  import javax.persistence.EntityListeners;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.PostLoad;
  import javax.persistence.Table;
  
  import com.esp.aaa.TempVal;
  
  @Entity
  @EntityListeners(value=TempVal.class)
  @Table(name="TEMP")
  public class Temp {
      private int id;
      private String name;
      private String email;
      private int roll;
  
      @Id @GeneratedValue
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public String getEmail() {
          return email;
      }
      public void setEmail(String email) {
          this.email = email;
      }
      public int getRoll() {
          return roll;
      }
      public void setRoll(int roll) {
          this.roll = roll;
      }
      @PostLoad
      public void load(){
          System.out.println("post load called here");
      }
  }
  </code></pre>
  
  <p><strong>TempVal class:</strong></p>
  
  <pre><code>package com.esp.aaa;
  
  import javax.persistence.PrePersist;
  
  public class TempVal {
      @PrePersist
      public void validate(Object temp){
          System.out.println("Object will persist now");
      }
  }
  </code></pre>
  
  <p><strong>MainClass class:</strong></p>
  
  <pre><code>package com.esp.aaa;
  
  import org.hibernate.Session;
  import com.esp.entity.Temp;
  import com.esp.utility.HibernateUtils;
  
  public class MainClass {
      public static void main(String args[]) {
          HibernateUtils.createSessionFactory();
          Session session=HibernateUtils.getSessionFactory().getCurrentSession();
          session.beginTransaction();
  
          Temp temp=new Temp();
  
          temp.setEmail("abc@gmail.com");
          temp.setName("Lucky");
          temp.setRoll(1112);
  
          session.save(temp);
          System.out.println("Object persist successfully");
  
          session.getTransaction().commit();
          HibernateUtils.shutdown();
      }
  }
  </code></pre>
  
  <p><strong>The Hibernate configuration:</strong></p>
  
  <pre><code>&lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/demo&lt;/property&gt;
          &lt;property name="connection.username"&gt;root&lt;/property&gt;
          &lt;property name="connection.password"&gt;root&lt;/property&gt;
  
          &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
          &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;
          &lt;property name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt;
  
          &lt;mapping class="com.esp.entity.Temp"/&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <h3>Program output</h3>
  
  <p>The program output is the following:</p>
  
  <pre><code>Hibernate: insert into TEMP (email, name, roll) values (?, ?, ?)
  Object persist successfully
  </code></pre>
  
  <p>The expected output would be:</p>
  
  <pre><code>Object will persist now
  Hibernate: insert into TEMP (email, name, roll) values (?, ?, ?)
  Object persist successfully
  </code></pre>