Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate.cfg.xml contains info about MySQL database they why I am getting this exception?

This is question about java hibernate.

my hibernate.cfg.xml is

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/poc</property>
    <property name="hibernate.connection.username">user</property>
    <property name="hibernate.connection.password"/>
    <mapping class="test.person" file="" jar="" package="" resource="person.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Connection to the database is working and I can explore databases and tables,

my sample

person.hbm.xml is

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-mapping>
    <class name="test.person" table="person">
        <id name="id" type="int" column="id" >
            <generator class="assigned"/>
        </id>
        <property name="fName">
            <column name="fName" />
        </property>
        <property name="lName">
            <column name="lName"/>
        </property>
        <property name="age">
            <column name="age"/>
        </property>
        <property name="gender">
            <column name="gender"/>
        </property>
    </class>
</hibernate-mapping>

this is my person class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

public class person {

    private int id;
    private String fName;
    private String lName;
    private String gender;
    private int age;

    public person() {
        System.out.println("person");
    }

    public int getId() {
        return id;
    }

    public int getAge() {
        return age;
    }

    public String getfName() {
        return fName;
    }

    public String getGender() {
        return gender;
    }

    public String getlName() {
        return lName;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

}

Here comes my Main class

package test;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Test {

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {


        Session session = null;
        SessionFactory sessionFactory =new Configuration().configure().buildSessionFactory();
        session = sessionFactory.openSession();

        person obj = new person();
        obj.setfName("Aqif");
        obj.setlName("Hamid");
        obj.setAge(24);
        obj.setGender("Male");

        session.save(obj);
        session.flush();
        session.close();

    }
}

I am getting this exception,

SEVERE: JDBC Driver class not found: org.apache.derby.jdbc.ClientDriver
java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
    at org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:61)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56)
    at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:414)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
    at test.Test.main(Test.java:15)
Exception in thread "main" org.hibernate.HibernateException: JDBC Driver class not found: org.apache.derby.jdbc.ClientDriver
    at org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:66)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56)
    at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:414)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
    at test.Test.main(Test.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
    at org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:61)
    ... 7 more
Java Result: 1

Please help me resolve these exceptions

I donot know why I am getting this exception

java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver

Where as my hibernate.cfg.xml is set for MySQL database


EDIT:

Location of files,

It is located at root of .java files

C:.....NetBeansProjects\test\src

That is src folder of my project.

and java files are in

C:.....NetBeansProjects\test\src\test

I have tried by coping my hibernate.cfg.xml file to some other locations as well.

like image 978
Aqif Hamid Avatar asked Aug 08 '11 20:08

Aqif Hamid


People also ask

Is it mandatory to have this option in hibernate CFG XML?

Try removing that and hibernate will not look for the non-existent file. Basically you are setting all the required properties via your properties object so there is no real need to tell Hibernate to look for a hibernate. cfg. xml file which is exactly what the configure() method does.

Can Hibernate be used with MySQL?

Hibernate will then use MySQL's autoincremented database column to generate the primary key values.

Where should I put hibernate CFG XML?

Let us create hibernate. cfg. xml configuration file and place it in the root of your application's classpath. You will have to make sure that you have testdb database available in your MySQL database and you have a user test available to access the database.


2 Answers

The hibernate.cfg.xml must be present in the root of the classpath. Hibernate uses the current thread's context classloader, to locate and load this file. This would mean that:

  • If the Test class is within a JAR, then the config file, must be at the root of the JAR.
  • If the Test class is in a directory, say /classes/test/Test.class, then hibernate.cfg.xml must be present in /classes.

Your current issue would most likely be due to another hibernate.cfg.xml file being located and loaded by Hibernate. This is possible if no configuration file was found in the root of your class path; Hibernate then delegates the loading of the configuration file to the Environment class' classloader, until it locates a configuration file. To avoid this, you must ensure that your desired config file, with the MySQL related properties, must be present in the root of your classpath.

like image 78
Vineet Reynolds Avatar answered Nov 15 '22 14:11

Vineet Reynolds


You are using the wrong hibernate.cfg.xml. It should be located in your src, not in a package/subdir.

like image 24
Vlad Avatar answered Nov 15 '22 15:11

Vlad