MappingException与未知实体

时间:2018-06-08 07:46:48

标签: java hibernate

基本示例使用注释。在hibernate.cfg.xml中配置了映射类。 但是我得到了这个异常org.hibernate.MappingException: Unknown entity: com.fh.entities.Customer

package com.fh.entities;

@Entity
public class Customer {
    @Id
    private int id;
    private String firstName;
    private String lastName;
    private String mobile;
    private String email;
//setters & getters.
}

在hibernate.cfg.xml中:

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">test</property>
        <property name="connection.password">system</property>
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>
        <mapping class ="com.fh.entities.Customer"></mapping>
    </session-factory>
</hibernate-configuration>

我在Eclipse中遇到异常,如下所示:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further 
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.fh.entities.Customer

1 个答案:

答案 0 :(得分:0)

您可以使用@Table(name = "Customer", catalog = "") 在您的实体类上,并在启动应用程序之前在数据库中创建表。 更新:如下所示,对于实时应用程序,“创建”并非空闲。

 <property name="hbm2ddl.auto">update</property>