[英]'Unknown Entity Exception' in Hibernate
我正在研究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>
请注意,提供了所有类似的数据连接条目
Stacktrace的异常:
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)
休眠配置和实际的类名中必须有一个错字。 在你所提供的
com.myproj.Student
和
<mapping class="com.proj.Student" />
所以我想您已经手动将其匿名化了,因此我怀疑您在真实的班级名称中也有类似的错字。
我建议从使用简单的Hibernate的JPA2。*开始。 您仍然可以将其用作您的持久性提供程序。
您提到您使用的是Hibernate 3,但没有使用次要版本,因此我首先要指出一些可能会有所帮助的事情,因为它看起来像您在项目中的早期,并且可能具有一定的灵活性。 但是,我的回答不会偏离Hibernate 3的细节。
AnnotationConfiguration
似乎已在Hibernate 3.5中添加 (所以我假设您正在使用Hibernate 3.5+) AnnotationConfiguration
,并且Configuration
所有等效功能 您在下面的行中有一个错字,但是我认为您的代码中没有错字,或者与MappingException
会出现编译错误:
sf = config.configure().buidSessionFactory();
// should be sf = config.configure().buildSessionFactory();
我建议以下内容:
hibernate.cfg.xml
并且没有错字 hibernate.cfg.xml
在您的CLASSPATH中 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,因此我的配置文件位于<path/to/project>/<project_name>/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
文件中的<mapping />
元素而言是多余的,我犹豫是否要提出此建议,因为如果您无法在hibernate.cfg.xml
解析<mapping />
元素,则可以您将要对要保留的所有所有带注释的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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.