-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: To get a Session of an existing SessionFactory
PostPosted: Fri Jun 02, 2006 3:18 pm 
Beginner
Beginner

Joined: Sun May 07, 2006 4:41 pm
Posts: 20
Location: São Paulo - BRAZIL
I'm using Hibernate 3 + TOMCAT 5.5, and in the hibernate site explain how configure the pool connections when the aplications has started. To lever this I has a SessionFatory created. My question is: As i make to create a new Session by SessionFactory existing in the memory?

Thank's.

_________________
Paulo Nepomuceno
Java Developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 6:47 pm 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
Once your application is running, you need to get a handle to the SessionFactory. Once you have that, it's as easy as:
Code:
SessionFactory sessionFactory = (some lookup code here);
Session session = sessionFactory.openSession();
// do Hibernate stuff here


Without knowledge of your application, I can't tell you how you might go about acquiring your SessionFactory.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 03, 2006 3:33 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Try this,

public class HibernateHelper{

protected static SessionFactory hibernateFac = null;
protected Session session;

protected void closeHibernateSession(Session session) throws Exception {
if (session != null) {
try {
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}

public void closeSession()throws Exception
{
this.closeHibernateSession(session);
}

public static SessionFactory getHibernateFactory() throws Exception {
SessionFactory sessionFactory = null;
try {
sessionFactory= new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
return sessionFactory;
}


public Session getHibernateSession() throws Exception {
Session session = null;
try {
if (hibernateFac == null) {
hibernateFac = HibernateHelper.getHibernateFactory();
}
session = hibernateFac.openSession();
} catch (HibernateException e) {
e.printStackTrace();
}
return session;
}

}
}

All the Best

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.