学习hibernate时要做大量的测试,一个比较好的解决方法是使用JUnit,本例延续之前的使用hibernate注解
1.实体类Teacher
package com.baosight.model;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* <p>Title: </p>
* <p>Description:Teacher </p>
* <p>Company: </p>
* @author yuan
* @date 2016-4-10 下午12:32:46*/
@Entity
public class Teacher {
private int id;
private String name;
private String title;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
2.hibernate的配置文件hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/baosight/model/Student.hbm.xml"/>
<mapping class="com.baosight.model.Teacher"/>
</session-factory>
</hibernate-configuration>
3.新建JUnit Test Case对hibernate的save方法进行测试,类名为TeacherTest,在根目录新建源文件夹,使用与Teacher所在包名作为TeacherTest的包名
package com.baosight.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;
/**
* <p>Title:TecherTest </p>
* <p>Description:TODO </p>
* <p>Company: </p>
* @author yuan
* @date 2016-4-13 下午10:32:17*/
public class TeacherTest {
private static SessionFactory sf = null;
@BeforeClass
public static void beforeClass(){
// 读取配置文件
Configuration cfg = new AnnotationConfiguration();
// 得到session工厂
sf = cfg.configure().buildSessionFactory();
}
@Test
public void test() {
// 教师测试类
Teacher t = new Teacher();
t.setId(1);
t.setName("t1");
t.setTitle("中级");
// 得到session
Session session = sf.openSession();
// 开启事务
session.beginTransaction();
// session执行save
session.save(t);
// 事务提交
session.getTransaction().commit();
// 关闭session
session.close();
}
@AfterClass
public static void afterClass(){
// 关闭session工厂
sf.close();
}
}
4.在类名或方法名或空白处点击右键-->Run As-->JUnit Test,运行效果如下:
其中,使用到了@BeforeClass和@AfterClass对读取hibernate配置文件进行了预处理,可以使该方法在测试多个方法时只执行一次,提高了执行效率。
以上即为使用JUnit对hibernate进行测试的内容,不过应该还可以使用Assert的方法吧,待以后探究吧