0

I am using eclipse juno and hibernate 4.1.6, mysql connector 5.1.24, jboss 7.1.1.

I tried this and this(as a module).

I have the connector JAR in WEB-INF/lib folder, and I also tried once without it. Everything failed. I don't know anymore how I can solve this problem. The whole stack trace:

18:35:44,284 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) Error creating Session: org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver com.mysql.jdbc.Driver class not found

The connector is an the class path, it's in the Maven Dependencies Library. I put it in the lib folder and in the System Library...nothing works.

public class HibernateUtil {

private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

static
{
    try
    {
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    }
    catch (HibernateException he)
    {
        System.err.println("Error creating Session: " + he);
        throw new ExceptionInInitializerError(he);
    }
}
Community
  • 1
  • 1
pulse
  • 83
  • 1
  • 4
  • 16
  • First of all, avoid comments like *Please help!! SOS!*. Secondly, you say that you have added the connection to Maven's pom.xml. Can you post your pom.xml? Or at least the part where you specify the `MySQL` connector dependency? – Raul Rene Jul 07 '13 at 17:13
  • mysql mysql-connector-java 5.1.24 – pulse Jul 07 '13 at 17:29
  • This seems right. Please edit your question and post the part where you initialize the DB connection using the MySQL Connector – Raul Rene Jul 07 '13 at 17:30
  • Try adding a `Class.forName("com.mysql.jdbc.Driver");` before the `Configuration configuration = new ..` line – Raul Rene Jul 07 '13 at 17:45
  • tried...but same exception – pulse Jul 07 '13 at 17:48

4 Answers4

3

Check in your hibernate.cfg.xml file that you have correctly specified the driver class and the dialect:

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

(these are properties of the <session-factory>)

Then, simply build your Session Factory:

sessionFactory = new Configuration().configure().buildSessionFactory();

If the application still does not see your Maven dependency for the connector, try doing a clean install, and after it check in the list of libraries for your project that the MySQL Connector appears there.

Raul Rene
  • 10,014
  • 9
  • 53
  • 75
  • im using hibernate 4.1.6...buildSessionFactory is deprecated.hibernate.cfg.xml is correct – pulse Jul 07 '13 at 17:56
  • Yes, it doesn't matter. Deprecated classes/methods are still supported. The only thing deprecation serves is to warn that the code won't work when the next major version comes of hibernate comes out. I've added the non-deprecated code as a separate answer for you to crib. – hd1 Jul 07 '13 at 18:03
  • it still doesnt work with that buildSessionFactory...same exception – pulse Jul 07 '13 at 18:06
  • Did you do a clean install? Did you check to see that the library appears in your classpath? – Raul Rene Jul 07 '13 at 18:08
1

Please check your mysql connector version is compatible with mysql db installed on your system. e.g. mysql connector version 5.1.26 is compatible with mysql 5.6.25 while 5.1.34 is not.

Navin Ketu
  • 11
  • 1
0

Try to add the connector to build path as external jar OR , create a lib folder and put connector jar in it and then Add Library to your build path and give that connector lib folder path.

roger_that
  • 9,493
  • 18
  • 66
  • 102
  • i tried all of it...nothing works...i have a lib folder in WEB-Inf/classes..isnt it enough...i also tried the lib folder of my jboss – pulse Jul 07 '13 at 17:16
  • mysql mysql-connector-java 5.1.24 – pulse Jul 07 '13 at 17:23
  • No changes required in manifest file. Its just that, your program is unable to get mysql connector jar file. Looking at your project structure and build path would be helpful. Try providing any snapshot or something so that i could help on further – roger_that Jul 08 '13 at 04:42
0
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
// end of static block

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
hd1
  • 33,938
  • 5
  • 80
  • 91