3

我有一个使用 Spring 3.0.2、Hibernate 3.5.1、JPA 2 和 Derby 在 Tomcat 中运行的非常简单的 Web 应用程序。我正在定义我所有的数据库连接,persistence.xml并且仅使用 Spring 进行依赖注入。我使用嵌入式 Derby 作为我的数据库。

persistence.xml当我以经典的 Hibernate 方式定义驱动程序和 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... 属性字符串,因此极不可能出现拼写错误。

4

4 回答 4

3

答案似乎是这是一个 Spring 问题,可能源于使用LocalContainerEntityManagerFactoryBean. 我使用 Spring 来启用@PersistenceContext注释的使用,而不是以标准 Java SE 方式手动初始化 EntityManager。当我@PersistenceContextPersistence.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>
于 2010-04-19T00:40:32.903 回答
2

无法重现(虽然我没有使用 Spring)。这是我的persistence.xml,它对我有用:

<?xml version="1.0" encoding="UTF-8"?>
<persistence
    xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

  <persistence-unit name="PetstorePu" transaction-type="RESOURCE_LOCAL">

    <properties>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby:webdb;create=true"/>

      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>
</persistence>

以防万一,这是我正在使用的 Maven 依赖项:

<!-- JPA2 provider -->
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>3.5.1-Final</version>
</dependency>
<!-- Logging -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.5.10</version>
</dependency>
<!-- JDBC driver -->
<dependency>
  <groupId>org.apache.derby</groupId>
  <artifactId>derby</artifactId>
  <version>10.5.3.0_1</version>
</dependency>

我的类路径如下:

替代文字

于 2010-04-18T02:47:49.177 回答
2

如果你没有使用 Maven 依赖并且只使用了 eclipse 类路径设置,你只需要将上面的 jar 文件放在 hibernate 3.5 及以上版本的类路径中

1)休眠核心Jar

2)休眠注释jar

3) jboss 日志记录罐

4)休眠实体管理器jar

JPA API 2. jar(它包含在 hibernate 发行版中)。

我有类似的问题,我使用了这种方法并且它有效。从同一版本的发行版中获取所有依赖的 jar 是最好的主意。如果您发现任何错误,您可以从日志中找到它并继续将 jar 放入类路径中

于 2011-10-30T05:25:07.813 回答
1

我认为这是与提供程序类有关的问题。该元素包含用于指定供应商特定设置的嵌套元素。供应商是指指定为提供者的类。因此,提供者属性齐头并进。我在使用 Hibernate 和 PostgreSQL 时遇到了同样的问题

所以对于提供者

   <provider>org.hibernate.ejb.HibernatePersistence</provider>

我必须在 persistence.xml 中提供以下内容

   <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
   <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/reverseepg-foxtel"/>
   <property name="hibernate.connection.username" value="postgres"/>
   <property name="hibernate.connection.password" value="password"/>

   If I provide below properties it doesn't work

   <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
   <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/reverseepg-foxtel"/>
   <property name="javax.persistence.jdbc.user" value="postgres"/>
   <property name="javax.persistence.jdbc.password" value="password"/>
于 2013-01-08T13:11:16.703 回答