zoukankan      html  css  js  c++  java
  • SSH中将hibernate托管给spring获取session的方法

    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();
        }
    }

  • 相关阅读:
    mysql 按出现次数排序
    拼接sql
    java 操作 excel
    jQuery ui 利用 datepicker插件实现开始日期(minDate)和结束日期(maxDate)
    android压力测试命令monkey详解
    java css
    iss 默认上传大小为30 M
    PHP自动生成后台导航网址的最佳方法
    PHP 文件上传的综合实例
    php字符串首字母转换大小写的实例
  • 原文地址:https://www.cnblogs.com/shenguo/p/3958218.html
Copyright © 2011-2022 走看看