Listen to Session Event : Session « Hibernate « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Hibernate » Session 
21. 22. 1. Listen to Session Event

File: Main.java

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.event.AutoFlushEvent;
import org.hibernate.event.DeleteEvent;
import org.hibernate.event.DirtyCheckEvent;
import org.hibernate.event.EvictEvent;
import org.hibernate.event.FlushEntityEvent;
import org.hibernate.event.FlushEvent;
import org.hibernate.event.InitializeCollectionEvent;
import org.hibernate.event.LoadEvent;
import org.hibernate.event.LockEvent;
import org.hibernate.event.MergeEvent;
import org.hibernate.event.PersistEvent;
import org.hibernate.event.PostLoadEvent;
import org.hibernate.event.PreLoadEvent;
import org.hibernate.event.RefreshEvent;
import org.hibernate.event.ReplicateEvent;
import org.hibernate.event.SaveOrUpdateEvent;
import org.hibernate.event.def.DefaultAutoFlushEventListener;
import org.hibernate.event.def.DefaultDeleteEventListener;
import org.hibernate.event.def.DefaultDirtyCheckEventListener;
import org.hibernate.event.def.DefaultEvictEventListener;
import org.hibernate.event.def.DefaultFlushEntityEventListener;
import org.hibernate.event.def.DefaultFlushEventListener;
import org.hibernate.event.def.DefaultInitializeCollectionEventListener;
import org.hibernate.event.def.DefaultLoadEventListener;
import org.hibernate.event.def.DefaultLockEventListener;
import org.hibernate.event.def.DefaultMergeEventListener;
import org.hibernate.event.def.DefaultPersistEventListener;
import org.hibernate.event.def.DefaultPostLoadEventListener;
import org.hibernate.event.def.DefaultPreLoadEventListener;
import org.hibernate.event.def.DefaultRefreshEventListener;
import org.hibernate.event.def.DefaultReplicateEventListener;
import org.hibernate.event.def.DefaultSaveOrUpdateEventListener;
import org.hibernate.mapping.Map;

public class Main {
  public static void main(String[] argsthrows Exception {
    HibernateUtil hibernateUtil = new HibernateUtil();
    hibernateUtil
        .executeSQLCommand("create table Product(id int, name varchar, description varchar, price decimal(6,2), supplierid int)");

    hibernateUtil.executeSQLCommand("create table Supplier (id int , name varchar)");

    hibernateUtil
        .executeSQLCommand("create table Software(id int, name varchar, description varchar, price decimal(6,2), supplierid int, version varchar)");

    hibernateUtil.getConfiguration().setListener("save-update"new MySaveOrUpdateEventListener());

    // Apply this event listener (programmatically)
    hibernateUtil.getConfiguration().setListener("auto-flush"new MyAutoFlushEventListener());
    hibernateUtil.getConfiguration().setListener("delete"new MyDeleteEventListener());
    hibernateUtil.getConfiguration().setListener("dirty-check"new MyDirtyCheckEventListener());
    hibernateUtil.getConfiguration().setListener("evict"new MyEvictEventListener());
    hibernateUtil.getConfiguration().setListener("flush-entity"new MyFlushEntityEventListener());
    hibernateUtil.getConfiguration().setListener("flush"new MyFlushEventListener());
    hibernateUtil.getConfiguration().setListener("load-collection",
        new MyInitializeCollectionEventListener());
    hibernateUtil.getConfiguration().setListener("load"new MyLoadEventListener());
    hibernateUtil.getConfiguration().setListener("lock"new MyLockEventListener());
    hibernateUtil.getConfiguration().setListener("merge"new MyMergeEventListener());
    hibernateUtil.getConfiguration().setListener("persist"new MyPersistEventListener());
    hibernateUtil.getConfiguration().setListener("post-load"new MyPostLoadEventListener());
    hibernateUtil.getConfiguration().setListener("pre-load"new MyPreLoadEventListener());
    hibernateUtil.getConfiguration().setListener("refresh"new MyRefreshEventListener());
    hibernateUtil.getConfiguration().setListener("replicate"new MyReplicateEventListener());
    hibernateUtil.getConfiguration().setListener("save-update"new MySaveOrUpdateEventListener());

    Session session = hibernateUtil.getConfiguration().buildSessionFactory().openSession();

    Supplier superCorp = new Supplier();
    superCorp.setName("Supplier1");
    session.save(superCorp);

    Supplier megaInc = new Supplier();
    megaInc.setName("Supplier2");
    session.save(megaInc);

    Product mouse = new Product("Product1""first product"20.0);
    mouse.setSupplier(superCorp);
    superCorp.getProducts().add(mouse);
    session.flush();
    Product mouse2 = new Product("Product2""second product"22.0);
    mouse2.setSupplier(superCorp);
    superCorp.getProducts().add(mouse2);

    Product keyboard = new Product("Product3""third product"30.0);
    keyboard.setSupplier(megaInc);
    megaInc.getProducts().add(keyboard);

    Software webBrowser = new Software("Web Browser""new browser"75.0"2.0");
    webBrowser.setSupplier(superCorp);
    superCorp.getProducts().add(webBrowser);

    Software email = new Software("Email""email client"49.99"4.1 Edition");
    email.setSupplier(megaInc);
    megaInc.getProducts().add(email);

    session.flush();

    session.close();
    hibernateUtil.checkData("select * from Product");
    hibernateUtil.checkData("select * from Software");
    hibernateUtil.checkData("select * from Supplier");
  }
}

class MySaveOrUpdateEventListener extends DefaultSaveOrUpdateEventListener {

