简体   繁体   English

java Querydsl错误结果与or()

[英]java Querydsl wrong result with or()

I'm new to querydsl and probably I miss the concept somehow. 我是querydsl的新手,也许我某种程度上想念这个概念。 What I want to do is fetch all person objects, who's name starts with "He", or who's birthday has passed or who's adresses' street starts with "ge". 我要做的是获取所有对象,这些对象的名称以“ He”开头,或者生日已过,或者地址的街道以“ ge”开头。

When I use the snippet here, I only get the person objects who match the third condition (person.adresse.strasse). 在此处使用代码段时,我只会得到与第三个条件(person.adresse.strasse)匹配的人员对象。 The first zwo conditions are completely ignored. 第一个zwo条件被完全忽略。 If I comment out the third condition, I get the results where the two first conditions (person.name machtes or person.birthday.before) matches. 如果我注释掉第三个条件,则会得到两个第一个条件(person.name machtes或person.birthday.before)匹配的结果。

What am I doing wrong? 我究竟做错了什么? Do I have to do a join instead? 我必须参加吗?

List<Person> persons = query.select( person )
            .from( person )
            .where(
                    person.name.startsWith( "He" )
                    .or( person.birthday.before( new Date( System.currentTimeMillis() ) )
                            .or( person.adresse.strasse.startsWithIgnoreCase( "ge" ) ) )
            )
            .fetch();

You have wrong number of parenthesis. 您的括号数字错误。

Unintentionally, you have nested the third or inside the second one, that's why when you comment the third or you get correct results. 无意中,您将第三个or第二个嵌套在嵌套中,这就是为什么当您注释第三个or获得正确结果时的原因。

Try this: 尝试这个:

List<Person> persons = query.select( person )
            .from( person )
            .where(
                    person.name.startsWith("He")
                    .or(person.birthday.before(new Date(System.currentTimeMillis())))
                    .or(person.adresse.strasse.startsWithIgnoreCase("ge")) 
            )
            .fetch();

It should work. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM