slf日志接口, 实现有slf4j nodep, log4j
hibernate里我们一般用 log4j,所以删除之前创建的hibernate 包里的 slf4j-nop包, 加入log4j-1.2.17.jar
现在hibernate的包里有slf的api 的jar, 不能和log4j 自动匹配, 所以中间需要一个接口, 再加入 slf4j-log4j 的包 (1.5.8)
这样在运行界面就可以看到有建表等语句.
ddl语句就出来了:
14:00:20,587 INFO SchemaExport:179 - exporting generated schema to database 14:00:20,588 DEBUG SchemaExport:303 - drop table if exists Teacher 14:00:20,890 DEBUG SchemaExport:303 - drop table if exists student 14:00:21,022 DEBUG SchemaExport:303 - create table Teacher (id integer not null, name varchar(255), title varchar(255), primary key (id)) 14:00:21,309 DEBUG SchemaExport:303 - create table student (id integer not null, name varchar(255), age integer, primary key (id)) 14:00:21,530 INFO SchemaExport:196 - schema export complete
因为debug信息太多, 只保留数据库信息可以将log4j.properties里的内容只保留下面2句, 其余都屏蔽:
log4j.rootLogger=warn, stdout log4j.logger.org.hibernate.tool.hbm2ddl=debug
最后ddl日志信息如下:
14:13:24,066 INFO SchemaExport:154 - Running hbm2ddl schema export 14:13:24,068 DEBUG SchemaExport:170 - import file not found: /import.sql 14:13:24,068 INFO SchemaExport:179 - exporting generated schema to database 14:13:24,069 DEBUG SchemaExport:303 - drop table if exists Teacher 14:13:24,193 DEBUG SchemaExport:303 - drop table if exists student 14:13:24,279 DEBUG SchemaExport:303 - create table Teacher (id integer not null, name varchar(255), title varchar(255), primary key (id)) 14:13:24,372 DEBUG SchemaExport:303 - create table student (id integer not null, name varchar(255), age integer, primary key (id)) 14:13:24,552 INFO SchemaExport:196 - schema export complete Hibernate: insert into Teacher (name, title, id) values (?, ?, ?)
搭建junit日志环境
preferrence里新建MyJUnit包, 加入junit包, 然后再项目右键add libary把自己的MyJUnit导入进项目.
1. 创建测试代码包: 项目右键 new --- source folder---test, 里面装测试代码, src里面都是项目代码
2. 给test创建一个和上面model一样的package, 然后创建测试类 TeacherTest.java的unit test, 代码如下:
package com.bjsxt.hibernate.model; import static org.junit.Assert.*; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import org.junit.Test; public class TeacherTest { @Test public void testTeacherSave() { Teacher t =new Teacher(); t.setId(4); t.setName("wdfd"); t.setTitle("high"); Configuration cfg=new AnnotationConfiguration(); SessionFactory sf=cfg.configure().buildSessionFactory(); //默认找hibernate.cfg.xml,然后产生一个connection工厂 Session session = sf.openSession(); session.beginTransaction(); session.save(t); session.getTransaction().commit(); session.close(); sf.close(); } }
上面代码中每次创建sessionFactory比较费时, 所以考虑用beforeclass和afterclass:
package com.bjsxt.hibernate.model; import static org.junit.Assert.*; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class TeacherTest { private static SessionFactory sf=null; @BeforeClass public static void beforeClass(){ sf=new AnnotationConfiguration().configure().buildSessionFactory(); } @Test public void testTeacherSave() { Teacher t =new Teacher(); t.setId(4); t.setName("wdfd"); t.setTitle("high"); Session session = sf.openSession(); session.beginTransaction(); session.save(t); session.getTransaction().commit(); session.close(); sf.close(); } @AfterClass public static void afterClass(){ sf.close(); } }
有时test文件会出错, 无法追踪, 需要在 方法加try catch:
@BeforeClass public static void beforeClass(){ try { sf=new AnnotationConfiguration().configure().buildSessionFactory(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
或者加一个main方法, 调用beforeClass();