gpt4 book ai didi

java - Hibernate 找不到映射文件 : org. hibernate.UnknownEntityTypeException:无法找到持久器

转载 作者:太空宇宙 更新时间:2023-11-04 13:14:08 24 4
gpt4 key购买 nike

我正在使用 hibernate 5 并尝试将对象保存到我的数据库中。但由于某种原因,我总是得到一个线程“main”org.hibernate.MappingException中出现异常:未知实体: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>

客户.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

我怎样才能摆脱这个异常以使其工作。是否缺少依赖项?或者我做错了什么?

最佳答案

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

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

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

我希望您的 Customer 类中有 JPA 注释,例如 @Entity 、 @Id 等。

关于java - Hibernate 找不到映射文件 : org. hibernate.UnknownEntityTypeException:无法找到持久器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33700934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com