Hibernate 关于配置Mapping不成功 Unknow Entity

Hibernate 5.xx加载hibernate.cfg.xml时<mapping resource="xxx.hbm.xml" />不生效,必须使用config.addClass()才行,否则就报org.hibernate.MappingException: Unknown entity异常 把Hibernate换回到4.x就生效了。

因为获取Seeeion Factory的方法发生了变化

应使用

StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.configure().build();
// 创建绘画工厂
sessionFactory = new MetadataSources( serviceRegistry ).buildMetadata().buildSessionFactory();
// 绘画对象
session = sessionFactory.openSession();
// 开启事物
transaction = session.beginTransaction();

代码如下 直接复制即可:

package test;

import java.util.Date;

import javax.persistence.Entity;

public class Students {
	/**
	 * 1.公有的类 2.提供不带参数的默认构造方法 3.属性私有 4.属性setter/getter封装
	 */
	private int sid;
	private String sname;
	private String gender;
	private Date birthady;
	private String address;

	public Students() {
	}

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

	@Override
	public String toString() {
		return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthady=" + birthady
				+ ", 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 getBirthady() {
		return birthady;
	}

	public void setBirthady(Date birthady) {
		this.birthady = birthady;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.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>
		<property name="connection.username">root</property>
		<property name="connection.password"></property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		<property name="hbm2ddl.auto">update</property>
		<mapping resource="test/Students.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
<?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-10-31 0:59:31 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="test.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="birthady" type="java.util.Date">
            <column name="BIRTHADY" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        </property>
    </class>
</hibernate-mapping>
4.X版本的test文件如下

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
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;

import test.Students;

public class StudentTest {

	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;

	@Before
	public void init() {
		// 创建配置对象
		<span style="color:#ff0000;">Configuration config = new Configuration().configure();
		config.addClass(test.Students.class);
		// 创建服务注册对象
		StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
				.applySettings(config.getProperties()).build();
		// 创建绘画工厂
		sessionFactory = config.buildSessionFactory(serviceRegistry);</span>
		// 绘画对象
		session = sessionFactory.openSession();
		// 开启事物
		transaction = session.beginTransaction();

	}

	@After
	public void destory() {
		transaction.commit();// 提交事务
		session.close();
		sessionFactory.close();
	}

	@Test
	public void testSaveStudents() {
		Students s = new Students(2, "daley", "男", new Date(), "shanxi");
		session.save(s);
	}
}
5.X版本如下

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
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;

import test.Students;

public class StudentTest {

	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;

	@Before
	public void init() {
		// 创建配置对象
		
		// 创建服务注册对象
<span style="color:#ff0000;">		StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
				.configure().build();
		// 创建绘画工厂
		sessionFactory = new MetadataSources( serviceRegistry ).buildMetadata().buildSessionFactory();</span>
		// 绘画对象
		session = sessionFactory.openSession();
		// 开启事物
		transaction = session.beginTransaction();

	}

	@After
	public void destory() {
		transaction.commit();// 提交事务
		session.close();
		sessionFactory.close();
	}

	@Test
	public void testSaveStudents() {
		Students s = new Students(3, "daley", "男", new Date(), "shanxi");
		session.save(s);
	}
}



上一篇:4.6. 路由配置


下一篇:IBM WebSphere Application Server Network Deployment 8.5.0.X 安装配置及补丁升级指南之Windows系统篇