码迷,mamicode.com
首页 > 编程语言 > 详细

SSH中将hibernate托管给spring获取session的方法

时间:2014-09-05 17:41:51      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:style   os   io   ar   cti   sp   on   c   new   

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
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;




public class PlanMachineTest {
private static final SessionFactory sessionFactory; 
static { 
try { 
Resource resource=new ClassPathResource("/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 void main(String[] args) {
    Session session = sessionFactory.openSession();
    Machine m = (Machine)session.get(Machine.class, 1);
        PlanMachine pm = new PlanMachine();
        pm.setMachine(m);
        save(pm);
    }


    public static void save(Object o){
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(o);
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    }
}

SSH中将hibernate托管给spring获取session的方法

标签:style   os   io   ar   cti   sp   on   c   new   

原文地址:http://www.cnblogs.com/shenguo/p/3958218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!