簡體   English   中英

JPA 規范在嘗試將 OR 謂詞組合在一起時不起作用

[英]JPA Specifications not working when trying to OR predicates together

我在嘗試獲取此查詢的工作規范時遇到問題。 我的數據 model 看起來像這樣:

            Item { 
                Person person, 
                Place place
            }

            Person {
                Address address
            }

            Place {
                Address address
            }

            Address {
                String country
                String state
            }

一個項目可以關聯一個人或一個地點,但不能同時關聯兩者。 我想通過搜索國家和 state 來根據地址是否與關聯的人或地點的地址匹配來查詢項目列表。我當前的實現如下所示:

            public static Specification<Item> locationIs(String country, String state) {
                return (root, query, criteriaBuilder) -> {
                    Expression<String> personCountry = root.get("person").get("address").get("country");
                    Expression<String> placeCountry = root.get("place").get("address").get("country");
                    Expression<String> personState = root.get("person").get("address").get("state");
                    Expression<String> placeState = root.get("place").get("address").get("state");

                    Predicate personCountryMatch = criteriaBuilder.like(personCountry, "%" + country + "%");
                    Predicate personStateMatch = criteriaBuilder.like(personState, "%" + state + "%");
                    Predicate personMatch = criteriaBuilder.and(personCountryMatch, personStateMatch);

                    Predicate placeCountryMatch = criteriaBuilder.like(placeCountry, "%" + country + "%");
                    Predicate placeStateMatch = criteriaBuilder.like(placeState, "%" + state + "%");
                    Predicate placeMatch = criteriaBuilder.and(placeCountryMatch, placeStateMatch);

                    Predicate match = criteriaBuilder.or(personMatch, placeMatch);

                    return match
                };
            }

如果我只返回 personMatch 或只返回 placeMatch,它似乎可以完美地搜索它,但是一旦我嘗試將它們它們放在一起,整個查詢就不會返回任何結果。 我已經嘗試從每個謂詞創建規范並使用相同的結果組合它們。

最終只是為此使用子查詢,做了類似的事情

criteriaBuilder.or(criteriaBuilder.exists(subquery1), criteriaBuilder.exists(subquery2))

那很好用

雖然子查詢可能是一個不錯的選擇,但您最初的目標查詢似乎是:

Join<Person> person = root.join("person", JoinType.LEFT);
Join<Person> place = root.join("place", JoinType.LEFT);

Predicate personCountryMatch = criteriaBuilder.like(person.get("country"), "%" + country + "%");
Predicate personStateMatch = criteriaBuilder.like(person.get("state"), "%" + state + "%");
Predicate personMatch = criteriaBuilder.and(personCountryMatch, personStateMatch);

Predicate placeCountryMatch = criteriaBuilder.like(place.get("country"), "%" + country + "%");
Predicate placeStateMatch = criteriaBuilder.like(lace.get("state"), "%" + state + "%");
Predicate placeMatch = criteriaBuilder.and(placeCountryMatch, placeStateMatch);

Predicate match = criteriaBuilder.or(personMatch, placeMatch);

return match

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM