zoukankan      html  css  js  c++  java
  • Hibernate 框架的配置之一

    1. 下载hibernate distribution 3.6.* final包

    2. 解压zip包

    3. 将lib目录下jpa和required目录下的jar包都拷贝到自己工程的WEB-INF目录下lib目录下

    4. 测试hibernate 

    /**
     * 
     */
    package com.test;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    
    
    /**
     * @author Administrator
     *
     */
    public class TestMain {
        public static void main(String[] args) {
    
            Configuration configuration = new Configuration();
            
            SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
            
            Session  session =  sessionFactory.openSession();
            
            Transaction transaction = session.beginTransaction();
            
            News news= new News();
            
            news.setContent("test content");
            news.setTitle("test title");
        
            session.save(news);
            session.close();
            sessionFactory.close();
            
        }
    }

    发现错误,org.hibernate.HibernateException: JDBC Driver class not found: com.mysql.jdbc.Driver

    导入mysql-connector-java-5.1.25-bin.jar包到lib目录下

    继续测试,发现错误

    JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2

    google,发现这个错误是debug 延时

    直接运行发现,数据库表中并没有多一条数据,发现没有commit

    /**
     * 
     */
    package com.test;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    
    
    /**
     * @author Administrator
     *
     */
    public class TestMain {
        public static void main(String[] args) {
    
            Configuration configuration = new Configuration();
            
            SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
            
            Session  session =  sessionFactory.openSession();
            
            Transaction transaction = session.beginTransaction();
            
            News news= new News();
            
            news.setContent("test content");
            news.setTitle("test title");
        
            session.save(news);
            transaction.commit();
            session.close();
            sessionFactory.close();
            
        }
    }
  • 相关阅读:
    实习笔记day03
    实习笔记day02
    实习笔记day01
    第4章:数组与方法
    栈内存与堆内存的区别
    java数据类型
    保护模式指令
    空描述符
    段描述符
    全局描述符表
  • 原文地址:https://www.cnblogs.com/unixshell/p/3187248.html
Copyright © 2011-2022 走看看