• 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();
            
        }
    }
  • 相关阅读:
    Django数据库 相关之select_related/prefetch_related
    Django 序列化
    Django 信号
    Django缓存配置和使用
    Django FBV/CBV、中间件、GIT使用
    学员管理系统(SQLAlchemy 实现)
    Oracle的三种高可用集群方案
    linux系统安装硬盘分区建议
    Linux下expdp自动备份
    impdp导入报错ORA-39070:无法打开日志文件
  • 原文地址:https://www.cnblogs.com/unixshell/p/3187248.html
Copyright © 2020-2023  润新知