Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /* HibernateSessionFactoryListener.java
 
 	Purpose:
 		
 	Description:
 		
 	History:
 		Tue Sep  5 10:11:55     2006, Created by henrichen
 
Copyright (C) 2006 Potix Corporation. All Rights Reserved.
{{IS_RIGHT
	This program is distributed under LGPL Version 2.1 in the hope that
	it will be useful, but WITHOUT ANY WARRANTY.
}}IS_RIGHT
*/
package org.zkoss.zkplus.hibernate;

Listener to init and cleanup the hibernate session factory automatically. This listener is used with OpenSessionInViewListener and HibernateUtil, or it will not work.

In WEB-INF/zk.xml, add following lines:


  <listener>
  <description>Hibernate SessionFactory Lifecycle</description>
  <listener-class>org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener</listener-class>
 </listener>
 

Since ZK 3.0.1, if your hibernate configuration file name is not the default "hibernate.cfg.xml", you can specify it in WEB-INF/zk.xml. Just add following lines:


 <preference>
 <name>HibernateUtil.config</name>
 <value>YOUR-HIBERNATE-CONFIG-FILENAME</value>
 

Sometimes, when using ZK with other backend framework, the Hibernate's SessionFactory might has to be created earlier. Therefore, since ZK 3.0.1, we have implemented this listener to be also a javax.servlet.ServletContextListener that you can choose to specify it in web.xml instead and make it initialized right when your applcation is deployed.

In WEB-INF/web.xml, add following lines:


 <context-param>
  <param-name>HibernateUtil.config</param-name>
  <param-value>YOUR-HIBERNATE-CONFIG-FILENAME</param-value>
 </context-param>
 <listener>
  <listener-class>org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener</listener-class>
 </listener>
 

Applicable to Hibernate version 3.2.ga or later

Deprecated:
As of release 6.0.2, please use the official Hibernate's method instead.
Author(s):
henrichen
	//WebAppInit//
    public void init(WebApp app)  {
        HibernateUtil.initSessionFactory(app);
    }
	//WebAppCleanup//
    public void cleanup(WebApp wapp) {
        HibernateUtil.cleanupSessionFactory();
    }
	//ServletContextListener//

Since:
3.0.1
	public void contextDestroyed(ServletContextEvent sce) {
        HibernateUtil.cleanupSessionFactory();
    }

Since:
3.0.1
		final ServletContext ctx = sce.getServletContext();
        HibernateUtil.initSessionFactory(resource);
    }
New to GrepCode? Check out our FAQ X