【问题标题】:Hibernate - Rollback: org.hibernate.MappingException: Unknown entity休眠 - 回滚:org.hibernate.MappingException:未知实体
【发布时间】:2018-06-09 23:33:21
【问题描述】:

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

我找不到此错误消息的原因。我的 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>

【问题讨论】:

  • 第几行你得到这个例外了吗?
  • @Aniket 只有我不知道?如何知道我在哪一行得到异常?
  • 除了您的System.out.println() 之外,还使用e.printStackTrace();。对于未来,使用 Log4J 或类似的东西,它将为您解析异常的 StackTrace。
  • @AndreaLigios 这个问题实际上解决了我必须映射我的 POJO 类,但是在运行这段代码之后它没有将我的表单数据插入到表中。我收到1 row inserted successfully. 消息,但没有在我的表中插入数据。

标签: java sql hibernate struts2 pojo


【解决方案1】:

如果实体未映射,则应检查休眠配置。 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 的版本而有所不同,并且可能需要额外的库。

【讨论】:

  • 感谢我在 hibernate.cfg 文件中添加了&lt;mapping class="v.esoft.pojos.Employee"/&gt;,现在这个问题已经解决了。但现在它没有插入我的数据库代码。它没有显示此错误的任何错误。可能是什么错误?
  • 可能是您未提交的事务,或与数据库相关的事务。
  • 谢谢 这个问题已经解决了。也许你能帮我解决这个问题stackoverflow.com/questions/18742091/…
猜你喜欢
  • 2016-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多