gpt4 book ai didi

java - Spring 配置的 Hibernate 方言问题

转载 作者:可可西里 更新时间:2023-11-01 06:34:20 24 4
gpt4 key购买 nike

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

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

这看起来很奇怪,因为我将 hibernate 方言设置如下:

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 方言吗?

最佳答案

不确定为什么它不适用于您的配置。使用 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>

祝你好运!

关于java - Spring 配置的 Hibernate 方言问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10440444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com