org.hibernate.MappingException: Unknown entity: Students

来源:1-10 使用JUnit进行测试

3us1c

2016-06-23 13:02

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 Date getBirthday() {
		return birthday;
	}

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

	public String getAddress() {
		return address;
	}

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

	@Override
	public String toString() {
		return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
				+ ", address=" + address + "]";
	}

}

<?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 name="">
  <property name="connection.username">root</property>
  <property name="connection.password"/>
  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  
  <property name="show_sql">true</property>
  <property name="format">true</property>
  <property name="hbm2ddl.auto">create</property>
  
  <mapping resource="Students.hbm.xml"/>
 </session-factory>
</hibernate-configuration>


import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
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 StandardServiceRegistryBuilder().applySettings(config.getProperties())
				.build();
		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);
	}
}

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-6-23 12:01:49 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="Students" table="STUDENTS">
        <id name="sid" type="int">
            <column name="SID" />
            <generator class="assigned" />
        </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" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        </property>
    </class>
</hibernate-mapping>


写回答 关注

4回答

  • 程序员TF
    2017-01-02 13:27:08

    感谢楼主,我的问题也解决了,困扰了我老久了

  • 慕粉3917970
    2016-09-02 18:46:27

    非常感谢楼主,问题解决了

  • 3us1c
    2016-06-23 16:37:29

    已在hibernate的官网文档找到答案了,可直接使用

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();

    问题解决

    3us1c 回复bytwoh...

    http://docs.jboss.org/hibernate/orm/5.2/quickstart/html_single/

    2016-06-23 22:03:55

    共 4 条回复 >

  • bytwohitsnow
    2016-06-23 16:19:02

    将圈中语句

    http://img.mukewang.com/576b9b560001a4c207840409.jpg

    改成

    http://img.mukewang.com/576b9b6600011db308020036.jpg

    原因不知道。


    3us1c

    回复 1217236338: :)

    2016-06-28 23:46:42

    共 4 条回复 >

Hibernate初探之单表映射

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

74808 学习 · 793 问题

查看课程

相似问题