Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 4 Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

Tags:

java

hibernate

I am using Hibernate latest version 4.3.5.Final.

My hibernate.cfg.xml file content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/Test</property>
    <property name="hibernate.connection.username">pankaj</property>
    <property name="hibernate.connection.password">xxxx</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    <mapping resource="employee.hbm.xml"/>
</session-factory>

</hibernate-configuration>

Utility class to create SessionFactory:

package com.example;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
    try {
        // Create the SessionFactory from hibernate.cfg.xml
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        System.out.println("Hibernate Configuration loaded");

        SessionFactory sessionFactory = configuration.buildSessionFactory(new StandardServiceRegistryBuilder().build());

        return sessionFactory;
    }
    catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
}

When I use it in my main() method, I get the following exception:

May 04, 2014 11:55:56 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Hibernate Configuration loaded
May 04, 2014 11:55:57 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Initial SessionFactory creation failed.org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.example.HibernateUtil.buildSessionFactory(HibernateUtil.java:25)
    at com.example.HibernateUtil.<clinit>(HibernateUtil.java:9)
    at com.example.HibernateMain.main(HibernateMain.java:26)
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
    at com.example.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
    ... 2 more

I have tried so many options and looked for online resources, hibernate docs but couldn't find what is missing here. Any help is appreciated.

like image 603
Pankaj Avatar asked May 05 '14 07:05

Pankaj


People also ask

What is Hibernate Dialect?

A hibernate dialect gives information to the framework of how to convert hibernate queries(HQL) into native SQL queries. Since Hibernate is database agnostic. It can work with different databases. However, databases have proprietary extensions/native SQL variations, and set/sub-set of SQL standard implementations.

What is the Hibernate dialect for MySQL?

Hibernate Dialect Class The SQL types not same for the all data bases, so that specific database will have specific dialect, for example, dialect for the DB2 database is org. hibernate. dialect. DB2Dialect , dialect for the MySql 5 database is org.

What is the dialect for PostgreSQL?

SQL Is the Language for Talking to Databases PostgreSQL, MySQL, Oracle, and SQL Server are all database products by different vendors. SQL is the programming language used to talk to these databases, and each database product has its own variant of SQL.


2 Answers

This will be OK

Configuration cfg = new Configuration().configure();

StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
        .applySettings(cfg.getProperties()).build();

SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);

Session session = sf.openSession();
like image 157
wmmj23 Avatar answered Sep 19 '22 15:09

wmmj23


I once faced the exact same problem, and it turned out that the issue was that I had not started the database service locally (which I see you are also using a local database). That simple!!!

like image 21
geoand Avatar answered Sep 18 '22 15:09

geoand