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();
            
        }
    }
  • 相关阅读:
    PHP 使用memcached
    linux下使用yum安装 mencached
    mysql 连接字符串 CONCAT
    linux 下 apache启动、停止、重启命令
    js中push()的用法
    linux下使用yum安装mysql
    SVN服务器多个项目的权限分组管理
    yum 安装nginx
    Linux下php安装Redis安装
    使用BarcodeLib.Barcode.ASP.NET生成条形码
  • 原文地址:https://www.cnblogs.com/unixshell/p/3187248.html
Copyright © 2011-2022 走看看