struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jygnash <>
Subject Re: Hibernate and Struts
Date Thu, 16 Oct 2008 18:35:14 GMT

Hi Anoop, 

YOu can use the JNDI context to get the Hibernate's  SessionFactory. 
in hibernate-cfg.xml  give the jndi name as below
     <session-factory name="HibernateSessionFactory">
in one of your startup servlet or first class handling the request for
session factory creation (we had a start up servlet) , you can have the
session factory created and made avialable
try {			
	//Create the SessionFactory from hibernate.cfg.xml
	sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
	//	Make sure you log the exception, as it might be swallowed
	System.err.println("Initial SessionFactory creation failed." + ex);
	throw new ExceptionInInitializerError(ex);

//in rest of classes which needs the hibernate session factory can get it
from JNDI
InitialContext ctx = new InitialContext();
SessionFactory sessionFactory = (SessionFactory)    
session = sessionFactory.openSession(); //.getCurrentSession();

hope that helps

View this message in context:
Sent from the Struts - User mailing list archive at

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message