简体   繁体   English

Spring配置中的休眠方言问题

[英]Hibernate dialect issue with Spring configuration

I use Hibernate (through JPA) configured by Spring and when I launch my application (war deployed on Tomcat 6), I get this error: 我使用Spring配置的Hibernate(通过JPA),当我启动应用程序(部署在Tomcat 6上的战争)时,出现以下错误:

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

It seems strange because I've set the hibernate dialect as follows: 这似乎很奇怪,因为我将休眠方言设置如下:

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

For more information, here my full applicationContext.xml: 有关更多信息,请参见我的完整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>

So, I decided to precise Hibernate Dialect within META-INF/persistence.xml file, and this time that works. 因此,我决定在META-INF / persistence.xml文件中精确调整Hibernate Dialect,这一次可行。 Here how I precised it: 这里是我如何精确的:

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

Do you have an idea why Hibernate Dialect is not set using Spring configuration ? 您是否知道为什么未使用Spring配置设置Hibernate Dialect?

Not sure why it won't work with your configuration. 不确定为什么它不适用于您的配置。 Maybe something goes wrong with using the p: annotation. 使用p:注释可能出问题了。 I'll post my code (which works for my config) to try if it will fix your code! 我将发布我的代码(适用于我的配置),以尝试是否可以修复您的代码! :) :)

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

Good luck! 祝好运!

A bit late, but I think this could add value. 有点晚了,但是我认为这可以增加价值。 Not necessarily you need to add databasePlatform property if you specify database property, the adapter itself will figure out the dialect. 如果指定了database属性,则不一定需要添加databasePlatform属性,适配器本身会找出方言。

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

Relevant code from org.springframework.orm.jpa.vendor.HibernateJPAVendorAdapter 来自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;
    }
}

Dialect could be auto-detected from DataSource driver. 可以从DataSource驱动程序中自动检测出方言。 So nether hibernate.dialect no database were needed. 因此, hibernate.dialect不需要database If exception 'hibernate.dialect' not set happened, it usually means, than something wrong with DB connection: 如果'hibernate.dialect' not set异常'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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM