0

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:

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("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 file:

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

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

What is wrong here?

EDIT:

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

enter image description here

Additionally, final project structure is as follows:

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

oddly
  • 260
  • 3
  • 9
  • 22

2 Answers2

2

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:

<mapping resource="com/domain-classes.hbm.xml"/>
Sarkhan
  • 1,281
  • 1
  • 11
  • 33
2

change

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

to

<mapping class="com.hibernate.data.Person"/>
hossein ketabi
  • 480
  • 7
  • 19