title: javax.persistence dependency error using spring-context
body: <p>I am developing an application using spring-data-jpa. The application launches and runs without any problem, but when I want to write unit tests using spring-context, it seems that I have a dependency conflict due to a JPA1 dependency, which seems to be brought by spring-context(provided by spring-web, spring-webmvc, spring-data-jpa). </p>
  
  <p>Here is my Test(shouldn't be relevant, I provide the code just in case):</p>
  
  <pre><code>package com.company.dashboard.core.services.archiving;  
  
  import java.util.List;  
  
  import org.junit.Before;  
  import org.junit.Test;  
  import org.junit.runner.RunWith;  
  import org.mockito.InjectMocks;  
  import org.mockito.Mock;  
  import org.mockito.MockitoAnnotations;  
  import org.springframework.beans.factory.annotation.Autowired;  
  import org.springframework.test.context.ContextConfiguration;  
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  
  import com.company.dashboard.config.CoreConfig;  
  import com.company.dashboard.config.MVCConfig;  
  import com.company.dashboard.config.PersistenceJPAConfig;  
  import com.company.dashboard.config.WebAppInitializer;  
  import com.company.dashboard.core.util.Converter;  
  import com.company.dashboard.persistence.repository.RecordRepository;  
  import com.company.dashboard.persistence.repository.RequestRepository;  
  import com.company.dashboard.rest.domain.RestRequest;  
  
  @RunWith(SpringJUnit4ClassRunner.class)  
  @ContextConfiguration(classes = {MVCConfig.class, PersistenceJPAConfig.class, CoreConfig.class, WebAppInitializer.class})  
  public class ContextServiceTest {  
  
      @Mock  
      private RequestRepository requestRepository;  
  
      @Mock  
      private RecordRepository recordRepository;  
  
      @Mock  
      private Converter converter ;  
  
      @InjectMocks  
      @Autowired  
      ArchivingService archivingService;  
  
      @Before  
      public void init() {  
          MockitoAnnotations.initMocks(this);  
      }  
  
      @Test  
      public void myTest() {  
  
          List&lt;RestRequest&gt; restRequests = archivingService.findLastArchivingRequests("userId", true);  
      }  
  }
  </code></pre>
  
  <p>My Stacktrace: </p>
  
  <pre><code>01-08-2014 15:13:19 ERROR TestContextManager:334 - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@30848636] to prepare test instance [com.company.dashboard.core.services.archiving.ContextServiceTest@4c875c1c]  
  java.lang.IllegalStateException: Failed to load ApplicationContext  
      at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)  
      at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)  
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)  
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)  
      at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331)  
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)  
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290)  
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)  
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292)  
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)  
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)  
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)  
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)  
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)  
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)  
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)  
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)  
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)  
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)  
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)  
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)  
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)  
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)  
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)  
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)  
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)  
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'exceptionTranslation' defined in class com.company.dashboard.config.PersistenceJPAConfig: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class com.company.dashboard.config.PersistenceJPAConfig: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;  
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)  
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)  
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)  
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)  
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)  
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)  
      at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:220)  
      at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:618)  
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467)  
      at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125)  
      at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)  
      at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)  
      at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:250)  
      at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)  
      at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)  
      ... 25 more  
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class com.company.dashboard.config.PersistenceJPAConfig: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;  
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)  
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)  
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)  
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)  
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)  
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)  
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)  
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:470)  
      at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:277)  
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:139)  
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.&lt;init&gt;(PersistenceExceptionTranslationInterceptor.java:79)  
      at org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.&lt;init&gt;(PersistenceExceptionTranslationAdvisor.java:71)  
      at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:84)  
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1572)  
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1540)  
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)  
      ... 39 more  
  Caused by: java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;  
      at org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor.getValidationMode(PersistenceUnitInfoDescriptor.java:99)  
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.mergePropertySources(EntityManagerFactoryBuilderImpl.java:567)  
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.&lt;init&gt;(EntityManagerFactoryBuilderImpl.java:212)  
      at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:51)  
      at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:182)  
      at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:177)  
      at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152)  
      at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:290)  
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)  
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)  
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)  
      ... 54 more  
  </code></pre>
  
  <p>My maven dependency tree: </p>
  
  <pre><code>[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ software-dashboard ---  
  [INFO] com.company:software-dashboard:war:0.0.1-SNAPSHOT  
  [INFO] +- org.springframework:spring-core:jar:4.0.6.RELEASE:compile  
  [INFO] |  \- commons-logging:commons-logging:jar:1.1.3:compile  
  [INFO] +- org.springframework:spring-web:jar:4.0.6.RELEASE:compile  
  [INFO] |  +- org.springframework:spring-aop:jar:4.0.6.RELEASE:compile  
  [INFO] |  |  \- aopalliance:aopalliance:jar:1.0:compile  
  [INFO] |  \- org.springframework:spring-context:jar:4.0.6.RELEASE:compile  
  [INFO] +- org.springframework:spring-webmvc:jar:4.0.6.RELEASE:compile  
  [INFO] |  \- org.springframework:spring-expression:jar:4.0.6.RELEASE:compile  
  [INFO] +- org.springframework:spring-beans:jar:4.0.6.RELEASE:compile  
  [INFO] +- org.springframework.data:spring-data-jpa:jar:1.6.2.RELEASE:compile  
  [INFO] |  +- org.springframework.data:spring-data-commons:jar:1.8.2.RELEASE:compile  
  [INFO] |  +- org.springframework:spring-orm:jar:3.2.10.RELEASE:compile  
  [INFO] |  |  \- org.springframework:spring-jdbc:jar:3.2.10.RELEASE:compile  
  [INFO] |  +- org.springframework:spring-tx:jar:3.2.10.RELEASE:compile  
  [INFO] |  +- org.aspectj:aspectjrt:jar:1.8.1:compile  
  [INFO] |  \- org.slf4j:jcl-over-slf4j:jar:1.7.7:compile  
  [INFO] +- org.springframework:spring-test:jar:4.0.6.RELEASE:compile  
  [INFO] +- org.apache.maven.plugins:maven-surefire-plugin:jar:2.13:compile  
  [INFO] |  +- org.apache.maven:maven-plugin-api:jar:2.0.9:compile  
  [INFO] |  +- org.apache.maven.surefire:maven-surefire-common:jar:2.13:compile  
  [INFO] |  |  +- org.apache.maven.surefire:surefire-booter:jar:2.13:compile  
  [INFO] |  |  +- org.apache.maven:maven-artifact:jar:2.0.9:compile  
  [INFO] |  |  |  \- org.codehaus.plexus:plexus-utils:jar:1.5.1:compile  
  [INFO] |  |  +- org.apache.maven:maven-plugin-descriptor:jar:2.0.9:compile  
  [INFO] |  |  |  \- org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:compile  
  [INFO] |  |  +- org.apache.maven:maven-project:jar:2.0.9:compile  
  [INFO] |  |  |  +- org.apache.maven:maven-settings:jar:2.0.9:compile  
  [INFO] |  |  |  +- org.apache.maven:maven-profile:jar:2.0.9:compile  
  [INFO] |  |  |  +- org.apache.maven:maven-artifact-manager:jar:2.0.9:compile  
  [INFO] |  |  |  \- org.apache.maven:maven-plugin-registry:jar:2.0.9:compile  
  [INFO] |  |  +- org.apache.maven:maven-model:jar:2.0.9:compile  
  [INFO] |  |  \- org.apache.maven:maven-core:jar:2.0.9:compile  
  [INFO] |  |     +- org.apache.maven:maven-plugin-parameter-documenter:jar:2.0.9:compile  
  [INFO] |  |     +- org.apache.maven.reporting:maven-reporting-api:jar:2.0.9:compile  
  [INFO] |  |     +- org.apache.maven.wagon:wagon-provider-api:jar:1.0-beta-2:compile  
  [INFO] |  |     +- org.apache.maven:maven-repository-metadata:jar:2.0.9:compile  
  [INFO] |  |     +- org.apache.maven:maven-error-diagnostics:jar:2.0.9:compile  
  [INFO] |  |     +- org.apache.maven:maven-monitor:jar:2.0.9:compile  
  [INFO] |  |     \- classworlds:classworlds:jar:1.1:compile  
  [INFO] |  +- org.apache.maven.surefire:surefire-api:jar:2.13:compile  
  [INFO] |  +- org.apache.maven:maven-toolchain:jar:2.0.9:compile  
  [INFO] |  \- org.apache.maven.plugin-tools:maven-plugin-annotations:jar:3.2:compile  
  [INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:compile  
  [INFO] +- com.oracle:ojdbc7:jar:12.1.0.1:compile  
  [INFO] +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile  
  [INFO] +- org.hibernate:hibernate-entitymanager:jar:4.3.6.Final:compile  
  [INFO] |  +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile  
  [INFO] |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile  
  [INFO] |  +- org.hibernate:hibernate-core:jar:4.3.6.Final:compile  
  [INFO] |  |  +- antlr:antlr:jar:2.7.7:compile  
  [INFO] |  |  \- org.jboss:jandex:jar:1.1.0.Final:compile  
  [INFO] |  +- dom4j:dom4j:jar:1.6.1:compile  
  [INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile  
  [INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile  
  [INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile  
  [INFO] |  \- org.javassist:javassist:jar:3.18.1-GA:compile  
  [INFO] +- com.company.software.kernel:API_RMI:jar:4.7.1.1:compile  
  [INFO] |  +- com.company.software.kernel:API_OBJECTS:jar:4.7.1.1:compile  
  [INFO] |  +- com.company.software.kernel:AUTHENT:jar:4.7.1.1:compile  
  [INFO] |  +- com.company.software.kernel:LDAP:jar:4.7.1.1:compile  
  [INFO] |  |  \- com.company.software:ACM_TOOLBOX:jar:1.2.2-SNAPSHOT:compile  
  [INFO] |  |     \- log4j:log4j:jar:1.2.15:compile  
  [INFO] |  |        +- javax.mail:mail:jar:1.4:compile  
  [INFO] |  |        |  \- javax.activation:activation:jar:1.1:compile  
  [INFO] |  |        +- javax.jms:jms:jar:1.1:compile  
  [INFO] |  |        +- com.sun.jdmk:jmxtools:jar:1.2.1:compile  
  [INFO] |  |        \- com.sun.jmx:jmxri:jar:1.2.1:compile  
  [INFO] |  +- com.company.software.kernel:DB:jar:4.7.1.1:compile  
  [INFO] |  |  +- proxool:proxool:jar:0.9.1:compile  
  [INFO] |  |  +- com.jolbox:bonecp:jar:0.7.1.RELEASE:compile  
  [INFO] |  |  |  \- com.google.guava:guava:jar:r08:compile  
  [INFO] |  |  +- org.slf4j:slf4j-log4j12:jar:1.6.4:compile  
  [INFO] |  |  +- commons-dbcp:commons-dbcp:jar:1.3:compile  
  [INFO] |  |  |  \- commons-pool:commons-pool:jar:1.5.4:compile  
  [INFO] |  |  +- javax.ejb:ejb-api:jar:3.0:runtime  
  [INFO] |  |  +- net.sourceforge.jtds:jtds:jar:1.2.5:compile  
  [INFO] |  |  +- mysql:mysql-connector-java:jar:5.1.10:compile  
  [INFO] |  |  \- ojdbc:ojdbc:jar:14:compile  
  [INFO] |  +- com.company.software.kernel:GLOBAL:jar:4.7.1.1:compile  
  [INFO] |  +- org.apache.cxf:cxf-bundle:jar:2.4.0:compile  
  [INFO] |  |  +- org.apache.ws.xmlschema:xmlschema-core:jar:2.0:compile  
  [INFO] |  |  +- wsdl4j:wsdl4j:jar:1.6.2:compile  
  [INFO] |  |  \- org.apache.neethi:neethi:jar:3.0.0:compile  
  [INFO] |  +- org.apache.geronimo.specs:geronimo-servlet_3.0_spec:jar:1.0:compile  
  [INFO] |  +- org.apache.geronimo.specs:geronimo-jaxws_2.2_spec:jar:1.0:compile  
  [INFO] |  +- org.apache.geronimo.specs:geronimo-ws-metadata_2.0_spec:jar:1.1.3:compile  
  [INFO] |  +- org.jasig.cas.client:cas-client-core:jar:3.2.1:compile  
  [INFO] |  +- com.unboundid:unboundid-ldapsdk:jar:2.3.1:runtime  
  [INFO] |  \- javax.xml.bind:jaxb-api:jar:2.2.1:compile  
  [INFO] +- org.slf4j:slf4j-api:jar:1.7.7:compile  
  [INFO] +- org.apache.logging.log4j:log4j-api:jar:2.0:compile  
  [INFO] +- org.apache.logging.log4j:log4j-core:jar:2.0:compile  
  [INFO] +- net.sf.dozer:dozer:jar:5.5.1:compile  
  [INFO] |  +- commons-beanutils:commons-beanutils:jar:1.9.1:compile  
  [INFO] |  |  \- commons-collections:commons-collections:jar:3.2.1:compile  
  [INFO] |  \- org.apache.commons:commons-lang3:jar:3.2.1:compile  
  [INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.0:compile  
  [INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.0:compile  
  [INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.4.0:compile  
  [INFO] +- javassist:javassist:jar:3.12.1.GA:compile  
  [INFO] +- proxool:cglib:jar:0.9.1:provided  
  [INFO] +- org.mockito:mockito-all:jar:1.9.5:compile  
  [INFO] +- org.testng:testng:jar:6.8.8:test  
  [INFO] |  +- org.beanshell:bsh:jar:2.0b4:test  
  [INFO] |  \- com.beust:jcommander:jar:1.27:test  
  [INFO] \- junit:junit:jar:4.11:test  
  [INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test  
  </code></pre>
  
  <p>I know that the PersistenceUnit comes from following jar:
  javax.persistence - persistence-api-1.0.2.jar - M2_REPO/javax/persistence/persistence-api/1.0.2 - /home/user/.m2/repository/javax/persistence/persistence-api/1.0.2/persistence-api-1.0.2.jar - software-dashboard.</p>
  
  <p>I tried excluding the jar in spring web, spring webmvc, spring data jpa using:</p>
  
  <pre><code>    &lt;exclusions&gt;
          &lt;exclusion&gt;
              &lt;groupId&gt;javax.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;persistence-api&lt;/artifactId&gt;
          &lt;/exclusion&gt;
      &lt;/exclusions&gt;
  </code></pre>
  
  <p>but it didn't do the trick.</p>
  
  <p>Finally I tried deleting manually the jar from the classpath but I got a NullPointerException instead. After putting it back manually I get the previous exception again.</p>
  
  <p>Is my understanding of the problem right? If so, what is the right "maven way" to go?</p>
  
  <p>Thanks,</p>
  
  <p>Harald</p>