Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Grails Criteria Builder

 
Gareth Millward
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

My first post on the forum! I have a real problem. I am trying to do a search on a object that references another table. But for the life of me I can not seem to search on the propertyType.id object. However I can build a search on the town, road, fields etc. I have tried to do a join and all-sorts, it just ends up bringing back all the results from the database. Any advice would be appreciated.

I have a 2 tables:

property (table)

id
property_name
property_town
property_raod
property_type_id

property_type (table)

id
property_type
description

My search form:



My controller code to handle search

 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't this:
eq("propertyType.id", Integer.parseInt(params.propertyTypeId) )

be this?:
eq("property_type_id", Integer.parseInt(params.propertyTypeId) )
[ September 29, 2008: Message edited by: Marc Peabody ]
 
Gareth Millward
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanks for the help...
I gave it a try and altered the query a little, I also put in a search by town from the property table which worked fine. Still though trying to search using the property_type_id which holds a integer mapping to the property_type table i still have a problem

Putting in the property_type_id like you suggested yielded the following:

Message: could not resolve property: property_type_id of: Property
Caused by: org.hibernate.QueryException: could not resolve property: property_type_id of: Property
Class: PropHomeController
At Line: [45]
Code Snippet:

The code in the controller is:

def search = {

//def property = Property.get()

if (request.method == 'GET') {

def criteria = Property.createCriteria()
def results = criteria.list {
like("town", "Almancil%")
and {
eq("property_type_id", '%' + params.propertyTypeId + '%')
}
}
render(view:'search', model:[ propertyList: results])
}
}
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using % characters I think you have to use like, not eq.

Also, your display name used to match our naming policy of a first and last name but now it's different. Please update your profile to change it back to a valid name.

There's currently a bug in Google Chrome that if you ask it to remember your password and then you visit the page to update your JavaRanch profile, Chrome will actually replace your Display Name with your user id. If you use Chrome, that might have been the problem. Thanks in advance for fixing your display name.
 
Gareth Millward
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed that not sure what happened.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64975
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Gareth", please check your private messages for an important administrative matter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic