【发布时间】: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 withSessionFactory`配置。 -
@v.ladynev 请按要求找代码
-
@Archit 请显示错误的堆栈跟踪
-
@Danylo Zatorsky 请找到附加的堆栈跟踪
标签: java hibernate hibernate-mapping