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