Grails query (HQL) problem

64 views
Skip to first unread message

jnlltd

unread,
Oct 16, 2014, 6:19:24 AM10/16/14
to grails-de...@googlegroups.com
Hi everyone please help.

I have issue with HQL query Grails 2.4.3:

Now this query works perfectly

def home = {
def city = City.findByName("London")
        def services = Services.findByName("Shop")
def pages= Pages.findAll("from Pages as p where p.city= ? and p.services= ?" ,[city]+[services])
render pages as JSON
}


This query as it is show below worked then for no reason stopped  no error message the json result is empty []

def search = {
def city = City.findByName(params.id)
        def services = Services.findByName(params.is)
def pages = Pages.findAll("from Pages as p where p.city= ? and p.services= ?" ,[city]+[services])
render pages as JSON
}

Note: this the url value i am passing: http://localhost:8080/com.api.test/Pages/search/London?Shop

any ideas?  or how can I do this query and what will be the url?



John Alade

unread,
Oct 17, 2014, 4:03:38 AM10/17/14
to grails-de...@googlegroups.com
Firstly, I think you can assign the parameters local method variables before using them

Also, Have you tried to log the variables and check thier values

Your URL also seem strange, can you explain the different segments

Jeff Scott Brown

unread,
Oct 17, 2014, 2:49:00 PM10/17/14
to grails-de...@googlegroups.com


On October 16, 2014 at 3:19:26 AM, jnlltd (jnl...@gmail.com) wrote:

> def pages= Pages.findAll("from Pages as p where p.city= ? and p.services=
> ?" ,[city]+[services])

I don’t think this is related to your problem but do you know if there is a reason to construct to Lists and add them to each other there, as opposed to only creating 1 List?



JSB
--
Jeff Scott Brown
jbr...@pivotal.io

Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

jnlltd

unread,
Oct 21, 2014, 10:11:27 PM10/21/14
to grails-de...@googlegroups.com

Country class

City Class

Services Class 

class Pages {

       String name

       String address

       String postcode

       String description

       Long phone

       String email

       String website

             

       static belongsTo = [country: Country, city: City, services: Services]

----------------------------------------------------------------------------------

Test 1 ) Controller;

def search = {      

       def city = City.findByName(“london”)

       def services = Services.findByName(“petshap”)

def results= Pages.findAll("from Pages as p where p.city = ? and p.services = ?" ,[city, services])

               render results as JSON          

       }     

POST Via AJAX; url posted is: http://localhost:8080/com.api.test/Pages/search/

The above example works OK (result [{"class":"com.api.test.Pages","id":2," etc…

 

------------------------------------------------------------------------------------

Test 2 ) Make a change to Controller;

def search = {      

       def city = City.findByName(params.id)

       def services = Services.findByName(“bus36”)

def results= Pages.findAll("from Pages as p where p.city = ? and p.services = ?" ,[city, services])

               render results as JSON          

       }     

POST Via AJAX; url posted is: http://localhost:8080/com.api.test/Pages/search/London

The above example works OK (result [{"class":"com.api.test.Pages","id":2," etc…

 

----------------------------------------------------------------------------------

Now this is what I want to do:

Test 3 ) Make a change to Controller;

def search = {      

       def city = City.findByName(params.id)

       def services = Services.findByName(params.id)

def results= Pages.findAll("from Pages as p where p.city = ? and p.services = ?" ,[city, services])

               render results as JSON          

       }     

POST Via AJAX; url posted is: http://localhost:8080/com.api.test/Pages/search/London?Bus36

(Result [] empty

Please let me know if use of ‘?’ to separate the params value is correct? (London?Bus36) I tried ‘,’ or ‘+’ or ‘&’ and they all return [].

How would you pass the to two query params in url? So I can make the AJAX to POST that pattern

Mikael Andersson

unread,
Oct 26, 2014, 6:12:22 AM10/26/14
to grails-de...@googlegroups.com
Hi

At a first glance it looks a bit strange that you are finding the City and Services domain objects using the same name value "params.id" , in your working hardcoded examples the values are always different.
If the names are different that explains why the query doesn't find a match.

I would make sure that the params.id attribute has the value you expect, perhaps you need to pass in two different request parameters which holds the City and Services names you want to find?

Cheers,
 Micke

On Thursday, 16 October 2014 11:19:24 UTC+1, jnlltd wrote:

jnlltd

unread,
Oct 26, 2014, 10:08:45 PM10/26/14
to grails-de...@googlegroups.com
You correct about the params.id
I am new to grails I started to learn grails about 2 months ago so I was bit lost and too much to take in.  I found the solution when I started to look into URL mapping by modifying url mapping to allow parameter under different name the same query worked.  Params.id & params.id1.  But the strange thing was the query worked for two days without any modification url mapping. Well I have solved the problem gained some new knowledge.
Reply all
Reply to author
Forward
0 new messages