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();

次の部分は、一致するエンティティを返さないことに責任があります。Root最初のクエリで like の代わりにa に適用された同じ部分がJoin、一致するエンティティを返しています。この部分がなければ、他のすべてが機能しています。

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

1 に答える 1

0

この問題は、アプリケーション サーバーを「実行」モードで再起動することで解決しました。

于 2013-10-05T10:08:44.573 に答える