【问题标题】:'Unknown Entity Exception' in HibernateHibernate 中的“未知实体异常”
【发布时间】:2017-10-31 16:07:32
【问题描述】:

我正在从事 Hibernate 3 项目并面临

“未知实体”异常

。我有所有的类,它们通过使用注释映射到表。我已在默认的“hibernate.cfg.xml”文件中创建了所有必需的条目。

我得到一个“未知实体异常”并试图弄清楚为什么我得到了相同的结果,即使我所有必需的配置都是准确的。

我已经为“@Entity”注释包含了正确的包,即: 'javax.persistence.Entity' 并且还在 'hibernate.cfg.xml' 文件中提供了条目 'mapping class' 的完整类名。

有人知道还有什么其他的异常原因吗?

以下是实体类:

package com.myproj;
import java.io.serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="STUDENT")
public class Student implements Serializable{
    @Id
    @Column(name="student_id")
    private Long studentId;

    @Column(name="student_name")
    private Long studentName;

    //getters and setters

}

创建 SessionFactory 的片段

SessionFactory sf;
Configuration config = new AnnotationConfiguration();
sf = config.configure().buidSessionFactory();

Student student = null;
Session session = sf.openSession();
student = (Student)session.get(Student.class, new Integer(1)); //Unknown Entity Exception thrown over here while trying to retrieve a Student with ID 1.

hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
    <!-- All the data connection entries are provided here -->
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <mapping class="com.myproj.Student" /> 
</session-factory>
</hibernate-configuration>

请注意,提供了所有数据连接条目

异常的堆栈跟踪:

org.hibernate.MappingException: Unknown entity: com.myproj.Student
org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:629)
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:91)
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:908)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:845)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:838)
com.myproj.StudentBean.search(StudentBean.java:50)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)

【问题讨论】:

  • 请在您的问题中添加一个您将获得Unknown Entity Exception 的实体类。同时添加hibernate.cfg.xml' and the code with SessionFactory`配置。
  • @v.ladynev 请按要求找代码
  • @Archit 请显示错误的堆栈跟踪
  • @Danylo Zatorsky 请找到附加的堆栈跟踪

标签: java hibernate hibernate-mapping


【解决方案1】:

您的休眠配置和实际类名中一定有错字。在您提供的内容中,您拥有

com.myproj.Student

<mapping class="com.proj.Student" /> 

所以我猜你已经手动将它匿名化,因此我怀疑你在真实的类名中有类似的错字。

我建议从使用 JPA2.* 开始,而不是普通的 Hibernate。您仍然可以将其用作您的现场提供者。

【讨论】:

  • 是的..正确,这是错字,很抱歉造成混乱。更正错字后仍然无法正常工作。感谢您注意到它!
【解决方案2】:

您提到您使用的是 Hibernate 3,但不是哪个次要版本,所以我将首先指出一些可能会有所帮助的事情,因为您似乎处于项目的早期并且可能具有一定的灵活性。但是,我的回答不会偏离 Hibernate 3 的详细信息。

  • Hibernate 3.2 中添加了注释支持
  • AnnotationConfiguration 在 Hibernate 3.5 中似乎是 added(所以我假设您使用的是 Hibernate 3.5+)
  • AnnotationConfiguration 在 Hibernate 3.6 中是 deprecated,所有等效功能都存在于 Configuration
  • 当前版本现在是 Hibernate 5.2.12

您在以下行中有错字,但我认为您的代码中不存在该错字,否则您将遇到编译错误而不是 MappingException

sf = config.configure().buidSessionFactory();  
// should be sf = config.configure().buildSessionFactory();

我建议如下:

  • 确认你的配置文件名是hibernate.cfg.xml,没有错别字
  • 确认hibernate.cfg.xml 在您的类路径中
  • 您可以尝试明确指定hibernate.cfg.xml 的路径

例如,

// Hibernate 3.5
sf = new AnnotationConfiguration()
        .configure("/path/to/hibernate.cfg.xml")
        .buildSessionFactory();

// Hibernate 3.6+
sf = new Configuration()
        .configure("/path/to/hibernate.cfg.xml")
        .buildSessionFactory();

例如,在我的项目中,我使用的是 Maven,因此我的配置文件位于&lt;path/to/project&gt;/&lt;project_name&gt;/src/main/resources/hibernate.cfg.xml,因此我不必显式提供路径。

为了简洁起见,您可能故意省略了 XML DTD 数据,我不知道这与您看到的 MappingException 有什么关系,但请确保 XML DTD 在您的 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="...">...</property>
        <mapping class="..."/>
    </session-factory>
</hibernate-configuration>

以下内容对于您的hibernate.cfg.xml 文件中的&lt;mapping /&gt; 元素是多余的,我不愿提出此建议,因为如果您无法解析hibernate.cfg.xml 中的&lt;mapping /&gt; 元素,您将不得不对您想要保留的任何和所有带注释的 POJO 执行此操作。但是,作为绝对的最后手段,您可以尝试addAnnotatedClass()

// Hibernate 3.5
sf = new AnnotationConfiguration()
        .addAnnotatedClass(Student.class)
        .configure()
        .buildSessionFactory();

// OR Hibernate 3.6+
sf = new Configuration()
        .addAnnotatedClass(Student.class)
        .configure()
        .buildSessionFactory(); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2015-09-22
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    相关资源
    最近更新 更多