0

Consider this domain class:

class House {
  Integer room
  Integer bathroom
  Date builtDate
  Date boughtDate

  String roadName

  String getSearch(){
    return room + " " + bathroom + " " + builtDate + " " + boughtDate
  }
}

I imagine having several fields for my search mechanism: search by room, bathroom, builtDate, boughtDate.

The user should be able to search for any combination of these paramaters. He can use only one, or all of them. I need some help with my controller code. I'm almost sure I cannot do that using HQL Dynamic Finders so I'll have to use SQLS statments.

Any help/hint would be appreciated.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
John22_2
  • 123
  • 1
  • 1
  • 7

1 Answers1

0

You probably want to use a hibernate criteria. Something along the lines of:

if (room && bathroom && builtDate && boughtDate) {
  House.withCriteria {
    if (room) {
      gte 'room', room
    }
    // ...
  }
}

Look at the docs on createCriteria and withCriteria for more information.

Blacktiger
  • 1,275
  • 1
  • 9
  • 19
  • hm wont work. what if i have 10 params. I have to do ifs for all that 10 combinations? – John22_2 Jul 01 '11 at 14:14
  • You could make things a bit simpler with some meta-programming... define a list of the params you need to deal with and then use the any method for the first if statement and a while loop to build the criteria object. Either way the criteria object is the best way to build complex sql like you want to. – Blacktiger Jul 01 '11 at 17:28