title: JPA + Spring Boot: TransactionRequiredException on CRUD operations
body: <p>I am likely missing something very fundamental to how JPA is used, but I am getting <code>javax.persistence.TransactionRequiredException: No transactional EntityManager available</code> when I try to perform CRUD operations from a DAO, specifically operations that alter the database. I am using Spring Boot.</p>
  
  <p>This is a toy example that gives the setup of my code, as you'll notice all the methods called from the EntityManager that just poll information work, but as soon as I try to alter the database somehow I have problems:</p>
  
  <pre><code>@Repository
  public class PersonJpa{
  
      @PersistenceContext
      private EntityManager em;
  
      public void foo() {
  
          Long id = 500l;
          Person p = new Person(id, "lastName", "firstName");
  
          // all of these calls work:
          em.find(Person.class, id);
          em.contains(p);
          em.getReference(Person.class, id);
  
          // this call causes exception:
          em.remove(p);
      }
  }
  </code></pre>
  
  <p>Here is my Spring Boot configuration:</p>
  
  <pre><code>@SpringBootApplication
  @EnableTransactionManagement
  @ComponentScan(basePackages = {Info.BASE_PACKAGE})
  @EntityScan(basePackages = {Info.BASE_PACKAGE})
  public class PersonServiceConfiguration {
  
      public static void main(String[] args) {
  
          SpringApplication.run(PersonServiceConfiguration.class, args);
      }
  }
  </code></pre>
  
  <p>Here is 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/db?autoReconnect=true
      username: user
      password: pwd
  
  spring.jpa.show-sql: false
  spring.jpa.database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
  </code></pre>