【发布时间】:2012-05-13 12:04:32
【问题描述】:
我使用 Spring 配置的 Hibernate(通过 JPA),当我启动我的应用程序(部署在 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 Dialect 吗?
【问题讨论】:
-
如何构建会话工厂?
-
我只是在 entityManager 引用上使用@PersistenceContext 注释
标签: java mysql spring hibernate jpa