eclipse Hibernate-无法加载在 Hibernate 配置 </mapping> 条目中声明的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29721856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:33:54  来源:igfitidea点击:

Hibernate-Unable to load class declared in Hibernate configuration </mapping> entry

javaeclipsespringhibernate

提问by oddly

In Eclipse, I have been building a simple Hibernate+Spring+MySQL+Maven project recently.I am stuck at the stage of database&java connection.When I run the project, it gives the the following error:

最近在Eclipse中搭建了一个简单的Hibernate+Spring+MySQL+Maven项目,一直卡在database&java连接阶段,运行该项目,报错如下:

Exception in thread "main" org.hibernate.MappingException: Unable to load class [ src/main/java/com.hibernate.data/Person] declared in  Hibernate configuration <mapping/> entry
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2279)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
at com.test.Main.main(Main.java:17)
Caused by: java.lang.ClassNotFoundException:src/main/java/com.hibernate.data/Person
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:260)
at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:193)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2276)
... 5 more

The main class:

主要类:

package com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import com.hibernate.data.Person;

public class Main {
public static void main(String [] args){

    // Create a configuration instance
    Configuration configuration = new Configuration();
    // Provide configuration file
    configuration.configure("hibernate.cfg.xml");
    // Build a SessionFactory
    SessionFactory factory = configuration.buildSessionFactory(new StandardServiceRegistryBuilder().configure().build());
    // Get current session, current session is already associated with Thread
    Session session = factory.getCurrentSession();
    // Begin transaction, if you would like save your instances, your calling of save must be associated with a transaction
    Transaction tx = session.getTransaction();

    // Create person
    Person newPerson = new Person();
    newPerson.setFirstName("Peter");
    newPerson.setLastName("Hymanson");
    newPerson.setGender("Male");
    newPerson.setAge(30);
    //Save
    session.save(newPerson);
    session.flush();

    tx.commit();
    session.close();

    /*
    @SuppressWarnings("deprecation")
    SessionFactory sf = new Configuration().configure().buildSessionFactory();
    System.out.println("CFG and hbm files loaded successfully.");
    Session session = sf.openSession();
    session.beginTransaction();
    System.out.println("Transaction began");
    */

}
}

hbm.xml file:

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">

<hibernate-mapping>
<class table="Person" lazy="false" name="com.hibernate.data.Person" >
    <id column="PERSON_ID" type="int" name="id" >
        <generator class="increment"/>
    </id>
        <property name="firstName" column="PERSON_FIRSTNAME" type="string" />
        <property name="lastName" column="PERSON_LASTNAME" type="string" />
        <property name="gender" column="PERSON_GENDER" type="string" />
        <property name="age" column="PERSON_AGE" type="int"  />
</class>

hibernate.cfg.xml file:

hibernate.cfg.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/PERSONDB</property>
<property name='connection.username'>root</property>
<property name='connection.password'>root</property>

<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Specify session context -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- Show SQL -->
<property name="show_sql">true</property>
<!-- Referring Mapping File -->
<mapping resource="domain-classes.hbm.xml"/>
<mapping class="src/main/java/com.hibernate.data/Person"/>
</session-factory>

</hibernate-configuration>

Person.java:

人.java:

package com.hibernate.data;

public class Person {
private int id;
private String firstName;
private String lastName;
private String gender;
private int age;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
}

What is wrong here?

这里有什么问题?

EDIT:

编辑:

I modified pom.xml file as maven-exec plugin requires:

我修改了 pom.xml 文件,因为 maven-exec 插件需要:

enter image description here

在此处输入图片说明

Additionally, final project structure is as follows:

此外,最终的项目结构如下:

enter image description herebut still, it gives the same error.What am I doing wrong?

在此处输入图片说明但仍然,它给出了同样的错误。我做错了什么?

回答by Sarkhan

i think:

我认为:

<mapping class="src/main/java/com.hibernate.data/Person"/>

must be:

必须是:

<mapping class="com.hibernate.data.Person"/>

also be sure that where is your hbm.xml file?if it is in com package you should write resource like this:

还要确保您的 hbm.xml 文件在哪里?如果它在 com 包中,您应该像这样编写资源:

<mapping resource="com/domain-classes.hbm.xml"/>

回答by hossein ketabi

change

改变

<mapping class="src/main/java/com.hibernate.data/Person"/>

to

<mapping class="com.hibernate.data.Person"/>