0

Hi I am getting an exception while doing db operations.

java.lang.RuntimeException: No EntityManager found in the context. Try to annotate your action method with @play.db.jpa.Transactional

Below is the code

paymentResponse.onRedeem(response -> persistTransaction(response, buyerInformation.getCustomerId()));
paymentResponse.onRedeem(response -> completeProductOrder(response, buyerInformation));
paymentResponse.onRedeem(response -> postSuccessfulProcess(response, purchasePayload, buyerInformation));

persistTransaction and postSuccessfulProcess are doing db update transactions using something like below

JPA.withTransaction(() -> {

and code to save is like

JPA.em().persist(entity);

withTransaction binds EntityManager from the map with key "default" but JPA.em() actually looks for EntityManager with name currentEntityManager. I tried to use JPA.em("default") but it gives connection timeout.

I am new to play framework. Can you please suggest me some remedy for this issue.

Sammy Pawar
  • 1,201
  • 3
  • 19
  • 38
  • Please, see my [answer](http://stackoverflow.com/a/32443893/1195766) on related question. Especially, look at the gist with simple complete example https://gist.github.com/dzagorovsky/b8064c97ba647ed453ab – dzagorovsky Oct 17 '16 at 19:03

1 Answers1

0

Starting from Play 2.5 global object JPA was deprecated and JPAApi should be used instead. The JPAApi must be injected somewhere before. Since I see only a small fragment of your code, the simplest way I can suggest for injecting it is like this:

JPAApi jpa = Play.current().injector().instanceOf(JPAApi.class);

Now you can use it:

 jpa.withTransaction(() ->...

or

 jpa.em().persist(entity);

Of cause the preferable way is injecting with @Inject annotation.

You can find more about differences in JPA usage starting from Play 2.5 in this post.

asch
  • 1,923
  • 10
  • 21
  • Using global state of application like `Play.current()` is not recommended, it is better to directly inject `JPAApi` would be a better approach. – RP- Oct 18 '16 at 05:41
  • @PR That is why it is mentioned, what is the preferable way. – asch Oct 18 '16 at 08:50
  • It depends, if we need JPAApi in a managed bean (Guice or other), then we can directly inject into it otherwise, we somehow need to pass it while initializing that object (where we need to use jpaapi) or use `static injection` which is also not recommended. With Play 2.5 the intention is everything should be through DI. – RP- Oct 18 '16 at 23:20