/osms/hibernate/utility and add the following lines so the application can interact with Hibernate:

package com.hp.osms.hibernate.utility; import org.hibernate.HibernateException; import org.hibernate.Session;

import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {

private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); private static Configuration configuration = new Configuration();

private static org.hibernate.SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATION;

private HibernateSessionFactory() {

}

public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get();

if (session == null !session.isOpen()) { if (sessionFactory == null) {

rebuildSessionFactory();

}

session = (sessionFactory != null) ? sessionFactory.openSession():null; threadLocal.set(session);

}

return session;

}

public static void rebuildSessionFactory() { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) {

System.err.println("Can not create SessionFactory!!"); e.printStackTrace();

}

}

public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null);

if (session != null) session.close();

}

public static org.hibernate.SessionFactory getSessionFactory() { return sessionFactory;

}

public static void setConfigFile(String configFile) { HibernateSessionFactory.configFile = configFile; sessionFactory = null;

}

}

6.Compile the HibernateSessionFactory.java file by entering the following commands:

#cd $CATALINA_HOME/webapps/SimpleDemo/WEB-INF/classes/com/hp \ /osms/hibernate/utility

#javac -cp $CATALINA_HOME/webapps/SimpleDemo \/WEB-INF/lib/hibernate3.jar HibernateSessionFactory.javaThe file HibernateSessionFactory.class is generated.7.Create a Hibernate mapping file named Users.hbm.xml, in the directory

$CATALINA_HOME/webapps/SimpleDemo/WEB-INF/classes/com/hp/osms/hibernate according to the definition in the USERS table that contains the following lines:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>

<class name="com.hp.osms.hibernate.Users" table="USERS"> <id name="userId" type="java.lang.Long">

<column name="USER_ID" precision="10" scale="0" /> <generator class="native">

<param name="sequence"> HIBERNATE_SEQUENCE</param> </generator>

</id>

Installing, Configuring, and Managing Web Server Middleware Stack Components 39