Java 为什么Hibernate忽略my persistence.xml中的JPA2标准化属性?

Java 为什么Hibernate忽略my persistence.xml中的JPA2标准化属性?,java,hibernate,spring,jpa,jpa-2.0,Java,Hibernate,Spring,Jpa,Jpa 2.0,我使用Spring3.0.2、Hibernate3.5.1、JPA2和Derby在Tomcat中运行了一个非常简单的web应用程序。我在persistence.xml中定义了我的所有数据库连接,并仅使用Spring进行依赖注入。我使用嵌入式Derby作为我的数据库 当我以经典的Hibernate方式在persistence.xml中定义驱动程序和url属性时,一切正常,如下所示: <property name="hibernate.connection.driver_class" valu

我使用Spring3.0.2、Hibernate3.5.1、JPA2和Derby在Tomcat中运行了一个非常简单的web应用程序。我在
persistence.xml
中定义了我的所有数据库连接,并仅使用Spring进行依赖注入。我使用嵌入式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"/>
有人知道为什么会失败吗

注意:我已经复制了javax。。。属性字符串直接来自Hibernate参考文档,因此键入错误的可能性极低。

无法复制(但我没有使用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>

org.hibernate

答案似乎是这是一个Spring问题,可能是由于使用了
LocalContainerEntityManagerFactoryBean
。我使用Spring启用了
@PersistenceContext
注释的使用,而不是以标准JavaSE方式手动初始化EntityManager。当我将use
@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>

如果您不使用Maven依赖项并且只使用eclipse类路径设置,那么您只需要将上述jar文件放在hibernate 3.5及更高版本的类路径中

1) 冬眠核心罐

2) hibernate注释jar

3) jboss日志jar

4) hibernate实体管理器jar

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


我也遇到过类似的问题,我使用了这种方法,效果很好。从同一版本的发行版获取所有Dependent jar是最好的主意。如果您发现任何错误,您可以从日志中找到它,并继续将jar放入类路径中,我认为这是与provider类相关的问题。元素包含用于指定供应商特定设置的嵌套元素。供应商是指指定为提供者的类。因此,提供者和属性是齐头并进的。我在Hibernate和PostgreSQL中也遇到了同样的问题

对于提供者来说也是如此

   <provider>org.hibernate.ejb.HibernatePersistence</provider>
org.hibernate.ejb.HibernatePersistence
我必须在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"/>

如果我提供以下属性,它将不起作用

感谢您的独立验证,这让我找到了回答问题的正确途径。Hibernate 4.1和Oracle也是如此。指定特定于hibernate的属性起作用,JPA属性似乎被忽略。没有心情去追查。。。
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <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"/>