3

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 ?

VictorArgentin
  • 423
  • 3
  • 8
  • 17

1 Answers1

7

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')
Dónal
  • 185,044
  • 174
  • 569
  • 824
  • 4
    `findWhere` and `findAllWhere` or Criteria/HQL queries are the way to go when you need more than 2. The reason for the limit of 2 is just to avoid absurdly long method names, but this limitation has been removed in 1.4. To avoid confusion about order of resolution you can't mix `and`/`or` - they have to all be `and` or all `or`, but it's simple enough to do a real query if you need to mix `and` and `or`. – Burt Beckwith Jun 15 '11 at 16:37