4
public class HibernateSession {

private static final SessionFactory sessionFactory = buildSessionFactory();
private static StandardServiceRegistry serviceRegistry;

private static SessionFactory buildSessionFactory() {
    try {
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
        serviceRegistry = serviceRegistryBuilder.build();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;            
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed!" + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

}

I am using hibernate 4.3. I am getting an error message "Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set". Something is wrong with StandardServiceRegistryBuilder. It's preferred as ServiceRegistryBuilder is deprecated. Please provide me solution to this problem.

My hibernate.cfg.xml file looks like this -

 <?xml version="1.0" encoding="UTF-8"?>

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/company_db</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <!-- <property name="hbm2ddl.auto">create</property> -->

    <mapping resource="com/twopiradian/Employee.hbm.xml" />

</session-factory>

Suva
  • 55
  • 2
  • 9

2 Answers2

0

You probably need to add something like this:

serviceRegistryBuilder.applySettings(configuration.getProperties());

before calling serviceRegistryBuilder.build() as per this post: https://stackoverflow.com/a/21017111/1617124

Community
  • 1
  • 1
Lars
  • 1,311
  • 1
  • 11
  • 11
  • I have already resolved the issue. The method configure and applysettings are mostly same except for the fact that applysettings adds a "hibernate." prefix before every property in the DB settings. Manually adding the prefix in the xml can also resolve the issue. – Suva Feb 26 '14 at 07:51
0

oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@localhost:1521:xe hr hr

    <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
    <property name="hibernate.show_sql">true</property>

    <mapping resource="com/org/Employee.hbm.xml"/>
</session-factory>

Demo.java file

import java.util.Properties;
import org.hibernate.cfg.Configuration;


public class Demo {

    public static void main(String[] args) 
    {
        Employee emp=new Employee();
        emp.setNme("Ravi");
        emp.setSal(1000);

        Configuration con= new Configuration();
        con.configure();
        Properties prop=con.getProperties();
         ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(con.getProperties()).build();

    }

}
anitha
  • 1