开发者

Why does Hibernate ignore the JPA2 standardized properties in my persistence.xml?

开发者 https://www.devze.com 2022-12-27 01:28 出处:网络
I have an extremely simple web application running in Tomcat using Spring 3.0.2, Hibernate 3.5.1, JPA 2, and Derby.I am defining all of my database connectivity in persistence.xml and merely using Spr

I have an extremely simple web application running in Tomcat using Spring 3.0.2, Hibernate 3.5.1, JPA 2, and Derby. I am defining all of my database connectivity in persistence.xml and merely using Spring for dependency injection. I am using embedded Derby as my database.

Everything works correctly when I define the driver and url properties in persistence.xml in the classic Hibernate manner as thus:

<property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="hibernate.connection.url" value="jdbc:derby:webdb;create=true"/>

The problems occur when I switch my configuration to the JPA2 standardized properties as thus:

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

When using the JPA2 property keys, the application bails hard with the following exception:

java.lang.UnsupportedOperationException: The user must supply a JDBC connection
开发者_开发知识库

Does anyone know why this is failing?

NOTE: I have copied the javax... property strings straight from the Hibernate reference documentation, so a typo is extremely unlikely.


The answer appears to be that this is a Spring issue, likely stemming from the use of the LocalContainerEntityManagerFactoryBean. I was using Spring to enable the use of the @PersistenceContext annotation rather than manually initializing the EntityManager in the standard Java SE way. When I replaced the use @PersistenceContext with Persistence.createEntityManagerFactory("WebApp").createEntityManager(); (and commented the EntityManager stuff out of my Spring config), everything worked as expected.

For reference, this was the Spring configuration I was using:

<?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>


If you are not using Maven dependency and using only eclipse classpath settings, you just need to put the above jar files in classpath from hibernate 3.5 and above

1) hibernate core Jar

2) hibernate annotations jar

3) jboss logging jar

4) hibernate entity manager jar

JPA API 2. jar (its included in hibernate distribution).

I had similar issues and I used this approach and it worked. Getting all the depedent jar from the same version of the distribution is the best idea. If you find any error you can find it from the logs and go on putting the jar in the classpath


Cannot reproduce (I'm not using Spring though). Here is my persistence.xml, and it works for me:

<?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>

Just in case, here are the maven dependencies I'm using:

<!-- 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>

My classpath below:

Why does Hibernate ignore the JPA2 standardized properties in my persistence.xml?


I think this is a problem related to the provider class. The element contains nested elements used to specify vendor-specific settings. By Vendor I mean the class specified as the provider. Thus provider and properties go hand in hand. I was facing the same issue with Hibernate and PostgreSQL

So for the provider

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

I must provide following in 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"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消