<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Intelligrape  Groovy &#38; Grails Blogs &#187; createCriteria</title>
	<atom:link href="http://www.intelligrape.com/blog/tag/createcriteria/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.intelligrape.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 02 Feb 2012 07:48:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
			<title>Intelligrape  Groovy &amp; Grails Blogs</title>
			<url>http://www.intelligrape.com/blog/wp-content/uploads/2011/05/favicon2.ico</url>
			<link>http://www.intelligrape.com/blog</link>
			<width></width>
			<height></height>
			<description></description>
		</image>		<item>
		<title>Criteria Query and pagination params</title>
		<link>http://www.intelligrape.com/blog/2010/07/14/criteria-query-and-pagination-params/</link>
		<comments>http://www.intelligrape.com/blog/2010/07/14/criteria-query-and-pagination-params/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 17:18:26 +0000</pubDate>
		<dc:creator>Bhagwat Kumar</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[createCriteria]]></category>
		<category><![CDATA[GORM count()]]></category>
		<category><![CDATA[GORM list()]]></category>
		<category><![CDATA[PagedResultList]]></category>
		<category><![CDATA[pagination params]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=1260</guid>
		<description><![CDATA[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&#40;&#41;.list&#40;&#41;&#123;
// multiple restrictions
   maxResults&#40;params.max&#41;
   firstResult&#40;params.offset&#41;
&#125; // Return type is ArrayList
&#160;
Integer  totalResult=SampleDomain.createCriteria&#40;&#41;.count&#40;&#41;&#123;
// multiple restrictions
// maxResults(params.max)
// firstResult(params.offset)
&#125;


Clearly duplicating the same closure except for the pagination restrictions was not [...]]]></description>
			<content:encoded><![CDATA[<p>I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params.</p>
<div class="code">

<div class="wp_syntax"><div class="code"><pre class="groovy"> <span style="color: #000000; font-weight: bold;">def</span> result<span style="color: #66cc66;">=</span>SampleDomain.<span style="color: #006600;">createCriteria</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
<span style="color: #808080; font-style: italic;">// multiple restrictions</span>
   maxResults<span style="color: #66cc66;">&#40;</span>params.<span style="color: #663399;">max</span><span style="color: #66cc66;">&#41;</span>
   firstResult<span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">offset</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span> <span style="color: #808080; font-style: italic;">// Return type is ArrayList</span>
&nbsp;
<span style="color: #aaaadd; font-weight: bold;">Integer</span>  totalResult<span style="color: #66cc66;">=</span>SampleDomain.<span style="color: #006600;">createCriteria</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">count</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
<span style="color: #808080; font-style: italic;">// multiple restrictions</span>
<span style="color: #808080; font-style: italic;">// maxResults(params.max)</span>
<span style="color: #808080; font-style: italic;">// firstResult(params.offset)</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

</div>
<p>Clearly duplicating the same closure except for the pagination restrictions was not a good solution.</p>
<p>After a little googling and reading mailing lists I got the solution. Passing pagination params to createCriteria.list() returns result of type <a href="http://www.grails.org/doc/1.0.x/api/grails/orm/PagedResultList.html" target="_blank">PagedResultList</a> which provides many useful methods. The <a href="http://www.grails.org/doc/1.0.x/api/grails/orm/PagedResultList.html#getTotalCount()" target="_blank">getTotalCount()</a> method of PagedResultList class returns the actual number of results returned irrespective of the pagination restrictions(maxResults and firstResult). Also the result contains only those records fulfiling maxResults and firstResult restrictions.</p>
<div class="code">

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">def</span> result<span style="color: #66cc66;">=</span>SampleDomain.<span style="color: #006600;">createCriteria</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #663399;">max</span>:params.<span style="color: #663399;">max</span>, offset:params.<span style="color: #006600;">offset</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
<span style="color: #808080; font-style: italic;">// multiple/complex restrictions</span>
   maxResults<span style="color: #66cc66;">&#40;</span>params.<span style="color: #663399;">max</span><span style="color: #66cc66;">&#41;</span>
   firstResult<span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">offset</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span> <span style="color: #808080; font-style: italic;">// Return type is PagedResultList</span></pre></div></div>

</div>
<p>Thanks to all the active users of Grails mailing list.</p>
<p>Here are few useful links:<br />
<a href="http://www.pubbs.net/grails/200912/2269" target="_blank">http://www.pubbs.net/grails/200912/2269</a><br />
<a href="http://www.grails.org/doc/1.0.x/api/grails/orm/PagedResultList.html" target="_blank">http://www.grails.org/doc/1.0.x/api/grails/orm/PagedResultList.html</a><br />
<a href="http://jira.codehaus.org/browse/GRAILS-2672" target="_blank">http://jira.codehaus.org/browse/GRAILS-2672</a></p>
<p>Bhagwat Kumar<br />
bhagwat(at)intelligrape(dot)com</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2010/07/14/criteria-query-and-pagination-params/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Grails criteria query example for unidirectional one-to-many relationships</title>
		<link>http://www.intelligrape.com/blog/2010/06/14/grails-createcriteria-query-example/</link>
		<comments>http://www.intelligrape.com/blog/2010/06/14/grails-createcriteria-query-example/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 09:52:03 +0000</pubDate>
		<dc:creator>Aman Aggarwal</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[createCriteria]]></category>
		<category><![CDATA[criteria query]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[unidirectional]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=1057</guid>
		<description><![CDATA[Following is an example of unidirectional one-to-many relationship:

class Employee &#123;
    String name
    static hasMany = &#91;roles: Role&#93;
&#125;
&#160;
class Role &#123;
    String name
&#125;

How can we find all the employees with a particular role &#8211; &#8220;Grails Developer&#8221;?

Role role = Role.findByName&#40;&#34;Grails Developer&#34;&#41;

One way of doing this can be:

Employee.list&#40;&#41;.findAll&#123;role in it.roles&#125;

This [...]]]></description>
			<content:encoded><![CDATA[<p>Following is an example of unidirectional one-to-many relationship:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">class</span> Employee <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> name
    <span style="color: #000000; font-weight: bold;">static</span> hasMany <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>roles: Role<span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Role <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> name
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>How can we find all the employees with a particular role &#8211; &#8220;Grails Developer&#8221;?</p>

<div class="wp_syntax"><div class="code"><pre class="groovy">Role role <span style="color: #66cc66;">=</span> Role.<span style="color: #006600;">findByName</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Grails Developer&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>One way of doing this can be:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy">Employee.<span style="color: #006600;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">findAll</span><span style="color: #66cc66;">&#123;</span>role <span style="color: #b1b100;">in</span> it.<span style="color: #006600;">roles</span><span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This is an inefficient way since, we are fetching all the records from the database every time. Also, in case we are showing paginated view of results, we will need to manage pagination ourself. </p>
<p>A better way of this will be using createCriteria() query:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy">List<span style="color: #66cc66;">&lt;</span>Employee<span style="color: #66cc66;">&gt;</span> employees <span style="color: #66cc66;">=</span> Employee.<span style="color: #006600;">createCriteria</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">list</span><span style="color: #66cc66;">&#123;</span>
    roles<span style="color: #66cc66;">&#123;</span>
        eq<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'id'</span>, role.<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>	
    firstResult<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>
    maxResults<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>	
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>I am wondering what will be the query in case Role.groovy is an enum.</p>
<p>–<br />
~Aman Aggarwal<br />
aman@intelligrape.com</p>
<p>http://www.IntelliGrape.com/</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2010/06/14/grails-createcriteria-query-example/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

