Hibernate - Rollback: org.hibernate.MappingException: Unknown entity(休眠 - 回滚:org.hibernate.MappingException:未知实体)
问题描述
我编写了一些代码来将数据插入到我的 SQL 数据库中.实际上,我正在尝试使用 Hibernate 学习 Struts 2.但是,不幸的是,我在提交表单后遇到了问题.
I have written few codes for inserting data into my SQL database. Actually, I am trying to learn Struts 2 with Hibernate. But, unfortunately I am facing a problem after submitting my form.
我找不到此错误消息的原因.我的尝试catch 块抛出如下错误:
I could not find the reason for this error message. My try & catch block throws error like:
Exception in saveOrUpdate() Rollback :org.hibernate.MappingException: Unknown entity: v.esoft.pojos.Employee
Pojo(Employee.java
):
Pojo(Employee.java
):
@Entity
@Table(name = "employee", catalog = "eventusdb")
public class Employee implements java.io.Serializable {
private Integer empId;
private String name;
private String website;
public Employee() {
}
public Employee(String name, String website) {
this.name = name;
this.website = website;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "emp_id", unique = true, nullable = false)
public Integer getEmpId() {
return this.empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
@Column(name = "name", nullable = false)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "website", nullable = false, length = 65535)
public String getWebsite() {
return this.website;
}
public void setWebsite(String website) {
this.website = website;
}
}
还有Employee.hbm.xml
:
<?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 Sep 10, 2013 4:29:04 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="v.esoft.pojos.Employee" table="employee" catalog="eventusdb">
<id name="empId" type="java.lang.Integer">
<column name="emp_id" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="name" not-null="true" />
</property>
<property name="website" type="string">
<column name="website" length="65535" not-null="true" />
</property>
</class>
</hibernate-mapping>
推荐答案
如果实体没有被映射,你应该检查休眠配置.Hibernate 是用于将 pojo(实体)映射到数据库模式对象的 ORM 框架.您没有配置或休眠找不到对象 Employee
的映射.配置文件是 hibernate.cfg.xml
应该包含到资源 Employee.hbm.xml
的映射.假设此文件与 Employee
类位于同一文件夹中.然后映射将是
If the entity isn't mapped, you should inspect the hibernate configuration. Hibernate is ORM framework used to map pojos (entities) to the database schema objects. You didn't configure or hibernate couldn't find mapping for the object Employee
. The configuration file is hibernate.cfg.xml
should contain the mapping to the resource Employee.hbm.xml
. Suppose this file is in the same folder as Employee
class. Then the mapping will be
<mapping resource="v/esoft/pojos/Employee.hbm.xml"/>
如果您使用基于注解的配置的另一种方法,那么您应该使用 class
属性映射到包含 Hibernate/JPA 注解的 pojo.
Another approach if you used an annotation based configuration, then you should use class
attribute to map to the pojo that contains Hibernate/JPA annotations.
<mapping class="v.esoft.pojos.Employee"/>
注意,基于注解的 Configuration
可能会根据 Hibernate 的版本而有所不同,并且可能需要额外的库.
Note, annotation based Configuration
might be different depending on version of Hibernate and may require additional libraries.
这篇关于休眠 - 回滚:org.hibernate.MappingException:未知实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:休眠 - 回滚:org.hibernate.MappingException:未知实体
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01