In my recent project there was a use case where I had to query domain's hasMany but relationship was not with any domain class but rather String type. Consider example [java] class Blog { static hasMany = [tags: String] } [/java] Now to find all the blogs with tag "grails" , our normal way of querying hasMany relationships...
When using criteria queries, many times we encounter a use case where we need to order our result not on any field of domain class but on our result set, in these cases aliases can be used. In my project, use case was to find most liked products from the domain: [java] ProductStat{ Date dateCreated Product product } [/java] ...
I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params. def result=SampleDomain.createCriteria().list(){ // multiple restrictions maxResults(params.max) firstResult(params.offset) } // Return type is ArrayList Integer ...
Following is an example of unidirectional one-to-many relationship: class Employee { String name static hasMany = [roles: Role] } class Role { String name } How can we find all the employees with a particular role - "Grails Developer"? Role role = Role.findByName("Grails Developer") One way of doing...