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.cfg.AnnotationConfiguration;

Utility to access Hibernate Session. This implementation 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.

Applicable to Hibernate version or later

As of release 6.0.2, please use the official Hibernate's method instead.
 public class HibernateUtil {
A preference or a library property used to configure HibernateUtil. It first looks up org.zkoss.zk.ui.util.Configuration.getPreference, and then Library.getProperty. Ignored if none of them is specified.
 	public static final String CONFIG = "HibernateUtil.config";
 	private static final Logger log = LoggerFactory.getLogger(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 {

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

Used in HibernateSessionFactoryListener to init Hibernate SessionFactory.

app web application, 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) {
				resource = app.getConfiguration().getPreference(null);
			if (resource == null)
				resource = Library.getProperty();
			return initSessionFactory(resource);
		return ;
	/*package*/ static SessionFactory initSessionFactory(String resource) {
		if ( == null) {
			try {
				 = resource == null ? 
					new AnnotationConfiguration().configure().buildSessionFactory() :
					new AnnotationConfiguration().configure(resource).buildSessionFactory();
				.debug("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);
		return ;

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