How do I handle this SQL query in grails in my ProductsController script? Notice its two tables with a join on the product id.
SELECT p.*,pd.*
FROM products p, products_description pd
WHERE p.products_id=pd.products_id
ORDER BY p.products_date_added DESC
Obviously I can’t do this:
def all= {
def a
a = Products.find("FROM products p, products_description pd
WHERE p.products_id=pd.products_id ORDER BY p.products_date_added DESC")
render a as JSON
}
If you are adamant on using a custom sql query instead of any grails dynamic finder, you can use the following code:
You will have to add a variable named sessionFactory to your controller. Something like this:
The result list would be a list of lists. Each element of the main list would be a list of size 2 with the first element as the product and the second as the product description.