views:

773

answers:

2

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.

+2  A: 

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:

alt text

Pascal Thivent
Thanks for the independent verification, it put me on the right path to answer my question.
Ophidian
+2  A: 

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"&gt;

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