Java 未设置“hibernate.dialect”时,Hibernate 4 对 DialectResolutionInfo 的访问不能为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23466951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:12:50  来源:igfitidea点击:

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

javahibernate

提问by Pankaj

I am using Hibernate latest version 4.3.5.Final.

我正在使用 Hibernate 最新版本4.3.5.Final

My hibernate.cfg.xmlfile content:

我的hibernate.cfg.xml文件内容:

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

用于创建 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:

当我在我的main()方法中使用它时,出现以下异常:

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.

我尝试了很多选择并寻找在线资源、休眠文档,但找不到这里缺少的内容。任何帮助表示赞赏。

采纳答案by Pankaj

Finally able to fix the issue, the issue is with the way SessionFactoryis getting created.

终于能够解决这个问题,问题在于SessionFactory创建的方式。

For a starter, official document is the first place to go and I must say that Hibernate official documentation seems to be not update with latest version changes.

首先,官方文档是第一个去的地方,我必须说 Hibernate 官方文档似乎没有随着最新版本的变化而更新。

Fix is to apply the configuration settings to StandardServiceRegistryBuilderinstance.

修复是将配置设置应用于StandardServiceRegistryBuilder实例。

// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
        .applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration
        .buildSessionFactory(serviceRegistry);

Please refer my blog post herefor more details.

在此处参考我的博客文章以获取更多详细信息。

回答by geoand

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

我曾经遇到过完全相同的问题,结果发现问题是我没有在本地启动数据库服务(我看到您也在使用本地数据库)。就这么简单!!!

回答by JamesENL

Remove both lines of setting the dialect, Hibernate 4 does a fantastic job of determining which dialect to use based on the JDBC Driver you give it and the database that it connects too. You don't need to specify a dialect. If you force a dialect that is incorrect or doesn't quite match the driver (which can happen as dialects get updated more often than JDBC drivers do) Hibernate might not perform as expected.

删除设置方言的两行,Hibernate 4 在根据您提供的 JDBC 驱动程序和它连接的数据库确定使用哪种方言方面做得非常出色。您无需指定方言。如果您强制使用不正确或与驱动程序不完全匹配的方言(这可能发生,因为方言比 JDBC 驱动程序更新得更频繁)Hibernate 可能无法按预期执行。

Configuring Hibernate using a hibernate.cfg.xml is how things were done when using Hibernate 3. Hibernate 4 supports XML configuration within the Spring Application context. (I'm assuming that you are using Spring) It hooks in really smoothly. I suggest that you switch to using the new method of configuring Hibernate as your configuration doctype is set for Hibernate 3.

使用 hibernate.cfg.xml 配置 Hibernate 是使用 Hibernate 3 时完成的事情。Hibernate 4 支持 Spring 应用程序上下文中的 XML 配置。(我假设您正在使用 Spring)它真的很顺利地挂上了。我建议您切换到使用配置 Hibernate 的新方法,因为您的配置文档类型是为 Hibernate 3 设置的。

This is how you want to specify a dataSource for Hibernate 4 using the Spring application context.

这就是您希望使用 Spring 应用程序上下文为 Hibernate 4 指定数据源的方式。

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/Test" />
    <property name="username" value="username" />
    <property name="password" value="password" />
</bean>

and then you wire it into your session factory like this:

然后像这样将它连接到会话工厂:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="package.where.your.annotated.objects.are" />
    <property name="hibernateProperties">
        <props>
            <!-- Uncomment this when testing -->
            <!--<prop key="hibernate.show_sql">true</prop>-->
            <prop key="hibernate.use_sql_comments">true</prop>
            <prop key="hibernate.hbm2ddl.auto">validate</prop>
        </props>
    </property>
</bean>
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

回答by ibai

The same problem occurred to me with the mysql service running, correct credentials, but the database name was misspelled, so basically the database did not exist.

同样的问题发生在我的 mysql 服务运行,正确的凭据,但数据库名称拼写错误,所以基本上数据库不存在。

回答by wmmj23

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

回答by Evan

From the hibernate source code here, a StandardServiceRegistryBuilder instance is used to get different services from hibernate.cfg.xml, for example serviceRegistry.getService( JdbcServices.class ), serviceRegistry.getService( CacheImplementor.class ).

这里的 hibernate 源代码来看,StandardServiceRegistryBuilder 实例用于从 hibernate.cfg.xml 获取不同的服务,例如serviceRegistry.getService( JdbcServices.class )serviceRegistry.getService( CacheImplementor.class )

The old configuration.buildSessionFactory()method is as below now:

configuration.buildSessionFactory()方法如下:

public SessionFactory buildSessionFactory() throws HibernateException {
    Environment.verifyProperties( properties );
    ConfigurationHelper.resolvePlaceHolders( properties );
    final ServiceRegistry serviceRegistry =  new StandardServiceRegistryBuilder()
            .applySettings( properties )
            .build();
    setSessionFactoryObserver(
            new SessionFactoryObserver() {
                @Override
                public void sessionFactoryCreated(SessionFactory factory) {
                }

                @Override
                public void sessionFactoryClosed(SessionFactory factory) {
                    ( (StandardServiceRegistryImpl) serviceRegistry ).destroy();
                }
            }
    );
    return buildSessionFactory( serviceRegistry );
}

回答by Cristina

I encountered this issue when using the following:

我在使用以下内容时遇到了这个问题:

  • Spring Framework / Data: 4.1.6;
  • Spring Boot: 1.2.3;
  • Hibernate provider for JPA: 4.3.8.
  • Spring 框架/数据:4.1.6;
  • 弹簧靴:1.2.3;
  • JPA 的休眠提供程序:4.3.8。

I had SSLenabled on the database side as part of the host-based authentication mechanism (using Postgres) and the issue was that I hadn't specified the SSLJVM properties related to the key store containing the certificate of the client app when starting the application:

SSL在数据库端启用了作为基于主机的身份验证机制(使用 Postgres)的一部分,问题是我SSL在启动应用程序时没有指定与包含客户端应用程序证书的密钥库相关的JVM 属性:

-Djavax.net.ssl.keyStore=mykeystore.jks -Djavax.net.ssl.keyStorePassword=myPassword

-Djavax.net.ssl.keyStore=mykeystore.jks -Djavax.net.ssl.keyStorePassword=myPassword

Note: I understand the answer has already been answered, but I'm leaving this answer for future reference.

注意:我知道答案已经得到解答,但我将保留此答案以供将来参考。

回答by Cristina

You cal also fix the issue by creating the sesisonFactory this way.

您还可以通过以这种方式创建 sesisonFactory 来解决该问题。

SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory(
new StandardServiceRegistryBuilder().applySettings(new
Configuration().configure().getProperties()).build());
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory(
new StandardServiceRegistryBuilder().applySettings(new
Configuration().configure().getProperties()).build());

回答by denispyr

In my case I ran the configuration routine twice. I had automatic configuration (ie config from xml in resources) in one method, then I implemented a configuration by setting properties in another method but I forgot to bypass the former. As soon as I did, the error gone.

就我而言,我运行了两次配置例程。我在一种方法中进行了自动配置(即从资源中的 xml 配置),然后我通过在另一种方法中设置属性来实现配置,但我忘记绕过前者。我一做,错误就消失了。

回答by phil

I had the exactly the same problem and the issue was, that my computer was not entered in the pg_hba.conf of the postgres database that allows which Clients(IP-Adresses) are allowed to speak with the database

我遇到了完全相同的问题,问题是,我的计算机没有输入到 postgres 数据库的 pg_hba.conf 中,该数据库允许允许哪些客户端(IP 地址)与数据库交谈