Hibernate - 回滚:org.hibernate.MappingException:未知实体 [英] Hibernate - Rollback :org.hibernate.MappingException: Unknown entity

查看:136
本文介绍了Hibernate - 回滚:org.hibernate.MappingException:未知实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了几个将数据插入到我的SQL数据库的代码。其实,我正在尝试用Hibernate学习Struts2。但不幸的是,我在提交表单后遇到了问题。



我无法找到此错误消息的原因。我的尝试& catch块会抛出如下错误:

  saveOrUpdate()中的异常回滚:org.hibernate.MappingException:未知实体:v.esoft。 pojos.Employee 

Pojo( Employee.java

  @Entity 
@Table(name =employee,catalog = eventusdb)
公共类Employee实现java.io.Serializable {
$ b $ private私人empId;
私人字符串名称;
私人字符串网站;
$ b $ 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映射DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\"> ;
<! - 由Hibernate Tools 3.4.0.CR1生成2013年9月10日4:29:04 PM - >
< hibernate-mapping>
< class name =v.esoft.pojos.Employeetable =employeecatalog =eventusdb>
< id name =empIdtype =java.lang.Integer>
< column name =emp_id/>
< generator class =identity/>
< / id>
< property name =nametype =string>
< column name =namenot-null =true/>
< / property>
< property name =websitetype =string>
< column name =websitelength =65535not-null =true/>
< / property>
< / class>
< / hibernate-mapping>


解决方案

如果实体未映射,您应该检查休眠配置。 Hibernate是用于将pojos(实体)映射到数据库模式对象的ORM框架。您没有配置或者hibernate无法找到对象 Employee 的映射。配置文件是 hibernate.cfg.xml 应该包含映射到资源 Employee.hbm.xml 。假设这个文件和 Employee class在同一个文件夹中。然后,映射将是

 < mapping resource =v / esoft / pojos / Employee.hbm.xml/> 

如果您使用基于注释的配置,那么您应该使用 class 属性映射到包含Hibernate / JPA注释的pojo。

 < mapping class =v.esoft.pojos.Employee/> 

请注意,基于配置的注释可能不同取决于Hibernate的版本,可能需要额外的库。

I have written few codes for inserting data into my SQL database. Actually, I am trying to learn Struts2 with Hibernate. But, unfortunately I am facing a problem after submitting my form.

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):

@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;
    }

}

also having 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>

解决方案

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"/>

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"/>

Note, annotation based Configuration might be different depending on version of Hibernate and may require additional libraries.

这篇关于Hibernate - 回滚:org.hibernate.MappingException:未知实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