2

如果我使用 Join 在条件查询中创建谓词,我将不会得到任何结果。当我将Game表用作根时,相同的谓词正在返回实体。

工作查询:

    CriteriaQuery<Game> query = cb.createQuery(Game.class);
    Root<Game> root = query.from(Game.class);

    List<Predicate> predicates = new LinkedList<Predicate>();
    if(!selectedPlatforms.isEmpty()) {
        predicates.add(root.get(Game_.type).in(TypeConverter.convert(selectedPlatforms)));
    }

    if(!selectedCategories.isEmpty()) {
        Join<Game, String> collection = root.join(Game_.categories);
        predicates.add(collection.in(cb.literal(selectedCategories)));
    }

    if(!selectedGames.isEmpty()) {
        predicates.add(cb.isTrue(root.get(Game_.name).in(selectedGames)));
    }


    query.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    games = em.createQuery(query).getResultList();

不工作查询:

    CriteriaQuery<Hit> query = cb.createQuery(Hit.class);
    List<Predicate> predicates = new LinkedList<>();
    Date startDate = null;
    Date endDate = null;

    Root<Hit> hitRoot = query.from(Hit.class);
    switch (time) {
        case "Week":
            startDate = new DateTime().withWeekOfWeekyear(timeValue).withDayOfWeek(DateTimeConstants.MONDAY).toDate();
            endDate = new DateTime().withWeekOfWeekyear(timeValue+1).withDayOfWeek(DateTimeConstants.SUNDAY).toDate();
    }
    predicates.add(cb.and(cb.greaterThanOrEqualTo(hitRoot.<Date>get("hitDate"), startDate), cb.lessThanOrEqualTo(hitRoot.<Date>get("hitDate"), endDate)));

    Join<Hit, Game> gameJoin = hitRoot.join("game", JoinType.LEFT);
    if(!selectedPlatforms.isEmpty()) {
        predicates.add(gameJoin.get(Game_.type).in(TypeConverter.convert(selectedPlatforms)));
    }

    if(!selectedCategories.isEmpty()) {
        Join<Game, String> collection = gameJoin.join(Game_.categories);
        predicates.add(collection.in(cb.literal(selectedCategories)));
    }

    if(!selectedGames.isEmpty()) {
        predicates.add(cb.isTrue(gameJoin.get(Game_.name).in(selectedGames)));
    }

    query.groupBy(hitRoot.get("hitDate"), hitRoot.get("shop"));
    query.orderBy(cb.asc(hitRoot.get("shop")));
    query.where(predicates.toArray(new Predicate[predicates.size()]));

    List<Hit> results = em.createQuery(query).getResultList();

以下部分负责不返回任何匹配实体。在第一个查询中刚刚应用于 aRoot而不是Joinlike 的相同部分正在返回 machting 实体。没有这部分,其他一切都在工作。

    if(!selectedGames.isEmpty()) {
        predicates.add(cb.isTrue(gameJoin.get(Game_.name).in(selectedGames)));
    }
4

1 回答 1

0

通过以“运行”模式重新启动应用程序服务器,问题自行解决。

于 2013-10-05T10:08:44.573 回答