StandardServiceRegistryBuilder not working

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 get the error "Access to DialectResolutionInfo cannot be null if" hibernate.dialect "is not installed." Something is wrong with StandardServiceRegistryBuilder. It is preferred because ServiceRegistryBuilder is deprecated. Please provide me a 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 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>

+4
source share
2 answers

You probably need to add something like this:

serviceRegistryBuilder.applySettings(configuration.getProperties());

before calling serviceRegistryBuilder.build()according to this post: fooobar.com/questions/31677 / ...

0
source

         oracle.jdbc.driver.OracleDriver       JDBC: : : @localhost: 1521:              

    <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

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();

    }

}
0

All Articles