java org.hibernate.HibernateException:当“hibernate.dialect”时,对 DialectResolutionInfo 的访问不能为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41698190/
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-11-03 06:04:19  来源:igfitidea点击:

org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect'

javaspringhibernate

提问by Sid

I know this is a commonly asked question, but none of the solutions available are working out for me. Below is my booltstraps.xml:

我知道这是一个常见问题,但没有一个可用的解决方案适合我。下面是我的 booltstraps.xml:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


<!--
    This configuration file configures the Database Access Object or DAO objects. 
    It is used at system start to bootstrap configurations. 
    DAO objects will also be used by the main PassMark application.
-->
    <bean class="com.passmarksecurity.utils.AppContext" id="appContext">
        <description>Simple bean to handle initializations</description>
    </bean>

    <bean class="com.company.csd.dao.transaction.CompanyHibernateTransactionManager" id="transactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
        <description>
            Database configuration settings.
        <!-- config-->
        <property name="driverClassName">
            <value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
        </property>
        <property name="url">
            <value>jdbc:sqlserver://localhost:1433;databaseName=CORE</value>
        </property>
        <property name="username">
            <value>core_user</value>
        </property>     
        <property name="password">
            <value>core_user</value>
        </property> 
        <!-- config-->      
    </bean>


    <!-- Configure DAOFactory -->
    <bean class="com.passmarksecurity.dao.api.DAOFactory" id="daoFactory" init-method="configPostProcess">
        <property name="DAOConfig">
            <ref bean="daoConfig"/>
        </property>
    </bean>
    <!-- IMPORTANT: Configure this setting first -->

    <!-- This is for Oracle configuration with database images -->
    <bean class="com.passmarksecurity.dao.config.DAOConfigMSSQL" id="daoConfig"/>


    <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory" lazy-init="true">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
                <prop key="hibernate.cache.use_query_cache">false</prop>
                <prop key="hibernate.show_sql">false</prop> 
            </props>
        </property>

    </bean> 
</beans>

When the application runs I get the below exception:

当应用程序运行时,我收到以下异常:

    - <Tool failed: Error creating bean with name 'transactionManager' defined in class path resource [pmBootstrapConfigs.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [pmBootstrapConfigs.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set>
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [pmBootstrapConfigs.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [pmBootstrapConfigs.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1475)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1220)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [pmBootstrapConfigs.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1572)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
    ... 17 more
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:205)
    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:1887)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1631)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
    ... 24 more

if I add <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>explicitly to the hibernate properties it starts working.

如果我<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>明确添加到休眠属性,它就会开始工作。

The most common answer available online is that there is some issue in connecting to the DB. So I tried writing a separate piece of code to just access the DB with the same details, and it worked. Dont know what the problem is.

在线提供的最常见答案是连接到数据库时存在一些问题。所以我尝试编写一段单独的代码来访问具有相同细节的数据库,并且它起作用了。不知道是什么问题。

回答by Mickael

In your XML Spring configuration, you must update :

在您的 XML Spring 配置中,您必须更新:

<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory" lazy-init="true">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
                <prop key="hibernate.cache.use_query_cache">false</prop>
                <prop key="hibernate.show_sql">false</prop> 
                <!-- HERE THE DIALECT CONFIGURATION -->
            </props>
        </property>

    </bean> 

From Hibernate documentation, you must provide the dialect as following :

Hibernate 文档中,您必须提供如下方言:

  • A dialect for Microsoft SQL Server 2008 with JDBC Driver 3.0 and above
  • 带有 JDBC Driver 3.0 及更高版本的 Microsoft SQL Server 2008 的方言

<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>

<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>

  • A dialect for Microsoft SQL Server 2000 and 2005
  • Microsoft SQL Server 2000 和 2005 的方言

<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>

<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>

回答by Shilan

if you specify your database name you won't need to set the dialect. To do so you can use JpaVendorAdaptor as a property for org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean or HibernatePersistenceProvider (for hibernate versions lower than 5) In this case if it is microsoft sql server

如果您指定数据库名称,则不需要设置方言。为此,您可以使用 JpaVendorAdaptor 作为 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean 或 HibernatePersistenceProvider(对于低于 5 的休眠版本)的属性在这种情况下,如果它是 microsoft sql server

    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="database" value="SQL_SERVER" />
        </bean>
    </property>

回答by Shilan

I had similar issue while testing Mysql-8 with Hibernate-5 & Spring-5. Initially, I had <prop key="dialect">org.hibernate.dialect.MySQL8Dialect</prop>in hibernatePropertiestag. After 10 mins of testing I was able to add a fix by replacing <prop key="dialect">with <prop key="dialect">.

我在使用 Hibernate-5 和 Spring-5 测试 Mysql-8 时遇到了类似的问题。最初,我曾<prop key="dialect">org.hibernate.dialect.MySQL8Dialect</prop>hibernateProperties标签。经过 10 分钟的测试后,我能够通过替换<prop key="dialect"><prop key="dialect">.

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="packagesToScan" value="com.model"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>