title: Spring Boot + JPA inject EntityManager no EntityManager available
body: <p>I am trying to annotation-inject an EntityManager into my DAO using spring boot but am getting an <code>InvalidDataAccessApiUsageException</code> saying there is no transactional EntityManager available. I am under the impression that Spring Boot will basically take care of everything for me as long as it has my DataSource info from <code>application.yml</code> and I tag the <code>EntityManager</code> field in my DAO with <code>@PersitenceContext</code>, so that I don't need an EntityManagerFactory in my own code nor a <code>persistence.xml</code> file. Am I wrong about that?</p>
  
  <p>My DAO:</p>
  
  <pre><code>@Repository
  public class PersonDaoJpa implements PersonDao {
  
      @PersistenceContext
      private EntityManager em;
  
      ...
  
      DAO methods
  
      ...
  }
  </code></pre>
  
  <p>My Spring Boot configuration:</p>
  
  <pre><code>@SpringBootApplication
  @EnableTransactionManagement
  @ComponentScan(basePackages = {ProgramInfo.BASE_PACKAGE})
  @EntityScan(basePackages = {ProgramInfo.BASE_PACKAGE})
  public class PersonServiceConfiguration {
  
      public static void main(String[] args) {
  
          SpringApplication.run(PersonServiceConfiguration.class, args);
      }
  }
  </code></pre>
  
  <p>My JUnit test:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = PersonServiceConfiguration.class)
  @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
      DirtiesContextTestExecutionListener.class,
      TransactionalTestExecutionListener.class,
      DbUnitTestExecutionListener.class })
  @EnableTransactionManagement
  @DatabaseSetup("/test_data_start.xml")
  public class PersonDaoJpaTest {
  
      @Autowired
      private PersonDaoJpa dao;
  
      ...
  
      JUnit tests 
  
      ...
  }
  </code></pre>
  
  <p>My <code>application.yml</code> file:</p>
  
  <pre><code>spring:
      profiles.active: default
  
  ---
  
  spring:
      profiles: default
  
  spring.datasource:
      driverClassName: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/database?autoReconnect=true
      username: user
      password: pwd
  
  spring.jpa.show-sql: false
  spring.jpa.database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
  </code></pre>
  
  <p>And finally my <code>pom.xml</code> file:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;1.2.2.RELEASE&lt;/version&gt;
    &lt;/parent&gt;  
    &lt;groupId&gt;myId&lt;/groupId&gt;
    &lt;artifactId&gt;myArtifictId&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  
    &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;/properties&gt;
  
    &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.dbunit&lt;/groupId&gt;
          &lt;artifactId&gt;dbunit&lt;/artifactId&gt;
          &lt;version&gt;2.5.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.github.springtestdbunit&lt;/groupId&gt;
          &lt;artifactId&gt;spring-test-dbunit&lt;/artifactId&gt;
          &lt;version&gt;1.2.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
          &lt;artifactId&gt;commons-dbcp2&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
      &lt;/dependency&gt;
    &lt;/dependencies&gt;
  
    &lt;repositories&gt;
        &lt;repository&gt;
            &lt;id&gt;spring-releases&lt;/id&gt;
            &lt;url&gt;https://repo.spring.io/libs-release&lt;/url&gt;
        &lt;/repository&gt;
    &lt;/repositories&gt;
    &lt;pluginRepositories&gt;
        &lt;pluginRepository&gt;
            &lt;id&gt;spring-releases&lt;/id&gt;
            &lt;url&gt;https://repo.spring.io/libs-release&lt;/url&gt;
        &lt;/pluginRepository&gt;
    &lt;/pluginRepositories&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Here is the full exception I get:</p>
  
  <pre><code>org.springframework.dao.InvalidDataAccessApiUsageException: No transactional EntityManager available; nested exception is javax.persistence.TransactionRequiredException: No transactional EntityManager available
  at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:410)
  at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:216)
  at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
  at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
  at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
  at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
  at com.accenture.javadojo.orgchart.dao.PersonDaoJpa$$EnhancerBySpringCGLIB$$6c935a0d.flush(&lt;generated&gt;)
  at dao.PersonDaoJpaTest.testUpdateInvalid(PersonDaoJpaTest.java:92)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
  at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
  at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
  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:68)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
  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: javax.persistence.TransactionRequiredException: No transactional EntityManager available
  at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:275)
  at com.sun.proxy.$Proxy54.flush(Unknown Source)
  at com.accenture.javadojo.orgchart.dao.PersonDaoJpa.flush(PersonDaoJpa.java:63)
  at com.accenture.javadojo.orgchart.dao.PersonDaoJpa$$FastClassBySpringCGLIB$$ed9d6faf.invoke(&lt;generated&gt;)
  at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
  at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
  at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
  ... 33 more
  </code></pre>
  
  <p><strong>UPDATE:</strong>
  The following JUnit test passes when I comment out <code>dao.flush()</code>:</p>
  
  <pre><code>@Test
  public void testFind() {
  
      Person one = dao.find(1l);
      //dao.flush();
      assertNotNull("User was not found", one);
      assertEquals("Last name not as expected", "one", one.getLastName());
      assertEquals("First name not as expected", "start1", one.getFirstName());
  }
  </code></pre>
  
  <p>but with it uncommented, I get the same exception as described above. The <code>find</code> and <code>flush</code> methods are as follows:</p>
  
  <pre><code>@Override
  public Person find(Long id) {
  
      return em.find(Person.class, id);
  }
  
  public void flush() {
  
      em.flush();
  }
  </code></pre>