小编典典

Hibernate-回滚:org.hibernate.MappingException:未知实体

hibernate

我写了一些代码将数据插入SQL数据库。实际上,我正在尝试使用Hibernate学习Struts2。但是,不幸的是,我在提交表格后遇到了问题。

我找不到此错误消息的原因。我的try&catch块引发如下错误:

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

}

还具有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>

阅读 271

收藏
2020-06-20

共1个答案

小编典典

如果未映射实体,则应检查hibernate配置。Hibernate是ORM框架,用于将pojo(实体)映射到数据库架构对象。您没有配置或hibernate找不到该对象的映射Employee。配置文件hibernate.cfg.xml应包含到资源的映射Employee.hbm.xml。假设此文件与Employeeclass
在同一文件夹中。然后映射将是

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

如果您使用基于注释的配置,那么另一种方法是,您应该使用classattribute映射到包含Hibernate / JPA注释的pojo。

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

注意,基于注释的Configuration版本可能会有所不同,具体取决于Hibernate的版本,并且可能需要其他库。

2020-06-20