Useful code for fetching grails domain entries

Posted By : Shakil Pathan | 30-Sep-2015

Hi

In this blog I am going to explain you about some useful code in your grails/java, which you can use in your project for solving some problems.

First of all I am going to explain you about how to filter a domain class with other domain class refrence in that domain.

List users = User.createCriteria().list{
			createAlias('detail', 'detail', Criteria.LEFT_JOIN)
			not {'in'("detail", detail)}
			eq("detail.isDeleted",false)
		}
 

Something like that you can implement your own logic to filter the entries with other domain class refrence object.
In the above code we first joing the current table with the refrence table and creating an alias of that domain class and then checking the values on that alias.

Second one is paginating the domain class objects with grails default pagination.

We can paginate the result by passing a map to the domain list method like this

Map map = [offset: offsetValue, max: maxValue, order: orderValue(asc/desc), sort: sortAttribute]
List users = User.list(map)
 

In the above code we are providing some constraints to the domain class according to the requirement of the data.

Hope it helps!

THANKS

About Author

Author Image
Shakil Pathan

Shakil is an experienced Groovy and Grails developer . He has also worked extensively on developing STB applications using NetGem .

Request for Proposal

Name is required

Comment is required

Sending message..