2

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

我找不到此错误消息的原因。我的 try & catch 块抛出错误,如:

Exception in saveOrUpdate() Rollback  :org.hibernate.MappingException: Unknown entity: v.esoft.pojos.Employee

波乔(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>
4

1 回答 1

0

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

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

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

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

请注意,基于注解Configuration的 Hibernate 版本可能会有所不同,并且可能需要额外的库。

于 2013-09-10T14:12:08.610 回答