In SSH integration, hibernate is hosted on Spring to get SessionFactory

Source: Internet
Author: User

<Prop key = "hibernate. current_session_context_class"> thread </prop>
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 go back to the furnace. My approach is to modify the HibernateUtil file and get the SessionFactory method.
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 on 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 ();
}
}
//
This and traditional methods can be used when hibernate is not hosted on Spring.
SessionFactory = new Configuration (). configure ("/WEB-INF/hibernate. cfg. xml ")
. BuildSessionFactory ();

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.