title: Spring Data - NoSuchMethodError JpaRepositoryFactoryBean.setMappingContext()
body: <p>I wrote a DAL following <a href="http://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html" rel="nofollow">this</a> guide. In particular this is my DALConfig.java configuration file</p>
  
  <pre><code>package my.pack;
  
  import java.util.Properties;
  import javax.annotation.Resource;
  import javax.sql.DataSource;
  import org.apache.commons.dbcp2.BasicDataSource;
  import org.apache.commons.dbcp2.BasicDataSourceFactory;
  import org.hibernate.ejb.HibernatePersistence;
  import org.hibernate.jpa.HibernatePersistenceProvider;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.context.annotation.PropertySources;
  import org.springframework.core.env.Environment;
  import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
  import org.springframework.data.repository.Repository;
  import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
  import org.springframework.orm.jpa.JpaTransactionManager;
  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.transaction.PlatformTransactionManager;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  @Configuration
  @ComponentScan(basePackages = { "my.pack" })
  @PropertySource("classpath:dbconnection.properties")
  @EnableJpaRepositories("my.pack.repository")
  @EnableTransactionManagement
  public class DALConfig {
  
  
      private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver_class";  
      private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";  
      private static final String PROPERTY_NAME_DATABASE_URL = "db.url";  
      private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";  
  
      private static final String PROPERTY_NAME_POOL_INITIAL_SIZE = "pool.initialsize";
      private static final String PROPERTY_NAME_POOL_MAX_IDLE = "pool.maxidle";
      private static final String PROPERTY_NAME_DAL_CLASSES_PACKAGE = "entities.packages_to_scan";  
      private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
      private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.showsql";
      private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
  
  
      @Resource
      private Environment environment;  
  
      @Bean
      public DataSource dataSource()
      {
  
          Properties props = new Properties();
  
  
          props.put("driverClassName", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
          props.put("url", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
          props.put("username", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
          props.put("password", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
          props.put("initialSize", environment.getRequiredProperty(PROPERTY_NAME_POOL_INITIAL_SIZE));
          props.put("maxIdle", environment.getRequiredProperty(PROPERTY_NAME_POOL_MAX_IDLE));
  
          BasicDataSource bds = null;
          try {
              bds = BasicDataSourceFactory.createDataSource(props);
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          return bds;
  
      }
  
      @Bean
      public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor()
      {
          PersistenceExceptionTranslationPostProcessor b = new PersistenceExceptionTranslationPostProcessor();
  
          return b;
      }   
  
      @Bean 
      public HibernateExceptionTranslator hibernateExceptionTranslator(){ 
        return new HibernateExceptionTranslator(); 
      }
  
      @Bean
      public PlatformTransactionManager transactionManager() throws ClassNotFoundException {
  
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
  
          return transactionManager;
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException {
  
          LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
  
          entityManagerFactoryBean.setDataSource(dataSource());
          entityManagerFactoryBean.setPackagesToScan(environment.getRequiredProperty(PROPERTY_NAME_DAL_CLASSES_PACKAGE));
          entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
  
          Properties jpaProterties = new Properties();
          jpaProterties.put(PROPERTY_NAME_HIBERNATE_DIALECT, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
          jpaProterties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
          jpaProterties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
  
          entityManagerFactoryBean.setJpaProperties(jpaProterties);
  
          return entityManagerFactoryBean;
      }
  
  }
  </code></pre>
  
  <p>This is the Repository I wrote</p>
  
  <pre><code>package my.pack.repository;
  
  import my.pack.entity.User;
  
  import org.springframework.data.repository.CrudRepository;
  import org.springframework.stereotype.Repository;
  
  @Repository
  public interface IUserRepository extends CrudRepository&lt;User, String&gt;{
  
  }
  </code></pre>
  
  <p>Finally this is the test I run</p>
  
  <pre><code>package my.pack.tests;
  
  import static org.junit.Assert.assertTrue;
  import futureservice.UserService;
  import my.pack.DALConfig;
  import my.pack.entity.User;
  
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  
  @ContextConfiguration(classes = { DALConfig.class})
  @RunWith(SpringJUnit4ClassRunner.class)
  public class DALTest {
  
      @Autowired
      UserService userService;
  
      @Test
      public void testGetUser() {
  
          User user = null;
          user = userService.findOne("mrossi");
          assertTrue(null != user);
  
      }
  }
  </code></pre>
  
  <p>When I run the test I get the following exception stack and the test fails.</p>
  
  <pre><code>...
  Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
  PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'entityManager' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.setMappingContext(Lorg/springframework/data/mapping/context/MappingContext;)V
      at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:108)
      at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:62)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1489)
      ... 41 more
  </code></pre>
  
  <p>It seems like the JpaRepositoryFactoryBean misses the <a href="http://docs.spring.io/spring-data/data-commons/docs/current/api/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupport.html?is-external=true#setMappingContext%28org.springframework.data.mapping.context.MappingContext%29" rel="nofollow">setMappingContext</a> method but as you can see from the documentation the method is supported. I'm using the <code>1.5.1.RELEASE</code> version of <code>spring-data-jpa</code> artifact. </p>
  
  <p>What can be the issue?</p>
  
  <p>Thank you</p>