【问题标题】:StandardServiceRegistryBuilder not workingStandardServiceRegistryBuilder 不工作
【发布时间】:2014-02-02 12:55:01
【问题描述】:
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;
}

}

我使用的是休眠 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>

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

    oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@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();
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

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

      serviceRegistryBuilder.applySettings(configuration.getProperties());
      

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

      【讨论】:

      • 我已经解决了这个问题。除了 applysettings 添加了“休眠”这一事实之外,configure 和 applysettings 方法几乎相同。数据库设置中每个属性之前的前缀。在xml中手动添加前缀也可以解决问题。
      猜你喜欢
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 2018-12-22
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多