在 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 中管理此查询?任何帮助表示赞赏。