Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Guice play.exceptions.jpaeexception:JPA上下文未初始化_Guice_Playframework 1.x - Fatal编程技术网

Guice play.exceptions.jpaeexception:JPA上下文未初始化

Guice play.exceptions.jpaeexception:JPA上下文未初始化,guice,playframework-1.x,Guice,Playframework 1.x,我有一个bean,它在构造函数中访问JPA,例如从数据库预加载缓存 申请启动时,我收到 Caused by: java.lang.IllegalStateException: Unable to create Guice injector 由于Guice注入,应用程序启动失败 1) Error injecting constructor, play.exceptions.JPAException: The JPA context is not initialized. JPA Entity

我有一个bean,它在构造函数中访问JPA,例如从数据库预加载缓存

申请启动时,我收到

Caused by: java.lang.IllegalStateException: Unable to create Guice injector
由于Guice注入,应用程序启动失败

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.
这个问题的根本原因是,Guice在开始使用inits JPA实体管理器之前创建了mybean的实例。其余的JPA代码工作正常,如果我在bean的构造函数中注释JPA调用,它也工作正常

要配置my Bean,请使用以下代码段:

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

问题是,如何使用Play从构造函数访问JPA!还有Guice?

最后我找到了避免这种异常的方法,我必须用

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

我会接受一个更优雅的答案

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