Event: Your Own Save Or Update Event Listener : Event « Hibernate « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Hibernate » Event 




Event: Your Own Save Or Update Event Listener

/////////////////////////////////////////////////////////////////////////
import java.io.Serializable;
import java.util.*;
import java.sql.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.criterion.*;
import org.hibernate.event.*;
import org.hibernate.event.def.*;

public class Main {
   public static void main(String[] argsthrows Exception {
      HibernateUtil.setup("create table User ( id int, username VARCHAR);");    
      Configuration config = new Configuration();
      
      config.setListener("save-update"new UseringSaveOrUpdateEventListener());

      SessionFactory factory = config.configure().buildSessionFactory();
      Session session = factory.openSession();
      
      Transaction tx = session.beginTransaction();

      session.saveOrUpdate(new User("R1"));
      session.saveOrUpdate(new User("R2"));      
      
      tx.commit();

      session.close();
      
   }
}
class UseringSaveOrUpdateEventListener extends DefaultSaveOrUpdateEventListener
{
   public Serializable onSaveOrUpdate(SaveOrUpdateEvent event)
         throws HibernateException {
      ifevent.getObject() instanceof User ) {
         User user = (User)event.getObject();
         System.out.println("Preparing to save or update user " + user.getUsername());

         ifuser.getUsername().equalsIgnoreCase("R1")) {
            System.out.println("Conventional user not recorded.");
            
            // Here we prevent the invocation
            // of saveOrUpdate on the Session from having 
            // any effect on the database!
            return null;
         }
      }
      
      return super.onSaveOrUpdate(event);
   }
}




/////////////////////////////////////////////////////////////////////////

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="User">
    <id name="id" type="int">
      <generator class="increment"/>
    </id>
    <property name="username" type="string" length="32"/>
  </class>
</hibernate-mapping>



/////////////////////////////////////////////////////////////////////////

public class User
{
    private int id;
    private String username;
    
    public User(){
    }
    
    public User(String u){
        username = u;
    }
    
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id = id;
    }
    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username)
    {
        this.username = username;
    }
}


           
       














HibernateEventYourOwnSaveOrUpdateEventListener.zip( 4,582 k)
Related examples in the same category
1.Event For Cancel Query
2.Event For Criteria List
3.Event For Flush
4.Event For Get Identifier
5.Event For Get Method
6.Event: Load Method
7.Event: Persist
8.Event: Unique Result
9.Event: On Commit
10.Event: On Delete
11.Event: On Dirty Check Event
12.Event: On Evict
13.Event: On Flush Event
14.Event: On Lock
15.Event: On Merge
16.Event: On PreLoad and On PostLoad
17.Event: On Refresh
18.Event: On Replicate
19.Event: On Save and On Update
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.