StandardServiceRegistryBuilder无法正常工作

时间:2014-01-14 12:49:24

标签: java spring hibernate

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;
}

}

我正在使用hibernate 4.3。我收到一条错误消息“当'hibernate.dialect'未设置”时,访问DialectResolutionInfo不能为空。 StandardServiceRegistryBuilder出了点问题。它是首选,因为不推荐使用ServiceRegistryBuilder。请为我解决这个问题。

我的hibernate.cfg.xml文件如下所示 -

 <?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>

2 个答案:

答案 0 :(得分:0)

您可能需要添加以下内容:

serviceRegistryBuilder.applySettings(configuration.getProperties());

在根据此帖子致电serviceRegistryBuilder.build()之前:https://stackoverflow.com/a/21017111/1617124

答案 1 :(得分:0)

             oracle.jdbc.driver.OracleDriver         JDBC:预言:瘦:@localhost:1521:XE         小时         小时

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

    }

}