【问题标题】:Playframeowk with Java throws JPA Transaction failed. caused by: there is no started appliactionPlay Framework with Java throws JPA Transaction failed。原因:没有启动的应用程序
【发布时间】:2016-09-17 13:32:36
【问题描述】:

我正在使用带有 Java 和休眠功能的 Play 2.5.7。我需要在应用程序启动时进行一些数据库调用并加载配置。它一直因标题中的错误而失败。 代码、应用启动类:

public class ApplicationStart {
    @Inject
    public ApplicationStart(JPAApi jpaApi, ConfigService configService) {
        jpaApi.withTransaction(() -> {
            configService.reloadConfigs();
        });
    }
}

模块,配置方法:

bind(HibernateDao.class).to(HibernateDaoImpl.class);
bind(ConfigService.class).to(ConfigServiceImpl.class);
bind(ApplicationStart.class).asEagerSingleton();

ConfigureServiceImpl:

@Override
public void reloadConfigs() {
    List<Implementation> impls = hibernateDao.executeQueryForObject(QUERY_GET_IMPLEMENTATIONS, null);
    // process and load the condig.
}

HibernateDaoImpl:

@Override
public <T> List<T> executeQueryForObject(String sql, Map<String, Object> map)
        throws PersistenceException {
    EntityManager em = JPA.em("default");
    try {
        Query query = prepareQuery(em, sql, map);
        List<T> objectList = query.getResultList();
        return objectList;
    } finally {
        em.close();
    }
}

实际错误:

play.api.UnexpectedException: Unexpected exception[CreationException: Unable to create injector, see the following errors:

1) Error injecting constructor, java.lang.RuntimeException: JPA transaction failed
...
Caused by: com.google.inject.CreationException: Unable to create injector, see the following errors:
...
Caused by: java.lang.RuntimeException: JPA transaction failed
...
Caused by: java.lang.RuntimeException: There is no started application
        at scala.sys.package$.error(package.scala:27)
        at play.api.Play$$anonfun$current$1.apply(Play.scala:86)
        at play.api.Play$$anonfun$current$1.apply(Play.scala:86)
        at scala.Option.getOrElse(Option.scala:121)
        at play.api.Play$.current(Play.scala:86)
        at play.api.Play.current(Play.scala)
        at play.Play.privateCurrent(Play.java:89)
        at play.Play.application(Play.java:22)
        at play.db.jpa.JPA.jpaApi(JPA.java:48)
        at play.db.jpa.JPA.em(JPA.java:60)

【问题讨论】:

    标签: java hibernate jpa playframework guice


    【解决方案1】:

    那是因为您使用的是静态 JPA.em,而不是注入正确的类。像在 ApplicationStart 类中一样注入它,它应该可以工作

    public class HibernateDaoImpl implements HibernateDao {
    
        private final JPAApi JPA_API;
    
        @Inject
        public HibernateDaoImpl(JPAApi api) {
            this.JPA_API = api;    
        }
    
    
        // (...)
    
        @Override
        public <T> List<T> executeQueryForObject(String sql, Map<String, Object> map)
                throws PersistenceException {
            EntityManager em = JPA_API.em("default");
            try {
                Query query = prepareQuery(em, sql, map);
                List<T> objectList = query.getResultList();
                return objectList;
            } finally {
                em.close();
            }
        }
        // (...)
    }
    

    【讨论】:

    • 成功了,谢谢。我发现 play 的文档非常混乱。
    • 是的,文档有点混乱。特别是因为在次要版本(例如:2.4 和 2.5)之间,很多 api 可以更改。最好的解决方法是使用与您的 Play 版本和正确的 Javadoc/sources 最匹配的文档
    猜你喜欢
    • 1970-01-01
    • 2018-12-14
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    相关资源
    最近更新 更多