<prop key= "Hibernate.current_session_context_class" >thread</prop>
And then
Resource resource=new Classpathresource ("/web-inf/applicationcontext.xml");
Beanfactory factory=new xmlbeanfactory (Resource);
Sessionfactory sessionfactory = (sessionfactory) factory.getbean ("Sessionfactory");
You can get it.
The rest will not be melted down, my practice is to modify the Hibernateutil file to get the Sessionfactory method to solve everything
Import org.hibernate.HibernateException;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.cfg.Configuration;
Import Org.springframework.beans.factory.BeanFactory;
Import Org.springframework.beans.factory.xml.XmlBeanFactory;
Import Org.springframework.core.io.ClassPathResource;
Import Org.springframework.core.io.Resource;
Get sessionfactory when Hibernate is hosted to spring
public class Hibernateutil {
private static final sessionfactory sessionfactory;
static {
try {
Resource resource=new Classpathresource ("/web-inf/applicationcontext.xml");
Beanfactory factory=new xmlbeanfactory (Resource);
Sessionfactory = (sessionfactory) factory.getbean ("Sessionfactory");
catch (Hibernateexception ex) {
throw new RuntimeException ("Exception Building sessionfactory:"
+ Ex.getmessage (), ex);
}
}
public static final ThreadLocal session = new ThreadLocal ();
public static session Currentsession () throws Hibernateexception {
Session S = (session) Session.get ();
Open a new session, if this Thread has none yet
if (s = = null) {
s = sessionfactory.opensession ();
Session.set (s);
}
return s;
}
public static void CloseSession () throws Hibernateexception {
Session S = (session) Session.get ();
Session.set (NULL);
if (s!= null)
S.close ();
}
}
//
When Hibernate is not hosted for spring use this and the traditional way can be obtained AH
Sessionfactory = new Configuration (). Configure ("/web-inf/hibernate.cfg.xml")
. Buildsessionfactory ();
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.