Announcement Announcement Module
Collapse
No announcement yet.
HibernatePersistence error trying to use JPA Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HibernatePersistence error trying to use JPA

    Hello,

    I'm a bit stuck. I have read a bunch of blogs/wikis/manuals and tried many different combinations, but in the end I get to the same error (actually with the adapter I was able to get this far)

    The error is:

    Code:
    18:23:46,628 INFO  [XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [applicationContext.xml]
    18:23:50,742 INFO  [LocalContainerEntityManagerFactoryBean] Building JPA container EntityManagerFactory for persistence unit 'lingraph-persistence'
    18:23:53,995 ERROR [DefaultFaultHandler] Fault occurred!
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderRepository' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IncompatibleClassChangeError: Class org.hibernate.ejb.HibernatePersistence does not implement the requested interface javax.persistence.spi.PersistenceProvider
    Caused by: 
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IncompatibleClassChangeError: Class org.hibernate.ejb.HibernatePersistence does not implement the requested interface javax.persistence.spi.PersistenceProvider
    Caused by: 
    java.lang.IncompatibleClassChangeError: Class org.hibernate.ejb.HibernatePersistence does not implement the requested interface javax.persistence.spi.PersistenceProvider
    	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:218)
    	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:251)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1202)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1172)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:428)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:261)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:109)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1100)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:862)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:424)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
    	at test.numbersinsight.lingraph.order.webservices.CardOrderUpdateServiceTestImpl.getNewOrders(CardOrderUpdateServiceTestImpl.java:34)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.codehaus.xfire.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:59)
    I checked the jars all around, and only hibernate-entitymanager.jar has this class. This is the one coming form the Jboss 4.2 implementation. I know this works because this same project is using JPA with EJB3 using Seam to access it and it works fine. I decompiled the class and it is in fact implementing the interface. Here's the header of that class taken right from the jar:

    Code:
    package org.hibernate.ejb;
    
    import java.util.Map;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.spi.PersistenceProvider;
    import javax.persistence.spi.PersistenceUnitInfo;
    
    // Referenced classes of package org.hibernate.ejb:
    //            Ejb3Configuration
    
    public class HibernatePersistence
        implements PersistenceProvider
    {
    
        public static final String PROVIDER = "javax.persistence.provider";
        public static final String TRANSACTION_TYPE = "javax.persistence.transactionType";
        public static final String JTA_DATASOURCE = "javax.persistence.jtaDataSource";
        public static final String NON_JTA_DATASOURCE = "javax.persistence.nonJtaDataSource";
        public static final String AUTODETECTION = "hibernate.archive.autodetection";
    Here's my persistence.xml (Which Spring seems to be reading just fine because it picks up the persistent unit name):

    Code:
    <?xml version="1.0"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
      <persistence-unit name="lingraph-persistence">
        <provider>
          org.hibernate.ejb.HibernatePersistence
        </provider>
        <jta-data-source>java:/jdbc/lingraphorder</jta-data-source>
        <properties>
          <property name="hibernate.dialect" 
              value="org.hibernate.dialect.MySQLDialect"/>
          <property name="hibernate.hbm2ddl.auto" 
              value="none"/>
          <property name="hibernate.show_sql" value="true"/>
        </properties>
      </persistence-unit>
    </persistence>
    Here's the applicationContext.xml:

    Code:
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        	<property name="dataSource" ref="lingraphDataSource"/>    	
        	<property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
               <property name="showSql" value="true" />
               <property name="generateDdl" value="true" />
               <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
            </bean>
         </property>  		
        		
       	</bean>
        <bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    		<property name="dataSource" ref="lingraphDataSource"/>
    	</bean>
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
            
            
        <bean id="lingraphDataSource" class="org.apache.commons.dbcp.datasources.SharedPoolDataSource">
    		<property name="dataSourceName" value="java:jdbc/lingraphorder"/>		
    	</bean>
    	   
        <bean id="orderRepository" class="com.numbersinsight.lingraph.order.dao.OrderRepositoryImpl">
        	<property name="entityManagerFactory" ref="entityManagerFactory"/>
       	</bean>
            
        <tx:annotation-driven transaction-manager="transactionManager"/>
    And finally the little piece of code where I try to access it:

    Code:
    BeanFactory factory = new XmlBeanFactory( new ClassPathResource("applicationContext.xml") );
    		
    		//This will load the scheduler and start it
    		OrderRepository repo = (OrderRepository)factory.getBean("orderRepository");
    		Collection newOrders = repo.getOrdersByStatus(Constants.ORDER_STATUS_NEW);
    I'm hoping this error is showing up because I'm missing something else because it doesn't make much sense to me. Has anyone seen something similar?

    Thanks,

    Jose

  • #2
    I'd check to see if you have more than one hibernate jar in your classpath, with one of them being old.

    Comment


    • #3
      I checked the jars for the class and there is only one of them. I even went ahead and modified the jar to remove that class 'HibernatePersistence' and then started getting no class def errors. This jars are the ones coming with JBoss 4.2.0 (Which Im not sure if I said that before.

      Comment


      • #4
        I went ahead and created a JUnit of that code to take the container out of the equation. I made the classpath point to the same jars (the ones that ship with the JBoss 4.2.0 server) and guess what, it works!!

        So it's something specific to the JBoss container that is cauing the issue.

        Since I'm also using Seam inside the application I'm going to try it without Seam to see if I can narrow down where the conflict is.

        Comment


        • #5
          It's definetely realted to running in a JBoss AS. I created a simple web and ejb project and I started getting the Incompatible error again.

          Anyone know of any issues between Jboss and Spring? In the Spring distribution with dependencies I notices a jar file call jboss-archive-browsing.jar Not sure what it's for.

          Comment


          • #6
            I'm using JBoss 4.2 which comes with all the Hibernate support for JPA. With JBoss installed, you can see in the "...\server\default\lib" the following Hiberante jars: hibernate3.jar, hibernate-entitymanager.jar, hibernate-annotations.jar.

            I'm getting other erros with Hiberanate/JBoss but there are beyond finding the correct version of the "org.hibernate.ejb.HibernatePersistence" class (at least that's what I think !).

            Comment


            • #7
              There is a lib called ejb3-persistence.jar which is incompatible with your persistence.jar.

              Comment


              • #8
                Originally posted by scratchback View Post
                There is a lib called ejb3-persistence.jar which is incompatible with your persistence.jar.
                So how to solve this problem?

                Comment


                • #9
                  kengkaj, remove all your persistence jar file in your package and use the jboss one.

                  Comment


                  • #10
                    Originally posted by scratchback View Post
                    kengkaj, remove all your persistence jar file in your package and use the jboss one.
                    Many thanks scratchback. This can cause a problem if I want to change JPA version in future. But for now this can solve the immediate problem.

                    Comment


                    • #11
                      Stuck with jpa and hibernate in jboss 5.1.0

                      Hi i am stuck with hibernate persistence error using jboss
                      I am using jboss 5.1.0GA with hibernate3 and jpa and spring2.5

                      This is my persistence.xml

                      <persistence version="1.0" 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_1_0.xsd">
                      <persistence-unit name="SpringDemoPU" transaction-type="RESOURCE_LOCAL">
                      <non-jta-data-source>java:/SpringDemoDS</non-jta-data-source>
                      <properties>
                      <property name="hibernate.show_sql" value="false"/>
                      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
                      <property name="hibernate.hbm2ddl.auto" value="update"/>

                      <!-- hibernate performance -->
                      <!-- property name="hibernate.cglib.use_reflection_optimizer" value="true" / -->
                      <property name="hibernate.bytecode.use_reflection_optimizer" value="true"/>
                      <property name="hibernate.cache.use_second_level_cache" value="true"/>
                      <property name="hibernate.connection.pool_size" value="20"/>
                      <property name="hibernate.cache.use_query_cache" value="false"/>
                      <!-- <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"></property>-->
                      </properties>
                      </persistence-unit>
                      </persistence>

                      and my login-servlet.xml file is

                      <beans xmlns="http://www.springframework.org/schema/beans"
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/p"
                      xmlns:context="http://www.springframework.org/schema/context"
                      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
                      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.5.xsd
                      http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-2.5.xsd
                      http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-2.5.xsd
                      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
                      default-autowire="byName">

                      <context:component-scan base-package="com" />
                      <bean class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping"/>
                      <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter"/>
                      <bean class="org.springframework.context.annotation.Comm onAnnotationBeanPostProcessor" />

                      <bean name="/userLogin" class="com.example.LoginController"/>
                      <bean id="viewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
                      <property name="viewClass"><value>org.springframework.web.se rvlet.view.JstlView</value></property>
                      <property name="prefix"><value>/</value></property>
                      <property name="suffix"><value>.jsp</value></property>
                      </bean>

                      <!-- bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
                      <property name="driverClassName"><value>com.mysql.jdbc.Drive r</value></property>
                      <property name="url"><value>jdbc:mysql://localhost:3306/techfaq</value></property>
                      <property name="username"><value>root</value></property>
                      <property name="password"><value>pingpong123</value></property>
                      </bean> -->

                      <!--bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
                      <property name="dataSource"><ref bean="myDataSource"/></property>
                      <property name="mappingResources">
                      <list>
                      <value>dao/user.hbm.xml</value>
                      </list>
                      </property>
                      <property name="hibernateProperties">
                      <value>
                      hibernate.dialect=org.hibernate.dialect.MySQLDiale ct
                      </value>
                      </property>
                      </bean-->

                      <bean id="userdao" class="com.dao.impl.UserDAOImpl">
                      <!-- property name="sessionFactory"><ref bean="mySessionFactory"/></property> -->
                      </bean>

                      <bean id="dataSource"
                      class="org.springframework.jdbc.datasource.DriverM anagerDataSource"
                      p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/techfaq" p:username="root" password="pingpong123" />




                      <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->

                      <bean id="entityManagerFactory"
                      class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
                      <property name="persistenceUnitName" value="SpringDemoPU" />
                      <property name="dataSource" ref="dataSource" />
                      <property name="jpaVendorAdapter">
                      <bean class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter">
                      <property name="showSql" value="false" />
                      <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
                      </bean>
                      </property>
                      </bean>

                      <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
                      <property name="entityManagerFactory" ref="entityManagerFactory" />
                      <property name="dataSource" ref="dataSource" />
                      </bean>
                      </beans>

                      When i deploy my war file in Jboss it gives me exception

                      14:46:34,558 ERROR [AbstractKernelController] Error installing to Start: name=persistence.unit:unitName=#SpringDemoPU state=Create
                      javax.persistence.PersistenceException: [PersistenceUnit: SpringDemoPU] Unable to build EntityManagerFactory
                      at org.hibernate.ejb.Ejb3Configuration.buildEntityMan agerFactory(Ejb3Configuration.java:677)
                      at org.hibernate.ejb.HibernatePersistence.createConta inerEntityManagerFactory(HibernatePersistence.java :132)
                      at org.jboss.jpa.deployment.PersistenceUnitDeployment .start(PersistenceUnitDeployment.java:301)
                      at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
                      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
                      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
                      at java.lang.reflect.Method.invoke(Unknown Source)
                      at org.jboss.reflect.plugins.introspection.Reflection Utils.invoke(ReflectionUtils.java:59)
                      at org.jboss.reflect.plugins.introspection.ReflectMet hodInfoImpl.invoke(ReflectMethodInfoImpl.java:150)
                      at org.jboss.joinpoint.plugins.BasicMethodJoinPoint.d ispatch(BasicMethodJoinPoint.java:66)
                      at org.jboss.kernel.plugins.dependency.KernelControll erContextAction$JoinpointDispatchWrapper.execute(K ernelControllerContextAction.java:241)
                      at org.jboss.kernel.plugins.dependency.ExecutionWrapp er.execute(ExecutionWrapper.java:47)
                      at org.jboss.kernel.plugins.dependency.KernelControll erContextAction.dispatchExecutionWrapper(KernelCon trollerContextAction.java:109)
                      at org.jboss.kernel.plugins.dependency.KernelControll erContextAction.dispatchJoinPoint(KernelController ContextAction.java:70)
                      at org.jboss.kernel.plugins.dependency.LifecycleActio n.installActionInternal(LifecycleAction.java:221)
                      at org.jboss.kernel.plugins.dependency.InstallsAwareA ction.installAction(InstallsAwareAction.java:54)
                      at org.jboss.kernel.plugins.dependency.InstallsAwareA ction.installAction(InstallsAwareAction.java:42)

                      Caused by: org.hibernate.HibernateException: Could not instantiate dialect class
                      at org.hibernate.dialect.DialectFactory.buildDialect( DialectFactory.java:107)
                      at org.hibernate.dialect.DialectFactory.buildDialect( DialectFactory.java:65)
                      at org.hibernate.cfg.SettingsFactory.determineDialect (SettingsFactory.java:460)
                      at org.hibernate.cfg.SettingsFactory.buildSettings(Se ttingsFactory.java:155)
                      at org.hibernate.cfg.Configuration.buildSettings(Conf iguration.java:2101)
                      at org.hibernate.cfg.Configuration.buildSessionFactor y(Configuration.java:1325)
                      at org.hibernate.cfg.AnnotationConfiguration.buildSes sionFactory(AnnotationConfiguration.java:867)
                      at org.hibernate.ejb.Ejb3Configuration.buildEntityMan agerFactory(Ejb3Configuration.java:669)
                      ... 61 more
                      Caused by: java.lang.ClassCastException: org.hibernate.dialect.MySQLDialect cannot be cast to org.hibernate.dialect.Dialect
                      at org.hibernate.dialect.DialectFactory.buildDialect( DialectFactory.java:101)
                      ... 68 more


                      I dont why this error is coming i have tried several forum replies but didnt get any success
                      Please help me to come out from this

                      Comment

                      Working...
                      X