为什么在Grails / GORM中使用三个条件时findAllBy *会失败?

时间:2021-08-19 06:48:52

Consider the following usages of the Grails/GORM dynamic finder method findAllBy*:

请考虑以下Grails / GORM动态查找器方法findAllBy *的用法:

def foo1 = Foo.findAllByYear(yyyy)
def foo2 = Foo.findAllByMonth(mm)
def foo3 = Foo.findAllByDay(dd)
def foo4 = Foo.findAllByYearAndMonth(yyyy, mm)
def foo5 = Foo.findAllByYearAndDay(yyyy, dd)
def foo6 = Foo.findAllByYearAndMonthAndDay(yyyy, mm, dd)

println "# foo1=${foo1.size()} foo2=${foo2.size()} foo3=${foo3.size()}"
println "# foo4=${foo4.size()} foo5=${foo5.size()} foo6=${foo6.size()}"

The first five of these dynamic finder usages works as expected.

这些动态查找器使用中的前五个按预期工作。

However, the sixth one fails with an InvalidPropertyException ("No property found for name [yearAndMonth] for class [class foo]").

但是,第六个失败,出现InvalidPropertyException(“为类[class foo]”找不到名称[yearAndMonth]的属性)。

Question:

Why doesn't the sixth one work? Isn't findAllBy* able to handle more than two conditions? Solution/work-around?

为什么第六个不起作用?是不是findAllBy *能够处理两个以上的条件?解决方案/变通?

2 个解决方案

#1


Ted's answer is correct, but the reason why Grails doesn't support more than 2 predicates is because if all the conjunctions are not the same then it's not clear what the intention is.

Ted的答案是正确的,但Grails不支持超过2个谓词的原因是因为如果所有连词都不相同那么就不清楚意图是什么了。

Or in plain English...

或者用简单的英语......

  • It's obvious what findAllByYearAndMonthAndDay(1999,2,3) means
  • 很明显findAllByYearAndMonthAndDay(1999,2,3)的含义

  • It's also obvious what findAllByYearOrMonthOrDay(1999,2,3) means
  • findAllByYearOrMonthOrDay(1999,2,3)的含义也很明显

  • But it's not obvious what findAllByYearOrMonthAndDay(1999,2,3) means
  • 但是,findAllByYearOrMonthAndDay(1999,2,3)意味着什么并不明显

However, if all the conjunctions are the same (all ands or all ors), then there's no reason why more than 2 predicates couldn't be supported. In fact, if you search the Grails JIRA you'll find there's already an open issue for this. Vote for it if you feel strongly about it.

但是,如果所有连词都是相同的(全部或全部),那么就没有理由不支持超过2个谓词。事实上,如果你搜索Grails JIRA,你会发现这已经是一个未解决的问题。如果您对此感到强烈,请投票支持。

Incidentally, the IntelliJ Grails plugin erroneously provides code completion for dynamic finders with more than 2 predicates.

顺便提一下,IntelliJ Grails插件错误地为具有2个以上谓词的动态查找器提供代码完成。

The solution/workaround is to simply write a Criteria query or use HQL instead.

解决方案/解决方法是简单地编写Criteria查询或使用HQL。

#2


Nope, not currently. It's limited to 2 conditions at the moment. For more than 2 look at the criteria builder or the findAllWhere methods.

不,不是。目前仅限于2个条件。有关2个以上,请查看条件构建器或findAllWhere方法。

#1


Ted's answer is correct, but the reason why Grails doesn't support more than 2 predicates is because if all the conjunctions are not the same then it's not clear what the intention is.

Ted的答案是正确的,但Grails不支持超过2个谓词的原因是因为如果所有连词都不相同那么就不清楚意图是什么了。

Or in plain English...

或者用简单的英语......

  • It's obvious what findAllByYearAndMonthAndDay(1999,2,3) means
  • 很明显findAllByYearAndMonthAndDay(1999,2,3)的含义

  • It's also obvious what findAllByYearOrMonthOrDay(1999,2,3) means
  • findAllByYearOrMonthOrDay(1999,2,3)的含义也很明显

  • But it's not obvious what findAllByYearOrMonthAndDay(1999,2,3) means
  • 但是,findAllByYearOrMonthAndDay(1999,2,3)意味着什么并不明显

However, if all the conjunctions are the same (all ands or all ors), then there's no reason why more than 2 predicates couldn't be supported. In fact, if you search the Grails JIRA you'll find there's already an open issue for this. Vote for it if you feel strongly about it.

但是,如果所有连词都是相同的(全部或全部),那么就没有理由不支持超过2个谓词。事实上,如果你搜索Grails JIRA,你会发现这已经是一个未解决的问题。如果您对此感到强烈,请投票支持。

Incidentally, the IntelliJ Grails plugin erroneously provides code completion for dynamic finders with more than 2 predicates.

顺便提一下,IntelliJ Grails插件错误地为具有2个以上谓词的动态查找器提供代码完成。

The solution/workaround is to simply write a Criteria query or use HQL instead.

解决方案/解决方法是简单地编写Criteria查询或使用HQL。

#2


Nope, not currently. It's limited to 2 conditions at the moment. For more than 2 look at the criteria builder or the findAllWhere methods.

不,不是。目前仅限于2个条件。有关2个以上,请查看条件构建器或findAllWhere方法。