我有一个非常简单的 Web 应用程序,它使用 Spring 3.0.2、Hibernate 3.5.1、JPA 2 和 Derby 在 Tomcat 中运行。我在 persistence.xml
中定义了我所有的数据库连接,并且仅使用 Spring 进行依赖注入(inject)。我使用嵌入式 Derby 作为我的数据库。
当我以经典的 Hibernate 方式在 persistence.xml
中定义驱动程序和 url 属性时,一切正常,如下所示:
<property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="hibernate.connection.url" value="jdbc:derby:webdb;create=true"/>
当我将我的配置切换到 JPA2 标准化属性时出现问题,如下所示:
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:webdb;create=true"/>
当使用 JPA2 属性键时,应用程序很难退出,但出现以下异常:
java.lang.UnsupportedOperationException: The user must supply a JDBC connection
有谁知道为什么会失败?
注意:我已经直接从 Hibernate 引用文档中复制了 javax... 属性字符串,所以拼写错误的可能性极小。
最佳答案
答案似乎是这是一个 Spring 问题,可能源于 LocalContainerEntityManagerFactoryBean
的使用。我使用 Spring 来启用 @PersistenceContext
注释,而不是以标准 Java SE 方式手动初始化 EntityManager。当我将 @PersistenceContext
替换为 Persistence.createEntityManagerFactory("WebApp").createEntityManager();
(并在我的 Spring 配置中注释掉 EntityManager 内容)时,一切正常正如预期的那样。
作为引用,这是我使用的 Spring 配置:
<?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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="net.webapp"/>
<tx:annotation-driven/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
</beans>
关于java - 为什么 Hibernate 会忽略我的 persistence.xml 中的 JPA2 标准化属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2660485/