我使用hibernate 5並嘗試將對象保存到我的數據庫。但由於某種原因,我總是得到一個 Exception in thread "main" org.hibernate.MappingException: Unknown entity: model.database.Customer
。Hibernate沒有找到映射文件:org.hibernate.UnknownEntityTypeException:找不到persist
出於某種原因,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;
}
}
我怎樣才能擺脫這種例外的,使其工作。有沒有依賴?或者我做錯了什麼?
你的問題到底是什麼? –
我怎樣才能擺脫這個例外,使其工作。有沒有依賴?或者我做錯了什麼? – testiguy
謝謝,@testiguy。我現在已經添加到你的問題。 –