user12384512
user12384512

Reputation: 3401

play.exceptions.JPAException: The JPA context is not initialized

I have a bean, which access JPA in constructor, e.g preload cache from database.

On application starts up I receive

Caused by: java.lang.IllegalStateException: Unable to create Guice injector

Application startup fails cause of Guice injection

1) Error injecting constructor, play.exceptions.JPAException: The JPA context is not initialized.
 JPA Entity Manager automatically start when one or more classes annotated with the @javax.persistence.Entity annotation are found in the application.

The root cause of this issue, that Guice is creating instance of my bean before play inits JPA entity manager. The rest JPA code works fine, if I comment JPA call in constructor of bean, it works fine too.

To configure my beans, using follow code snippet:

public class MainGuiceModule extends AbstractModule {
    @Override
    protected void configure() {
             bind(UserManager.class).to(UserManagerImpl.class).in(Singleton.class);
              ...
        }
}

The question is, how to access JPA from constructor using Play! and Guice ?

Upvotes: 1

Views: 1406

Answers (1)

user12384512
user12384512

Reputation: 3401

Finally I found the way how to avoid this exception, I have to wrap creation of injecor with

play.Play.plugin(JPAPlugin.class).startTx(true);

and

play.Play.plugin(JPAPlugin.class).closeTx(false);

I will accept answer with more elegant solution

Upvotes: 3

Related Questions