【问题标题】:Hibernate-Unable to load class declared in Hibernate configuration </mapping> entry [duplicate]Hibernate - 无法加载在 Hibernate 配置中声明的类 </mapping> 条目 [重复]
【发布时间】:2015-06-25 15:24:33
【问题描述】:

在Eclipse中,我最近在构建一个简单的Hibernate+Spring+MySQL+Maven项目,卡在数据库&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

主类:

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("Jackson");
    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 文件:

<?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 文件:

<?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:

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;
}
}

这里有什么问题?

编辑:

我根据 maven-exec 插件的要求修改了 pom.xml 文件:

另外,最终项目结构如下:

但是,它仍然给出了同样的错误。我做错了什么?

【问题讨论】:

  • 我将此标记为重复。点击链接。

标签: java eclipse spring hibernate


【解决方案1】:

我认为:

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

必须是:

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

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

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

【讨论】:

    【解决方案2】:

    改变

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多