7

我使用 Spring 配置的 Hibernate(通过 JPA),当我启动我的应用程序(部署在 Tomcat 6 上的战争)时,我收到此错误:

org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set 

这似乎很奇怪,因为我将休眠方言设置如下:

p:databasePlatform="org.hibernate.dialect.MySQL5Dialect

有关更多信息,这里是我的完整 applicationContext.xml:

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


    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="com.mysql.jdbc.Driver"
          p:url="jdbc:mysql://localhost/room_management" p:username="root" p:password=""/>

    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          p:dataSource-ref="dataSource" p:persistenceUnitName="RoomManagement">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                  p:database="MYSQL"
                  p:databasePlatform="org.hibernate.dialect.MySQL5Dialect"
                  p:showSql="true"/>
        </property>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.orm.jpa.JpaTransactionManager"
          p:entityManagerFactory-ref="entityManagerFactory"/>

    <context:annotation-config/>


    <context:component-scan base-package="com.parisdescartes.roommanagement.*"/>

    <tx:annotation-driven/>

</beans>

因此,我决定在 META-INF/persistence.xml 文件中精确定位 Hibernate Dialect,这一次可行。这是我如何精确的:

<properties>
            <property name="hibernate.dialect"
                      value="org.hibernate.dialect.MySQL5Dialect"/>
</properties>

你知道为什么没有使用 Spring 配置设置 Hibernate Dialect 吗?

4

3 回答 3

25

不知道为什么它不适用于您的配置。使用 p: 注释可能会出现问题。我将发布我的代码(适用于我的配置)以尝试它是否会修复您的代码!:)

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false"/>
            <property name="generateDdl" value="true"/>
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
        </bean>
    </property>
</bean>

祝你好运!

于 2012-05-04T08:31:04.283 回答
7

有点晚了,但我认为这可以增加价值。databasePlatform如果指定属性,不一定需要添加属性database,适配器本身会找出方言。

<property name="database" value="MYSQL" />

相关代码来自org.springframework.orm.jpa.vendor.HibernateJPAVendorAdapter

protected Class determineDatabaseDialectClass(Database database) {
    switch (database) {
        case DB2: return DB2Dialect.class;
        case DERBY: return DerbyDialect.class;
        case H2: return H2Dialect.class;
        case HSQL: return HSQLDialect.class;
        case INFORMIX: return InformixDialect.class;
        case MYSQL: return MySQLDialect.class;
        case ORACLE: return Oracle9iDialect.class;
        case POSTGRESQL: return PostgreSQLDialect.class;
        case SQL_SERVER: return SQLServerDialect.class;
        case SYBASE: return SybaseDialect.class;
        default: return null;
    }
}
于 2013-12-17T11:19:15.797 回答
1

可以从DataSource驱动程序自动检测方言。所以hibernate.dialect根本database不需要。如果发生异常'hibernate.dialect' not set,通常意味着数据库连接有问题:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
      p:dataSource-ref="dataSource"
      p:packagesToScan="ru.javawebinar.**.model">

    <property name="jpaPropertyMap">
        <map>
            <entry key="#{T(org.hibernate.cfg.AvailableSettings).FORMAT_SQL}" value="${hibernate.format_sql}"/>
            <entry key="#{T(org.hibernate.cfg.AvailableSettings).USE_SQL_COMMENTS}" value="${hibernate.use_sql_comments}"/>
        </map>
    </property>

    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
              p:showSql="${jpa.showSql}">
        </bean>
    </property>
</bean>
于 2016-10-02T14:07:31.657 回答