title: Spring Data JPA - Using @Transactional in a CDI environment instead of Spring environment
body: <p>I realized after writing this question I could sum it up in a few sentences. How can I manage transactions in Spring-Data-JPA with CDI the same way you would by using @Transactional in Spring itself?</p>
  
  <p>First thing I did was set up Spring Data JPA CDI based on the documentation here. <a href="http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/jpa.repositories.html#jpd.misc.cdi-integration" rel="nofollow">http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/jpa.repositories.html#jpd.misc.cdi-integration</a></p>
  
  <p>I set this up and it is working fine for read operations but not write operations</p>
  
  <p>For Example, Their example in the docs would work fine.</p>
  
  <pre><code>List&lt;Person&gt; people = repository.findAll();
  </code></pre>
  
  <p>So I have the basic setup complete.</p>
  
  <p>Written by hand may have typos. This is similar to the code I execute.</p>
  
  <pre><code>@Inject
  UserRepository userRepository;
  
  User user;
  
  @Transactional
  public void signUpUserAction() {
    userRepository.saveAndFlush(user);
  }
  </code></pre>
  
  <p>Then I receive this error </p>
  
  <pre><code>Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
  </code></pre>
  
  <p>At first I realized I did not have the @Transactional so I added it and still did not work.(I believe in spring you need to use the AOP xml file to set up @Transactional so it makes sense this does not work in EE out of the box, I just do not know how to make it work.)</p>
  
  <p>FYI annotating with this does not work</p>
  
  <pre><code>@TransactionAttribute(TransactionAttributeType.REQUIRED)
  </code></pre>
  
  <p>Something I tried while I was writing this post and I got it to work sort of... but I don't like the code and am still interested in using @Transactinoal, this code feels dirty, I'm pretty sure @Transactional handles calling other methods that are transactional in a clean way while this code would not.</p>
  
  <p>This saves and I verify it's in the database.</p>
  
  <pre><code>@Inject
  EntityManager em;
  
  @Inject
  UserRepository userRepository;
  
  private User user;
  
  public void signUpUserAction() {
    em.getTransaction().begin();
    userRepository.saveAndFlush(user);
    em.getTransaction().commit();
  }
  </code></pre>
  
  <p>So in short, how can I use @Transactional or something similar to manage my transactions?</p>
  
  <p>Thank you for any help.</p>