Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  		Mon Sep  4 17:18:21     2006, Created by henrichen
 Copyright (C) 2006 Potix Corporation. All Rights Reserved.
 package org.zkoss.zkplus.hibernate;
 import  org.hibernate.Session;
 import  org.hibernate.SessionFactory;
 import  org.hibernate.ConnectionReleaseMode;
 import  org.hibernate.HibernateException;
 import  org.hibernate.cfg.Configuration;
 import  org.hibernate.cfg.AnnotationConfiguration;

Utitlity to access Hibernate Session. This implemenation works with the Hibernate's thread session context (version 3.1+). That is, you have to specified hibernate's configuration file "hibernate.cfg.xml" to as follows:

  <property name="current_session_context_class">thread</property>


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:


Also notice that the zkplus.jar must be put under application's WEB-INF/lib because the SessionFactory is stored as a class static member.

 public class HibernateUtil {
 	public static final String CONFIG = "HibernateUtil.config";
 	private static final Log log = Log.lookup(HibernateUtil.class);
 	private static SessionFactory _factory;

Get the singleton hibernate Session Factory.
 	public static SessionFactory getSessionFactory() {
 		return initSessionFactory((WebApp)null);

Wrapping HibernateUtil.getSessionFactory().getCurrentSession() into a simple API.
 	public static Session currentSession() throws HibernateException {
 		return getSessionFactory().getCurrentSession();

Wrapping HibernateUtil.getSessionFactory().getCurrentSession().close() into a simple API.
 	public static void closeSession() throws HibernateException {

Used in HibernateSessionFactoryListener to init Hibernate SessionFactory.

app web applicaton, given null will try to get it from current Execution.
 	/* package */ static SessionFactory initSessionFactory(WebApp app) {
 		if ( == null) {
 			//read hibernate.config preference
 			if (app == null) {
 				final Execution exec = Executions.getCurrent();
 				if (exec != null) {
 					final Desktop desktop = exec.getDesktop();
 					if (desktop != null) {
 						app = desktop.getWebApp();
			String resource = null;
			if (app != null) {
				final org.zkoss.zk.ui.util.Configuration config = app.getConfiguration();
				resource = config.getPreference(null);
			return initSessionFactory(resource);
		return ;
	/*package*/ static SessionFactory initSessionFactory(String resource) {
		if ( == null) {
			try {
			    // Create the SessionFactory per JavaVM version and allow JDK 1.4 compatibility
				if (JVMs.isJava5()) {
				     = java5Factory(resource);
else {
				  	 = java4Factory(resource);
				.info("Hibernate configuration file loaded: "+ (resource == null ? "hibernate.cfg.xml" : resource));
catch (Throwable ex) {
			    // Make sure you log the exception, as it might be swallowed
			    .error("Initial SessionFactory creation failed." + ex);
			    throw new ExceptionInInitializerError(ex);
		return ;
	//We have to put the instantiation of AnnotationConfiguration
	//in a separate method. Otherwise, it will be loaded even if
	//isJava5 is false
	/*private*/ static SessionFactory java5Factory(String resource) {
		return resource == null ? 
			new AnnotationConfiguration().configure().buildSessionFactory() :
			new AnnotationConfiguration().configure(resource).buildSessionFactory();
	/*private */ static SessionFactory java4Factory(String resource) {
		return resource == null ?
			new Configuration().configure().buildSessionFactory() :
			new Configuration().configure(resource).buildSessionFactory();

Used in HibernateSessionFactoryListener to init Hibernate SessionFactory.
	/* package */ static void cleanupSessionFactory() {
		if ( != null) {
			.close(); // Free all resources
			 = null;
New to GrepCode? Check out our FAQ X