template.save(object)后数据未插入数据库

时间:2013-02-14 12:53:45

标签: spring hibernate sessionfactory nhibernate-configuration

我重新初始化了Session Factory,现在我想使用这个新初始化的Session Factory使用Hibernate将数据插入到数据库中,但我得到的错误是“没有hibernate会话绑定到线程”。我该怎么办?我的代码如下:

        Class<?>[] classes = new Class<?>[2];
    classes[0] =com.orb.bean.UserTab.class;
    classes[1] =com.orb.bean.CompanyInfo.class;

        String jdbcUrl = "jdbc:mysql://192.116.0.102:3306/hello";
        java.util.Properties hibernateProperties = new java.util.Properties();
        hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        hibernateProperties.setProperty("hibernate.show_sql", "true");
        hibernateProperties.setProperty("hibernate.hbm2ddl.auto","none");
        hibernateProperties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        hibernateProperties.setProperty("hibernate.connection.url", jdbcUrl);
        hibernateProperties.setProperty("hibernate.connection.username", uname);
        hibernateProperties.setProperty("hibernate.connection.password", password);

           AnnotationSessionFactoryBean slfb = new AnnotationSessionFactoryBean();
          // LocalSessionFactoryBean slfb= new LocalSessionFactoryBean();
        slfb.setHibernateProperties(hibernateProperties);
        slfb.setAnnotatedClasses(classes);


            try {
                slfb.afterPropertiesSet();
            } catch (Exception e) {                 
                e.printStackTrace();
            }

        SessionFactory mySessionFactory = slfb.getObject();
          context.getAutowireCapableBeanFactory().initializeBean(mySessionFactory, "mySessionFactory");
      //  HibernateTemplate templete=(HibernateTemplate) context.getBean("hibernateTemplate");
       // templete.setSessionFactory(mySessionFactory);  

        Dao dao=(Dao) context.getBean("Dao");
        dao.setFactory(mySessionFactory);
        context.getAutowireCapableBeanFactory().initializeBean(dao, "Dao");

0 个答案:

没有答案