Monday, March 17, 2008

HQL in Grail

Today we'll discuss the HQL query in Grail.as you know,Grails's GROM is based on Hibernate.So you can use HQL to implement sql query in Grails.Grails provide three static method:find,findAll and executeQuery.Here are some demonstrate

/**find method*/
//following method will return a project object whose attribute is 'App1'
def project=Project.find('from Project p where p.title=?',['App1'])
/**findAll method*/
//following method will return all of project objects.
def projectList=Project.findAll('from Project')
//Grails also provide a elegant IN operator
def plist=Project.findAll('from Project')
def ilist=Issue.findAll("from Issue as b where b.project in (:p)", [p:plist])
/**executeQuery method*/
def plist=Project.executeQuery("select distinct p.title from Project p")
//it is possible to perform more-advanced queries using joins, aggregate functions, and subqueries.
And also we can some limit for query result number,for example:
def plist=Project.executeQuery("select distinct p.title from Project p",[max:10,offset:5])
)

No comments: