0

在 MySql 中有这样的查询:

SELECT * FROM runninggame WHERE game = 6 AND spieler IN (SELECT id FROM spieler WHERE tormann = 1);

查询在 MySQL 中运行良好,我在 RunninggameDAO.java 中需要一个类似的查询。我在 Rapidclipse 的 JPA-SQL-Editor 中尝试了这个:

findGoaliesInGame (Game game) {
select * from Runninggame where game = :game and spieler in (select id from Spieler where tormann = 1) order by spieler

}

JPA-SQL-Editor 没有显示错误,但在 DAO-Class 的 Java-Tab 中,“subquery.multiselect(subqueryRoot.get(Spieler_.id));”行中有错误 => “方法 multiselect(subqueryRoot.get(Spieler_.id)) 未定义子查询类型”

public List<Runninggame> findGoaliesInGame(final Game game) {
    final EntityManager entityManager = em();
    final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    final ParameterExpression<Game> gameParameter = criteriaBuilder.parameter(Game.class, "game");
    final CriteriaQuery<Runninggame> criteriaQuery = criteriaBuilder.createQuery(Runninggame.class);
    final Subquery<Integer> subquery = criteriaQuery.subquery(Integer.class);
    final Root<Spieler> subqueryRoot = subquery.from(Spieler.class);
    subquery.multiselect(subqueryRoot.get(Spieler_.id));
    subquery.where(criteriaBuilder.equal(subqueryRoot.get(Spieler_.tormann), criteriaBuilder.literal(1)));
    final Root<Runninggame> root = criteriaQuery.from(Runninggame.class);
    criteriaQuery.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(Runninggame_.game), gameParameter),  root.get(Runninggame_.spieler).in(subquery)));
    criteriaQuery.orderBy(criteriaBuilder.asc(root.get(Runninggame_.spieler)));
    final TypedQuery<Runninggame> query = entityManager.createQuery(criteriaQuery);
    query.setParameter(gameParameter, game);
    return query.getResultList();
}

此代码由 Rapidclipse 生成,无法更改,那么如何在 Rapidclipse 中管理此查询?任何帮助表示赞赏。

4

1 回答 1

1

这种子选择似乎有问题,所以我为它创建了一个问题。

但是这个查询的简写版本似乎工作得很好:

findGoaliesInGame(Game game) {
    from Runninggame
    where game = :game
        and spieler.tormann = 1
    order by spieler
}

我希望这有帮助!

于 2019-10-17T08:41:24.420 回答