Announcement Announcement Module
Collapse
No announcement yet.
spring-data-jpa Repository @Autowired throw exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring-data-jpa Repository @Autowired throw exception

    I am trying to using spring-data-jpa version 1.0.0.M2. When I am trying to @Autowired Repository I am getting BeanCreationException (Nested exception EntityManagerFactoryImpl.getMetamodel()).

    Below is code snippet (Entity, Repository, Controller and applicationContext.xml):

    /**
    * POJO class for user login information.
    * @author SKumar
    */
    @Table(name = "USER_LOGIN")
    @Entity
    public class UserLogin {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "USER_ID")
    private Long id;
    .
    .
    .
    }


    package com.campaignify.repository;

    import org.springframework.data.jpa.repository.JpaReposit ory;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Transac tional;

    import com.campaignify.entity.UserLogin;
    @Transactional
    @Repository
    public interface UserLoginRepository extends JpaRepository<UserLogin, Long>{
    }

    @Repository
    @Controller
    @RequestMapping("/user")
    @Transactional()
    public class UserManagementController {

    @Autowired
    UserLoginRepository userRepository;

    @RequestMapping(method = RequestMethod.POST)
    @Transactional(readOnly = false)
    public View saveUser(@RequestBody UserLogin user) {
    View returnView = new RedirectView("/user/");
    try {
    userRepository.save(user);
    returnView = new RedirectView("/user/" + user.getId());
    } catch (Exception e) {
    // TODO Exception handling here.
    e.printStackTrace();
    }
    return returnView;
    }
    }

    <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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns="http://www.springframework.org/schema/p"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schem...ontext-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schem...ing-tx-3.0.xsd
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schem...g-jdbc-3.0.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.campaignify" use-default-filters="false"/>
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor"/>

    <bean id="campaignifyDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sampleDB"/>
    <property name="user" value="root"/>
    <property name="password" value="password"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean" >
    <property name="dataSource" ref="campaignifyDataSource" />
    <property name="persistenceUnitName" value="campaignSample"></property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean><bean id="dataFirstTransactionManager"
    class="org.springframework.orm.jpa.JpaTransactionM anager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <qualifier value="campaignManagement" />
    <property name="jpaDialect">
    <bean class="org.springframework.orm.jpa.vendor.Hibernat eJpaDialect" />
    </property>
    </bean>
    <jpa:repositories base-package="com.campaignify.repository" />
    </beans>

    Error while running:

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'userLoginRepository': FactoryBean threw exception on object creation; nested exception is java.lang.AbstractMethodError: org.hibernate.ejb.EntityManagerFactoryImpl.getMeta model()Ljavax/persistence/metamodel/Metamodel;</pre></p><h3>Caused by:</h3><pre>javax.servlet.UnavailableException: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'userLoginRepository': FactoryBean threw exception on object creation; nested exception is java.lang.AbstractMethodError: org.hibernate.ejb.EntityManagerFactoryImpl.getMeta model()Ljavax/persistence/metamodel/Metamodel;
    at org.mortbay.jetty.servlet.ServletHolder.makeUnavai lable(ServletHolder.java:406)
    at org.mortbay.jetty.servlet.ServletHolder.initServle t(ServletHolder.java:449)
    at org.mortbay.jetty.servlet.ServletHolder.getServlet (ServletHolder.java:339)
    at org.mortbay.jetty.servlet.ServletHolder.handle(Ser vletHolder.java:478)
    at org.mortbay.jetty.servlet.ServletHandler.handle(Se rvletHandler.java:380)
    at org.mortbay.jetty.security.SecurityHandler.handle( SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(Se ssionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(Co ntextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebA ppContext.java:417)
    at org.mortbay.jetty.handler.ContextHandlerCollection .handle(ContextHandlerCollection.java:230)
    at org.mortbay.jetty.handler.HandlerCollection.handle (HandlerCollection.java:114)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(Ha ndlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(Htt pConnection.java:535)
    at org.mortbay.jetty.HttpConnection$RequestHandler.co ntent(HttpConnection.java:880)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser. java:747)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpPa rser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnec tion.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(Selec tChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run (QueuedThreadPool.java:520)

    @Autowired should have worked as I have defined repositories base package and component-scan. Spring should create proxy implementation of UserLoginRepository and inject within controller. Please help what I am missing.

    <jpa:repositories base-package="com.campaignify.repository" />
    <context:component-scan base-package="com.campaignify" use-default-filters="false"/>

  • #2
    1. Please use the CODE tag
    2. you should remove @Transactional and @Repository from UserLoginRepository
    3. Error => nested exception is java.lang.AbstractMethodError: org.hibernate.ejb.EntityManagerFactoryImpl.getMeta model()Ljavax/persistence/metamodel/Metamodel; Which hibernate version do you use? Which persistence.jar file is in your classpath?

    Comment


    • #3
      I second GWA. The exception says you're lacking metamodel related methods on the EntityManager. Make sure you're using JPA and a Hibernate version that supports JPA 2.0 (Hibernate 3.5 or higher).

      Comment


      • #4
        Point 1#: Where to use code tag? (I am currently testing sample by RESTClient)
        Point 2#: Earlier I had UserLoginRepository without @Transactional, @Repository annotation. I added with asumption that we may need separate transaction at Repository layer independent of service\controller transaction. Tried again removing annotation but no luck.
        Point 3#:
        Below are hibernate dependency:

        <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.2.GA</version>
        </dependency>
        <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.4.0.GA</version>
        </dependency>
        <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.4.0.GA</version>
        </dependency>


        Persistance: ejb3-persistence-1.0.2.GA.jar
        hibernate-jpa-2.0-api-1.0.0.Final.jar

        I can see spring-data-jpa-1.0.0.M2.pom using hibernate.version 3.6.0.Final for hibernate-entitymanager (not presently on apache Maven repository).

        Comment


        • #5
          Hibernate 3.4.0 does *not* support JPA 2.0… So you've got to use 3.5 or higher.

          Comment


          • #6
            Thanks for pointer.

            Pointing hibernate version to 3.5.1-Final solved the issue.

            Comment


            • #7
              another AbstractMethodError

              I am getting an exception from Spring Data Jpa 1.0.0.M2 like this:

              Caused by: java.lang.AbstractMethodError: org.springframework.data.repository.support.Reposi toryFactorySupport.getRepositoryBaseClass(Lorg/springframework/data/repository/support/RepositoryMetadataLjava/lang/Class;
              at org.springframework.data.repository.support.Reposi toryFactorySupport.getRepositoryInformation(Reposi toryFactorySupport.java:167)
              at org.springframework.data.repository.support.Reposi toryFactorySupport.getRepository(RepositoryFactory Support.java:128)
              at org.springframework.data.repository.support.Reposi toryFactoryBeanSupport.getObject(RepositoryFactory BeanSupport.java:107)
              at org.springframework.data.repository.support.Reposi toryFactoryBeanSupport.getObject(RepositoryFactory BeanSupport.java:36)
              at org.springframework.beans.factory.support.FactoryB eanRegistrySupport.doGetObjectFromFactoryBean(Fact oryBeanRegistrySupport.java:142)
              ... 40 more

              Here is my setup:
              <groupId>org.hibernate</groupId>
              <artifactId>hibernate-core</artifactId>
              <version>3.6.3.Final</version>
              </dependency>
              <dependency>
              <groupId>org.hibernate</groupId>
              <artifactId>hibernate-validator</artifactId>
              <version>4.1.0.Final</version>
              </dependency>
              <dependency>
              <groupId>hibernate-entitymanager</groupId>
              <artifactId>hibernate-entitymanager</artifactId>
              <version>3.3.2.GA</version>
              </dependency>
              <dependency>
              <groupId>org.hibernate</groupId>
              <artifactId>hibernate-commons-annotations</artifactId>
              <version>3.2.0.Final</version>
              </dependency>
              <dependency>
              <groupId>org.hibernate.javax.persistence</groupId>
              <artifactId>hibernate-jpa-2.0-api</artifactId>
              <version>1.0.0.Final</version>
              </dependency>

              Thanks a lot for any ideas that might help!

              Comment


              • #8
                Please use CODE tag!

                Like say in the previous reply, spring-data-jpa need a jpa2 provider.

                If you use hibernate, you need to use a v3.5+

                In your dependency you have:
                Code:
                <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>3.6.3.Final</version>
                </dependency>
                ...
                <dependency>
                <groupId>hibernate-entitymanager</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>3.3.2.GA</version>
                </dependency>
                The entitymanager dependency is the hibernate jpa integration. Here you use 3.3 version which is outdated (JPA1)

                As hibernate-core is a dependency of hibernate-entitymanager simply remove the hibernate core dependency and update entitymanager version to 3.6.3.Final.

                Comment


                • #9
                  I second GWA's answer but his hint is probably not solving your issue. It seems you have a non-suitable version of Spring Data Commons Core in your classpath. Spring Data JPA M2 needs Spring Data Commons Core in M5 (as stated by the pom.xml).

                  Cheers,
                  Ollie

                  Comment

                  Working...
                  X