Hibernate 关于配置Mapping不成功 Unknow Entity

简介: Hibernate 5.xx加载hibernate.cfg.xml时不生效,必须使用config.addClass()才行,否则就报org.hibernate.MappingException: Unknown entity异常 把Hibernate换回到4.x就生效了。

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);
	}
}



相关文章
|
6月前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——TestStu.java
hibernate正向生成数据库表以及配置——TestStu.java
|
6月前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——Teacher.hbm.xml
hibernate正向生成数据库表以及配置——Teacher.hbm.xml
|
6月前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——Teacher.java
hibernate正向生成数据库表以及配置——Teacher.java
|
6月前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——Student.java
hibernate正向生成数据库表以及配置——Student.java
|
6月前
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Dept实体类和对应的配置信息
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Dept实体类和对应的配置信息
|
3月前
|
数据库 开发者 Java
Hibernate映射注解的魔力:实体类配置的革命,让你的代码量瞬间蒸发!
【8月更文挑战第31天】Hibernate 是一款出色的对象关系映射框架,简化了 Java 应用与数据库的交互。其映射注解让实体类配置变得直观简洁。本文深入剖析核心概念与使用技巧,通过示例展示如何简化配置。
44 0
|
5月前
|
前端开发 Java 关系型数据库
在Spring3 MVC中五步配置集成注解方式Hibernate3
在Spring3 MVC中五步配置集成注解方式Hibernate3
41 3
|
6月前
|
Java 数据库连接
错误org.hibernate.AnnotationException: No identifier specified for entity
请根据你的实际情况,将实体类中的字段和注解进行适当的调整,以确保每个实体类都有一个明确定义的标识符(主键)。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
73 0
|
6月前
|
JavaScript Java 数据库连接
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Emp实体类和对应的配置信息
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Emp实体类和对应的配置信息
|
Java 关系型数据库 数据库连接
Spring Boot 学习研究笔记(八) - Spring Data JPA与Hibernate区别及配置(2)
Spring Boot 学习研究笔记(八) - Spring Data JPA与Hibernate区别及配置
266 0