hibernate连的oracle数据库测试junit报错,请问是哪里错了?

来源:2-2 hibernate常用配置

zoeyqq

2016-05-09 14:44

http://img.mukewang.com/5730313b0001340a05940277.jpg

import java.util.Date;

public class Students {
	
	private int sid;
	private String sname;
	private String gender;
	private Date birthday;
	private String address;
	
	public Students(){
		
	}
    

	public Students(int sid, String sname, String gender, Date birthday, String address) {
		//super();
		this.sid = sid;
		this.sname = sname;
		this.gender = gender;
		this.birthday = birthday;
		this.address = address;
	}



	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}
	
	

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Students [sid=" + sid +",sname="+ sname + ", gender="
				+ gender+",birthday = "+ birthday + ",address =" + address +"]";
	}
	
	

}

hibernate.cfg.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
     <property name="connection.username">dbusrmkt</property>
     <property name="connection.password">futuremkt</property>
     <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
     <property name="connection.url">jdbc:oracle\:thin\:@172.17.11.60\:1521\:HDQTLS</property>
     <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
     
     <property name="show_sql">true</property>
     <property name="format_sql">true</property>
     <property name="hbm2ddl.auto">create</property>
     <property name="hibernate.default_schema">dbusrmkt</property>
     
     <mapping resource="Students.hbm.xml"/>
     
    </session-factory>
</hibernate-configuration>

StudentsTest 测试类如下:


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;

public class StudentsTest {
	
	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	
	@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();//关闭会话工厂
	}
	
	@Test
	public void testSaveStudents()
	{
		Students s = new Students(1,"张三","男",new Date(),"武汉");
		session.save(s);
	}

}

求教啊,谢谢!

写回答 关注

1回答

  • sherenrui
    2016-05-10 15:54:36

    Failure Trace第一行显示的是什么?

    sheren... 回复zoeyqq

    ok^

    2016-05-10 16:13:57

    共 2 条回复 >

Hibernate初探之单表映射

Java持久化框架Hibernate入门教程,掌握Hibernate基本概念

74810 学习 · 793 问题

查看课程

相似问题