【发布时间】: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