Hibernate找不到Mapping-file:org.hibernate.UnknownEntityTypeException:无法找到persister

时间:2015-11-13 20:06:56

标签: java hibernate

我正在使用hibernate 5并尝试将对象保存到我的数据库中。但出于某种原因,我总是得到一个 Exception in thread "main" org.hibernate.MappingException: Unknown entity: model.database.Customer

My project structure

由于某种原因,未找到Customer.hbm.xml。我真的不知道为什么。

Customer.hbm.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.database.Customer" table="CUSTOMER">
    <id name="username" type="string">
        <column name="USERNAME" length="8" />
        <generator class="assigned"></generator>
    </id>
    <property name="password" type="string">
        <column name="PASSWORD" length="8" />
    </property>
    <property name="lastname" type="string">
        <column name="LASTNAME" length="20" />
    </property>
    <property name="firstname" type="string">
        <column name="FIRSTNAME" length="20" />
    </property>
</class>
</hibernate-mapping>

Customer.java:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="CUSTOMER")
public class Customer  implements java.io.Serializable {



    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="username", nullable=false)

    private String username;

    @Column(name="firstname")
    private String firstname;

    @Column(name="lastname")
    private String lastname;

    @Column(name="password")
    private String password;

    public Customer () {

    }

    public Customer(String username) {
        this.username = username;
    }

    public Customer(String username, String firstname, String lastname, String password) {
        this.username = username;
        this.firstname = firstname;
        this.lastname = lastname;
        this.password = password;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


    public String getFirstname() {
        return this.firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }


    public String getLastname() {
        return this.lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }


    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


}

hibernate.cfg.xml中:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">
      org.hibernate.dialect.PostgreSQL81Dialect
    </property>
    <property name="hibernate.connection.driver_class">
      org.postgresql.Driver
    </property>
    <property name="hibernate.connection.url">
      jdbc:postgresql://localhost:5432/postgres
    </property>
    <property name="hibernate.connection.username">
      postgres
    </property>
    <property name="hibernate.connection.password">
      postgres
    </property>
    <property name="hibernate.current_session_context_class">
      thread
    </property>

    <mapping resource="model/database/Customer.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

主:

public class Main {

    public static void main(String[] args) {
        Customer cstm = new Customer("lebetyp", "peter", "ja", "ja");
        CustomerManager mngr = new CustomerManager();
        mngr.saveCustomer(cstm);
    }
}

的HibernateUtil:

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration configuration = new Configuration().configure();
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            sessionFactory = configuration.buildSessionFactory(builder.build());
            System.out.println("Initial SessionFactory creation");
        } catch (Throwable ex) {
            System.out.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

My dependencies for hibernate

如何摆脱此异常才能使其正常工作。是否缺少依赖?或者我做错了什么?

1 个答案:

答案 0 :(得分:0)

将其添加到您的hibernate.cfg.xml

删除<mapping resource="model/database/Customer.hbm.xml"/>并添加

<mapping class="Customer"></mapping>.

我希望您的Customer类中包含JPA注释,如@ Entity,@ Id等。