为了账号安全,请及时绑定邮箱和手机立即绑定

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.test.entity.Grade

请问为什么发生了这个错误。映射文件都是按着教程来的

五月 20, 2016 12:08:54 下午 org.hibernate.Version logVersion

INFO: HHH000412: Hibernate Core {5.1.0.Final}

五月 20, 2016 12:08:54 下午 org.hibernate.cfg.Environment <clinit>

INFO: HHH000206: hibernate.properties not found

五月 20, 2016 12:08:54 下午 org.hibernate.cfg.Environment buildBytecodeProvider

INFO: HHH000021: Bytecode provider name : javassist

五月 20, 2016 12:08:55 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>

INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}

五月 20, 2016 12:08:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)

五月 20, 2016 12:08:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/swh1?useUnicode=true&characterEncoding=UTF-8]

五月 20, 2016 12:08:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH10001001: Connection properties: {user=root, password=****}

五月 20, 2016 12:08:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH10001003: Autocommit mode: false

五月 20, 2016 12:08:55 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>

INFO: HHH000115: Hibernate connection pool size: 1 (min=1)

Fri May 20 12:08:55 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

五月 20, 2016 12:08:55 下午 org.hibernate.dialect.Dialect <init>

INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect

五月 20, 2016 12:08:56 下午 org.hibernate.validator.util.Version <clinit>

信息: Hibernate Validator bean-validator-3.0-JBoss-4.0.2

五月 20, 2016 12:08:56 下午 org.hibernate.validator.engine.resolver.DefaultTraversableResolver detectJPA

信息: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.test.entity.Grade

at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)

at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1533)

at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)

at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)

at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)

at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)

at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)

at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)

at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:682)

at org.hibernate.internal.SessionImpl.save(SessionImpl.java:674)

at org.hibernate.internal.SessionImpl.save(SessionImpl.java:669)

at com.test.entity.TestStudent.add(TestStudent.java:34)

at com.test.entity.TestStudent.main(TestStudent.java:18)


正在回答

5 回答

http://img1.sycdn.imooc.com//59955654000105f911620881.jpg5.0以上使用这个

0 回复 有任何疑惑可以回复我~

hibernate5+用这个方式获取ServiceRegistry、SessionFactory:

ServiceRegistry sr = new StandardServiceRegistryBuilder().configure().build();
sessionFactory = new MetadataSources(sr).buildMetadata().buildSessionFactory();

0 回复 有任何疑惑可以回复我~

我也碰到了和你一模一样的问题,解决了

Exception in thread "main" org.hibernate.MappingException: Unknown entity

刚开始我看到这句话,首先肯定是hibernate的错误 mapping映射错误

我还以为是hibernate的cfg.xml里的<mapping>标签里的映射文件路径写错了,导致找不到持久化类。但是检查确认了老半天,发现文件路径是对的,很崩溃。


后来发现我导入的hibernate框架是我自己从官网上下载的hibernate 5.2,不是老师讲课用的hibernate4.3。

然后重新导了一下老师讲课用的hibernate4.3,然后错误就没了。


你是不是用的不是老师讲课用的hibernate4.3啊,视频下方的课程资源下一下。不要用上一节课的hibernate4.2.4,也不要从官网上下最新的。版本不一样 API可能不一样。


5 回复 有任何疑惑可以回复我~
#1

hhddd

cfg = new Configuration().configure(); sf = cfg.buildSessionFactory(); 高版本获取SessionFactory直接这样获取就行了
2017-08-08 回复 有任何疑惑可以回复我~

package com.test.entity;


import java.io.Serializable;

import java.util.HashSet;

import java.util.Set;


public class Grade implements Serializable {

private int gid;

private String gname;

private String gdesc;

//在一方定义一个多方的集合

private Set<Student> students =new HashSet<Student>();


public Grade() {

//super();

}


public Grade(int gid, String gname, String gdesc, Set<Student> students) {

//super();

this.gid = gid;

this.gname = gname;

this.gdesc = gdesc;

this.students = students;

}




public int getGid() {

return gid;

}


public void setGid(int gid) {

this.gid = gid;

}


public String getGname() {

return gname;

}


public void setGname(String gname) {

this.gname = gname;

}


public String getGdesc() {

return gdesc;

}


public void setGdesc(String gdesc) {

this.gdesc = gdesc;

}


public Set<Student> getStudents() {

return students;

}


public void setStudents(Set<Student> students) {

this.students = students;

}




public Grade(String gname, String gdesc) {

//super();

this.gname = gname;

this.gdesc = gdesc;

}

package com.test.entity;


import java.io.Serializable;

import java.util.HashSet;

import java.util.Set;


public class Grade implements Serializable {

private int gid;

private String gname;

private String gdesc;

//在一方定义一个多方的集合

private Set<Student> students =new HashSet<Student>();


public Grade() {

//super();

}


public Grade(int gid, String gname, String gdesc, Set<Student> students) {

//super();

this.gid = gid;

this.gname = gname;

this.gdesc = gdesc;

this.students = students;

}




public int getGid() {

return gid;

}


public void setGid(int gid) {

this.gid = gid;

}


public String getGname() {

return gname;

}


public void setGname(String gname) {

this.gname = gname;

}


public String getGdesc() {

return gdesc;

}


public void setGdesc(String gdesc) {

this.gdesc = gdesc;

}


public Set<Student> getStudents() {

return students;

}


public void setStudents(Set<Student> students) {

this.students = students;

}




public Grade(String gname, String gdesc) {

//super();

this.gname = gname;

this.gdesc = gdesc;

}

}


}



<?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.test.entity.Grade" table="grade" catalog="swh1">

<id name="gid" type="java.lang.Integer">

<column name="gid" />

<generator class="native" />

</id>

<property name="gname" type="java.lang.String">

<column name="gname" length="20" not-null="true" />

</property>

<property name="gdesc" type="java.lang.String">

<column name="gdesc" length="50" />

</property>

<set name="students" table="student">

<key>

<column name="gid" />

</key>

<one-to-many class="com.test.entity.Student" />

</set>

</class>

</hibernate-mapping>



<?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>

    <!-- Database connection settings -->

    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

    <!--  

    <property name="connection.url">jdbc:mysql://localhost:3306/swh1?useUnicode=true&amp;characterEncoding=UTF-8</property>

    -->

    <property name="connection.url">

    <![CDATA[

    jdbc:mysql://localhost:3306/swh1?useUnicode=true&characterEncoding=UTF-8

    ]]>

    </property>

    

    <property name="connection.username">root</property>

    <property name="connection.password">root</property>



    <!-- SQL dialect -->

    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>


    <!-- Enable Hibernate's automatic session context management -->

    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->

    <!--  

    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

-->

    <!-- Echo all executed SQL to stdout -->

    <property name="show_sql">true</property>


    <!-- Drop and re-create the database schema on startup -->

    <property name="hbm2ddl.auto">update</property>

    <!-- 指定映射文件的路径 -->

    

    <mapping resource="com/test/entity/Grade.hbm.xml"/>

    <mapping resource="com/test/entity/Student.hbm.xml"/>

  </session-factory>

  

  

</hibernate-configuration>


0 回复 有任何疑惑可以回复我~

代码呢兄弟

0 回复 有任何疑惑可以回复我~
#1

蓝小七 提问者

大神看看哪里错了
2016-05-21 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Hibernate初探之一对多映射
  • 参与学习       42171    人
  • 解答问题       295    个

Hibernate中一对多关联映射配置,以及cascade和inverse属性作用

进入课程

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.test.entity.Grade

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信