  public void onSaveOrUpdate(SaveOrUpdateEvent event) {
    System.out.println(event.getEntity());
    super.onSaveOrUpdate(event);
  }
}

class MyAutoFlushEventListener extends DefaultAutoFlushEventListener {
  public void onAutoFlush(AutoFlushEvent eventthrows HibernateException {
    System.out.println("AutoFlushEventListener.onAutoFlush(AutoFlushEvent)");
    super.onAutoFlush(event);
  }
}

class MyDeleteEventListener extends DefaultDeleteEventListener {
  public void onDelete(DeleteEvent eventthrows HibernateException {
    System.out.println("DeleteEventListener.onDelete(DeleteEvent)");
    super.onDelete(event);
  }
}

class MyDirtyCheckEventListener extends DefaultDirtyCheckEventListener {
  public void onDirtyCheck(DirtyCheckEvent event) {
    System.out.println("DirtyCheckEventListener.onDirtyCheck(DirtyCheckEvent)");
    super.onDirtyCheck(event);
  }
}

class MyEvictEventListener extends DefaultEvictEventListener {
  public void onEvict(EvictEvent eventthrows HibernateException {
    System.out.println("EvictEventListener.onEvict(EvictEvent)");
    super.onEvict(event);
  }
}

class MyFlushEntityEventListener extends DefaultFlushEntityEventListener {
  public void onFlushEntity(FlushEntityEvent eventthrows HibernateException {
    System.out.println("FlushEntityEventListener.onFlushEntity(FlushEntityEvent)");
    super.onFlushEntity(event);
  }
}

class MyFlushEventListener extends DefaultFlushEventListener {
  public void onFlush(FlushEvent eventthrows HibernateException {
    System.out.println("FlushEventListener.onFlush(FlushEvent)");
    super.onFlush(event);
  }
}

class MyInitializeCollectionEventListener extends DefaultInitializeCollectionEventListener {
  public void onInitializeCollection(InitializeCollectionEvent eventthrows HibernateException {
    System.out
        .println("InitializeCollectionEventListener.onInitializeCollection(InitializeCollectionEvent)");
    super.onInitializeCollection(event);
  }
}

class MyLoadEventListener extends DefaultLoadEventListener {
  public void onLoad(LoadEvent event, LoadType typethrows HibernateException {
    System.out.println("LoadEventListener.onLoad(LoadEvent,LoadType)");
    super.onLoad(event, type);
  }
}

class MyLockEventListener extends DefaultLockEventListener {
  public void onLock(LockEvent eventthrows HibernateException {
    System.out.println("LockEventListener.onLock(LockEvent)");
    super.onLock(event);
  }
}

class MyMergeEventListener extends DefaultMergeEventListener {
  public void onMerge(MergeEvent event, Map copiedAlready) {
    System.out.println("MergeEventListener.onMerge(MergeEvent,Map)");
    // super.onMerge(event, copiedAlready);
  }

  public void onMerge(MergeEvent eventthrows HibernateException {
    System.out.println("MergeEventListener.onMerge(MergeEvent)");
    super.onMerge(event);
  }
}

class MyPersistEventListener extends DefaultPersistEventListener {
  public void onPersist(PersistEvent event, Map createdAlreadythrows HibernateException {
    System.out.println("PersistEventListener.onPersist(PersistEvent,Map)");
    // super.onPersist(event, createdAlready);
  }

  public void onPersist(PersistEvent eventthrows HibernateException {
    System.out.println("PersistEventListener.onPersist(PersistEvent)");
    super.onPersist(event);
  }
}

class MyPostLoadEventListener extends DefaultPostLoadEventListener {
  public void onPostLoad(PostLoadEvent event) {
    System.out.println("PostLoadEventListener.onPostLoad(onPostLoad)");
    super.onPostLoad(event);
  }
}

class MyPreLoadEventListener extends DefaultPreLoadEventListener {
  public void onPreLoad(PreLoadEvent event) {
    System.out.println("PreLoadEventListener.onPreLoad(PreLoadEvent)");
    super.onPreLoad(event);
  }
}

class MyRefreshEventListener extends DefaultRefreshEventListener {
  public void onRefresh(RefreshEvent eventthrows HibernateException {
    System.out.println("RefreshEventListener.onRefresh(RefreshEvent)");
    super.onRefresh(event);
  }
}

class MyReplicateEventListener extends DefaultReplicateEventListener {
  public void onReplicate(ReplicateEvent eventthrows HibernateException {
    System.out.println("ReplicateEventListener.onReplicate(ReplicateEvent)");
    super.onReplicate(event);
  }
}

class NoisySaveOrUpdateEventListener extends DefaultSaveOrUpdateEventListener {
  public void onSaveOrUpdate(SaveOrUpdateEvent eventthrows HibernateException {
    System.out.println("SaveOrUpdateEventListener.onSaveOrUpdate(SaveOrUpdateEvent)");
    super.onSaveOrUpdate(event);
  }
}
  Download:  HibernateSessionEvent.zip( 4,866 k)
21. 22. Session
21. 22. 1. Listen to Session Event
21. 22. 2. Save and update event
21. 22. 3. Let Hibernate to decide whether to save or update
21. 22. 4. Create Query from Session
21. 22. 5. Create Criteria from Session
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.