3 Replies - 8121 Views - Last Post: 21 April 2015 - 03:51 AM

#1 oddly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 05-January 15

Hibernate-Unable to load class declared in Hibernate configuration

Posted 18 April 2015 - 02:21 PM

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



pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Spring-Hibernate-JSF-MySQL-Example</groupId>
  <artifactId>Spring-Hibernate-JSF-MySQL-Example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <configuration>
             <mainClass>com.test.Main</mainClass>
        </configuration>
       </plugin>
    </plugins>
  </build>
  <url>http://maven.apache.org</url>
  <repositories>
        <repository>
            <id>prime-repo</id>
            <name>PrimeFaces Maven Repository</name>
            <url>http://repository.primefaces.org</url>
            <layout>default</layout>
        </repository>
  </repositories>
  <dependencies>
    <!-- Servlet -->
  	<dependency>
  		<groupId>javax.servlet</groupId>
  		<artifactId>servlet-api</artifactId>
  		<version>2.4</version>
  	</dependency>
  	<!-- Faces Library -->
  	<dependency>
		<groupId>com.sun.faces</groupId>
		<artifactId>jsf-api</artifactId>
		<version>2.2.10</version>
	</dependency>
	<!-- Faces Implementation -->
	<dependency>
		<groupId>com.sun.faces</groupId>
		<artifactId>jsf-impl</artifactId>
		<version>2.2.10</version>
	</dependency>
	<!-- Primefaces Version 5 -->
	<dependency>
		<groupId>org.primefaces</groupId>
		<artifactId>primefaces</artifactId>
		<version>5.0</version>
	</dependency>
	<!-- JSTL Library -->
	<dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>jstl</artifactId>
         <version>1.1.2</version>
     </dependency>
     <!-- JSP Library -->
	<dependency>
		<groupId>javax.servlet.jsp</groupId>
		<artifactId>jsp-api</artifactId>
		<version>2.1</version>
	</dependency>
	<!-- Primefaces Theme Library -->
	<dependency>
		<groupId>org.primefaces.themes</groupId>
		<artifactId>blitzer</artifactId>
		<version>1.0.10</version>
	</dependency>
	<!-- Hibernate library -->
	<dependency>
		<groupId>org.hibernate</groupId>
		<artifactId>hibernate-core</artifactId>
		<version>4.3.6.Final</version>
	</dependency>
	<!-- MySQL driver connector library -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.6</version>
	</dependency>
	<!-- Spring ORM -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-orm</artifactId>
		<version>3.1.1.RELEASE</version>
	</dependency>
	<!-- Spring Web -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>4.1.6.RELEASE</version>
	</dependency>
	<!-- Hibernate Requirements -->
	<dependency>
		<groupId>commons-dbcp</groupId>
		<artifactId>commons-dbcp</artifactId>
		<version>1.4</version>
	</dependency>
	<dependency>
		<groupId>org.javassist</groupId>
		<artifactId>javassist</artifactId>
		<version>3.18.1-GA</version>
	</dependency>
	<dependency>
		<groupId>commons-collections</groupId>
		<artifactId>commons-collections</artifactId>
		<version>3.1</version>
	</dependency>
	<dependency>
		<groupId>javax.transaction</groupId>
		<artifactId>jta</artifactId>
		<version>1.1</version>
	</dependency>
	<dependency>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<version>3.1</version>
		<type>maven-plugin</type>
	</dependency>
	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.12</version>
	</dependency>
  </dependencies>
</project>



Finally, project structure is as attached below.

What am I doing wrong here?

Attached image(s)

  • Attached Image

This post has been edited by oddly: 18 April 2015 - 02:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Hibernate-Unable to load class declared in Hibernate configuration

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 984
  • View blog
  • Posts: 3,879
  • Joined: 13-June 14

Re: Hibernate-Unable to load class declared in Hibernate configuration

Posted 19 April 2015 - 12:14 AM

On line 23 of your hibernate.cfg.xml file, you have

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



The '/' should be a '.'. Remember that classes in Java are qualified by their packages and we separate subpackages by '.'.
Was This Post Helpful? 0
  • +
  • -

#3 oddly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 05-January 15

Re: Hibernate-Unable to load class declared in Hibernate configuration

Posted 21 April 2015 - 01:37 AM

Thanks for the reply.I didn't notice that there is '/' instead of comma.
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 984
  • View blog
  • Posts: 3,879
  • Joined: 13-June 14

Re: Hibernate-Unable to load class declared in Hibernate configuration

Posted 21 April 2015 - 03:51 AM

It's a dot or a period, not a comma!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1