Grails using dynamic finders with 3x+ logical arguments

主宰稳场 提交于 2019-12-31 01:58:11

问题


I sucessfully managed to search in the database using this dynamic finder from Hibernate:

def temp = User.findByNameAndStreet("name", "street")

Although, i need a tripple logical argument like this:

def temp = User.findByNameAndStreetAndCity("name", "street", "city")

Any simple way to do it ?


回答1:


The Grails dynamic finders don't support more than two predicates. This is because it's not clear whether

User.findByNameAndAgeOrGender('foo', 12, 'm')

means this:

(name == 'foo' && age == 12) || gender == 'm'

or this:

name == 'foo' && (age == 12 || gender == 'm')

Admittedly if the predicates are always combined with And or Or.


Update: since Grails 1.4 you can have an unlimited number of predicates if they're all combined with either And or Or


Instead, you can use either findWhere or findAllWhere (depending on whether you want just the first result or all results). Both of these support an unlimited number of predicates which I assume are combined with And, for example:

User.findAllWhere(name: "foo", age: 12, gender: 'm')


来源:https://stackoverflow.com/questions/6360547/grails-using-dynamic-finders-with-3x-logical-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!