struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jygnash <jygn...@aol.com>
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. 
1) 
in hibernate-cfg.xml  give the jndi name as below
<hibernate-configuration>
     <session-factory name="HibernateSessionFactory">
2)
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);
}

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


hope that helps

-- 
View this message in context: http://www.nabble.com/Hibernate-and-Struts-tp19985981p20016169.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message