Announcement Announcement Module
Collapse
No announcement yet.
Hibernate 3.6 + DataSource + JPA2 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate 3.6 + DataSource + JPA2

    Hello !
    I've been developing a application using JSF + Hibernate + Spring, guidind some examples using direct acess to the database at persistence.xml, configuring the hibernate.connection.url, hibernate.connection.username and hibernate.connection.password.

    I configured the applicationContext i was using the following code to inject the EntityManager:
    Code:
    <bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="ADDOrca-Model" />
        </bean>
    
        <context:component-scan base-package="br.uff.addlabs.orca.controller" />
    	
        <tx:annotation-driven transaction-manager="transactionManager" />
    
        <context:annotation-config />
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
    The application was working right but now, because of client company specifications, we need to acess a datasource located at the server (they use Weblogic). I'm trying to do this since few days ago using the Tomcat (the container i've been using to run application locally) but i am having problems with the injection (i think). I've changed the LocalEntityManagerFactoryBean to LocalContainerEntityManagerFactoryBean and configured some other things and since i did this the tomcat gives me an error about the version of JPA descripted in persistence.xml
    (Value '2.0' of attribute 'version' of element 'persistence' is not valid with respect to the corresponding attribute use. Attribute 'version' has a fixed value of '1.0'.)
    I tried to found any reference to JPA1 at my classpath and i didn't found anything and i don't have any clue to solve this problem. The current code:

    persistence.xml
    Code:
    <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="ADDOrca-Model" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:comp/env/jdbc/addorca</jta-data-source>
        
        <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
          <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
          <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WeblogicTransactionManagerLookup"/>
          <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
    
        </properties>
      </persistence-unit>
    </persistence>

    applicationContext.xml
    Code:
    <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"
            xmlns:p="http://www.springframework.org/schema/p"
    	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">
         <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
                <property name="resourceRef">
                        <value>false</value>
                </property>
                <property name="jndiName">
                        <value>java:comp/env/jdbc/addorca</value>
                </property>
        </bean>
        
        <bean id="jpaAdapter"
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
            p:databasePlatform="org.hibernate.dialect.Oracle10gDialect"
            />
            
        
        <bean id="entityManagerFactory" p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="ADDOrca-Model" />
            <property name="jpaProperties">
                <value>
                    hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
                    hibernate.show_sql=true
                    hibernate.transaction.manager_lookup_class=org.hibernate.transaction.WeblogicTransactionManagerLookup
                    hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
                </value>
            </property>
            <property name="loadTimeWeaver">
                <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver" />
            </property>
        </bean>
    
        <bean id="transactionManager" autowire="byName" class="org.springframework.transaction.jta.JtaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
        
        <context:component-scan base-package="br.uff.addlabs.orca.controller" />
    	
        <tx:annotation-driven transaction-manager="transactionManager" />
    
        <context:annotation-config />  
    </beans>

  • #2
    Which spring version you use and are you on a weblogic version which supports JPA2?

    Comment


    • #3
      Thanks for your reply,
      I'm using spring 2.5 and the version of Weblogic is 10.3.5 and supports JPA2 (however i'm not sure if it supports Hibernate). But before this step of trying to deploy in weblogic, i'm still in Apache Tomcat trying to deploy the project using datasource, it was working with direct connection, but now using datasource configured at the server the project is not running, i can't get a a valid configuration of applicationContext.xml and persistence.xml that makes the project run

      Comment


      • #4
        Spring 2.5 doesn't support JPA2 so that might be a/the problem. Also tomcat doesn't do anything with the persistence.xml spring reads and parses it using the JPA provider.

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          Spring 2.5 doesn't support JPA2 so that might be a/the problem. Also tomcat doesn't do anything with the persistence.xml spring reads and parses it using the JPA provider.
          Hi Marten,
          I forgot to update the topic, based on your coment i updated the version of Spring to 3.2, and I could use LocalContainerEntityManagerFactoryBean instead of LocalEntityManagerFactoryBean.

          My applicationContext
          Code:
          <?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">
          
              <bean id="entityManagerFactory"
          		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                  <property name="persistenceUnitName" value="ADDOrca-Model" />
              </bean>
          
              <context:component-scan base-package="br.uff.addlabs.orca.controller" />
          	
              <tx:annotation-driven transaction-manager="transactionManager" />
          
              <context:annotation-config />
              <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
                  <property name="entityManagerFactory" ref="entityManagerFactory" />
              </bean>
          </beans>

          persistence.xml (using resouce-local instead JTA)
          Code:
          <?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="ADDOrca-Model" transaction-type="RESOURCE_LOCAL">
              <provider>org.hibernate.ejb.HibernatePersistence</provider>
              <!--TOMCAT <jta-data-source>java:comp/env/jdbc/addorca</jta-data-source>-->
              <!--JBOSS <jta-data-source>java:/jdbc/addorca</jta-data-source>-->
              <!--WL <jta-data-source>addorcaDS</jta-data-source>-->
              <jta-data-source>java:comp/env/jdbc/addorca</jta-data-source>
              <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
              <properties>
            </persistence-unit>
          </persistence>

          Finally, to run the application in weblogic 10.3.5 the support of the client had to run a update on the server to enable support to JPA 2

          Regards !

          Comment

          Working...
          X