Test报错,无法测试 报错如下
2016-11-26 10:54:02 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
2016-11-26 10:54:02 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.4.Final}
2016-11-26 10:54:02 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2016-11-26 10:54:02 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2016-11-26 10:54:02 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2016-11-26 10:54:02 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2016-11-26 10:54:02 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/hhu/factory/Student.hbm.xml
代码如下
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.hhu.factory.Student;
public class StudentTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Test
public void TestStudent(){
Student student = new Student(1,"洪七公","男",new Date(),"丐帮");
session.save(student);//保存对象进入数据库
}
@Before
public void init(){
//创建配置对象
Configuration config = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工厂对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//打开会话
session = sessionFactory.openSession();
//打开事务
transaction = session.beginTransaction();
}
@After
public void destory(){
//提交事务;
transaction.commit();
//关闭会话;
session.close();
//关闭会话工厂;
sessionFactory.close();
}
}
下面是我的 student.hbm.xml文档
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.hhu.factory.Student" table="STUDENT" catalog="mydata">
<id name="sid" type="java.lang.Integer">
<column name="SID" />
<generator class="native" />
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" />
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER" />
</property>
<property name="birthday" type="java.util.Date">
<column name="BIRTHDAY" length="10" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
</class>
</hibernate-mapping>