Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Hibernate Jpa and Spring

  1. #1
    Join Date
    Sep 2008
    Posts
    6

    Default Hibernate Jpa and Spring

    Hi everyone!

    First of all, sorry for my english, I'm french.

    I did Mark Fisher's tutorial 'Getting Started With JPA in Spring 2.0' without any problem.

    I tried to do the same thing with the hibernate implementation of JPA and that doesn't work anymore.

    I only changed the libraries and the application-context.xml file :

    Code:
       <bean id="restaurantDao" class="blog.jpa.dao.JpaRestaurantDao">
          <property name="entityManagerFactory" ref="entityManagerFactory"/>
       </bean>
    
       <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
          <property name="dataSource" ref="dataSource"/>
          <property name="persistenceUnitName" value="Spring" />
          <property name="jpaVendorAdapter">
             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
             	<property name="database" value="MYSQL"/>
                <property name="showSql" value="true"/>
                <property name="generateDdl" value="true"/>
                <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
             </bean>
          </property>
       </bean>
    
       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
          <property name="url" value="jdbc:mysql://localhost/test"/>
          <property name="username" value="Beegees"/>
          <property name="password" value="***"/>
       </bean>
       
       <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
       
    
       <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
          <property name="entityManagerFactory" ref="entityManagerFactory"/>
          <property name="dataSource" ref="dataSource"/>
       </bean>
    
    </beans>
    Here is my persistence.xml :
    Code:
        <persistence-unit name="Spring" transaction-type="RESOURCE_LOCAL"/>
    And when i run the Junit tests, I get this error :
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'restaurantDao' while setting bean property 'restaurantDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restaurantDao' defined in ServletContext resource [/WEB-INF/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 ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(Ljavax/persistence/spi/PersistenceUnitInfo;Ljava/util/Map;)Ljavax/persistence/EntityManagerFactory;
    If you want the complete trace, I can post it later.

    Could you help me please?
    Thanks in advance

    Beegees

  2. #2
    Join Date
    Sep 2008
    Location
    Clermont-Ferrand
    Posts
    37

    Default

    Hi!
    I'm using Hibernate JPA and Spring.
    First of all I don't create a datasource in xml file. It's automatically done with you persistence.xml. Here is my persistence.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence>
    	<persistence-unit name="test-data" transaction-type="RESOURCE_LOCAL">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>
    		<properties>
    			<property name="hibernate.archive.autodetection" value="class, hbm" />
    			<property name="hibernate.show_sql" value="false" />
    			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
    			<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/ldlcdemo" />
    			<property name="hibernate.connection.username" value="test" />
    			<property name="hibernate.connection.password" value="test" />
    			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    			<property name="hibernate.hbm2ddl.auto" value="validate" />
    			<property name="hibernate.format_sql" value="true" />
    		</properties>
    	</persistence-unit>
    </persistence>
    And my Spring config:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans>
    	<!-- Read META-INF/persistence.xml in classpath -->
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="test-data" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="showSql" value="true" />
    			</bean>
    		</property>
    	</bean>
    	
    	<!-- Transaction Manager -->
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    	
    	<!-- Enables interpretation of the @PersistenceUnit/@PersistenceContext annotations providing convenient
           access to EntityManagerFactory/EntityManager -->
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
    		<property name="defaultPersistenceUnitName" value="test-data" />
    	</bean>
    </beans>
    Ask me if you have any question.
    Mickael
    :::::::::::::::::::::::::::::::::::::::::::::::
    Mickael Gervais
    Agaetis

    Site : www.agaetis.fr
    :::::::::::::::::::::::::::::::::::::::::::::::

  3. #3
    Join Date
    Sep 2008
    Posts
    6

    Default

    Thanks!

    I'll try it in few hours.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Check your classpath and check the correct version of the jars. You probably have an issue there.

    Quote Originally Posted by mgetvais
    First of all I don't create a datasource in xml file. It's automatically done with you persistence.xml. Here is my persistence.xml:
    I would configure it in the xml. That makes it quite easy to swap for testing/environment purpose. When defined in the persistence.xml it is quite hard. Also I like to be in full control and not be delivered to the mercy of Hibernate creating/constructing the correct datasource. I want to be able to access the full C3P0/DBCP capability not just the once hibernate allows.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Sep 2008
    Posts
    6

    Default

    Here are my jars and their versions :
    _commons-logging.jar (1.1.1)
    _hibernate3.jar (3.3.0 SP1)
    _hibernate-entitymanager.jar (3.4.0 GA)
    _spring.jar (spring framework 2.5.5)
    _spring-jpa.jar (spring framework 2.5.5)
    _spring-mock.jar (spring framework 2.5.5)
    _junit.jar (4.4)
    _log4j.jar (1.2.15)
    _mysql-connector.jar (5.1.6)
    _toplink-essentials.jar (2.1 b48)

    Are some jars missing? Are they all compatible?

    Thx.


    I just tried mgervais@agaetis.fr's solution and I have a new Error

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restaurantDao' 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.NoClassDefFoundError: org/jboss/util/file/ArchiveBrowser$Filter
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
    	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:585)
    	at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:229)
    	at junit.framework.TestResult$1.protect(TestResult.java:110)
    	at junit.framework.TestResult.runProtected(TestResult.java:128)
    	at junit.framework.TestResult.run(TestResult.java:113)
    	at junit.framework.TestCase.run(TestCase.java:124)
    	at junit.framework.TestSuite.runTest(TestSuite.java:232)
    	at junit.framework.TestSuite.run(TestSuite.java:227)
    	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    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.NoClassDefFoundError: org/jboss/util/file/ArchiveBrowser$Filter
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1337)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
    	... 33 more
    Caused by: java.lang.NoClassDefFoundError: org/jboss/util/file/ArchiveBrowser$Filter
    	at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:72)
    	at org.springframework.orm.jpa.LocalEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalEntityManagerFactoryBean.java:81)
    	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:251)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1368)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1334)
    	... 43 more

    Beegees
    Last edited by beegees; Sep 9th, 2008 at 06:38 AM.

  6. #6
    Join Date
    Sep 2008
    Location
    Clermont-Ferrand
    Posts
    37

    Default

    You should fix the class not found exception. Apparently you reference some JBoss classes directly in the code.
    :::::::::::::::::::::::::::::::::::::::::::::::
    Mickael Gervais
    Agaetis

    Site : www.agaetis.fr
    :::::::::::::::::::::::::::::::::::::::::::::::

  7. #7
    Join Date
    Sep 2008
    Posts
    6

    Default

    I don't reference any Jboss class in my application. I have no compilation issue.

    I tried with spring 2.0 (this time i used maven to manage libraries) with my application-context.xml and the error is the same:

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restaurantDao' 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.AbstractMethodError: org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(Ljavax/persistence/spi/PersistenceUnitInfo;Ljava/util/Map;)Ljavax/persistence/EntityManagerFactory;
    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.AbstractMethodError: org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(Ljavax/persistence/spi/PersistenceUnitInfo;Ljava/util/Map;)Ljavax/persistence/EntityManagerFactory;
    Caused by: java.lang.AbstractMethodError: org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(Ljavax/persistence/spi/PersistenceUnitInfo;Ljava/util/Map;)Ljavax/persistence/EntityManagerFactory;
    	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:1201)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1171)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:425)
    	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:1099)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:861)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:421)
    	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.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:287)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
    	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:585)
    	at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:229)
    	at junit.framework.TestResult$1.protect(TestResult.java:106)
    	at junit.framework.TestResult.runProtected(TestResult.java:124)
    	at junit.framework.TestResult.run(TestResult.java:109)
    	at junit.framework.TestCase.run(TestCase.java:118)
    	at junit.framework.TestSuite.runTest(TestSuite.java:208)
    	at junit.framework.TestSuite.run(TestSuite.java:203)
    	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    Is it LocalContainerEntityMangerFactoryBean or LocalEntityManagerFactoryBean in the application-context.xml?

  8. #8
    Join Date
    Sep 2008
    Location
    Clermont-Ferrand
    Posts
    37

    Default

    Can you post your pom.xml. I've encountered some problems with Maven and Spring.
    Mickael
    :::::::::::::::::::::::::::::::::::::::::::::::
    Mickael Gervais
    Agaetis

    Site : www.agaetis.fr
    :::::::::::::::::::::::::::::::::::::::::::::::

  9. #9
    Join Date
    Sep 2008
    Posts
    6

    Default

    Code:
    <modelVersion>4.0.0</modelVersion>
      <groupId>spring2</groupId>
      <artifactId>spring2</artifactId>
      <version>0.0.1</version>
      <dependencies>
        <dependency>
          <groupId>hibernate</groupId>
          <artifactId>hibernate</artifactId>
          <version>3.1rc2</version>
        </dependency>
        <dependency>
          <groupId>hibernate</groupId>
          <artifactId>hibernate-annotations</artifactId>
          <version>3.1beta4</version>
        </dependency>
        <dependency>
          <groupId>hibernate</groupId>
          <artifactId>hibernate-entitymanager</artifactId>
          <version>3.1beta1</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring</artifactId>
          <version>2.0.6</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jpa</artifactId>
          <version>2.0.6</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-mock</artifactId>
          <version>2.0.6</version>
        </dependency>
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.0.5</version>
        </dependency>
      </dependencies>

  10. #10
    Join Date
    Sep 2008
    Location
    Clermont-Ferrand
    Posts
    37

    Default

    Di you try with this version of Hibernate?
    Code:
    <!-- Hibernate -->
    			<dependency>
    				<groupId>org.hibernate</groupId>
    				<artifactId>hibernate</artifactId>
    				<version>3.2.6.ga</version>
    				<type>jar</type>
    			</dependency>
    			<dependency>
    				<groupId>org.hibernate</groupId>
    				<artifactId>hibernate-annotations</artifactId>
    				<version>3.3.1.GA</version>
    				<type>jar</type>
    			</dependency>
    			<dependency>
    				<groupId>org.hibernate</groupId>
    				<artifactId>hibernate-entitymanager</artifactId>
    				<version>3.3.2.GA</version>
    				<type>jar</type>
    			</dependency>
    :::::::::::::::::::::::::::::::::::::::::::::::
    Mickael Gervais
    Agaetis

    Site : www.agaetis.fr
    :::::::::::::::::::::::::::::::::::::::::::::::

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •