0

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?

First thing I did was set up Spring Data JPA CDI based on the documentation here. http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/jpa.repositories.html#jpd.misc.cdi-integration

I set this up and it is working fine for read operations but not write operations

For Example, Their example in the docs would work fine.

List<Person> people = repository.findAll();

So I have the basic setup complete.

Written by hand may have typos. This is similar to the code I execute.

@Inject
UserRepository userRepository;

User user;

@Transactional
public void signUpUserAction() {
  userRepository.saveAndFlush(user);
}

Then I receive this error

Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress

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.)

FYI annotating with this does not work

@TransactionAttribute(TransactionAttributeType.REQUIRED)

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.

This saves and I verify it's in the database.

@Inject
EntityManager em;

@Inject
UserRepository userRepository;

private User user;

public void signUpUserAction() {
  em.getTransaction().begin();
  userRepository.saveAndFlush(user);
  em.getTransaction().commit();
}

So in short, how can I use @Transactional or something similar to manage my transactions?

Thank you for any help.

4

2 に答える 2