<?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>Oliver's Place &#187; Hibernate</title>
	<atom:link href="http://weichhold.com/category/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://weichhold.com</link>
	<description></description>
	<lastBuildDate>Thu, 12 Aug 2010 18:13:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Mapping the result of a native SQL query to a Grails domain class collection</title>
		<link>http://weichhold.com/2010/01/18/mapping-result-of-a-native-sql-query-to-grails-domain-class-collection/</link>
		<comments>http://weichhold.com/2010/01/18/mapping-result-of-a-native-sql-query-to-grails-domain-class-collection/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 20:05:27 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Hibernate]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=143</guid>
		<description><![CDATA[Today I could once again not find a proper way to express a query needed for my current Grails project in either Hibernate HQL nor the DSL based variant of Hibernate&#8217;s Criteria API. While it is quite simple to run native SQL Queries in Grails Project using Spring&#8217;s NamedParameterJdbcTemplate I never had to actually Map [...]]]></description>
			<content:encoded><![CDATA[<p>Today I could once again not find a proper way to express a query needed for my current Grails project in either Hibernate HQL nor the DSL based variant of Hibernate&#8217;s Criteria API.</p>
<p>While it is quite simple to run native SQL Queries in Grails Project using Spring&#8217;s <a href="http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.html">NamedParameterJdbcTemplate</a> I never had to actually Map the result of a native SQL back to one of my Grails Domain Classes. After a bit of fiddling around it turned out that everything becomes quite trivial once you get ahold of the Hibernate SessionFactory that is made available by Grails for each request.</p>
<p>Let&#8217;s pretend we have the following Grails domain class:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p143code3'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1433"><td class="code" id="p143code3"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">com.acme.domain</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Foo
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #aaaadd; font-weight: bold;">String</span> name
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>You would execute a native SQL Query that returns a List of Foo instances like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p143code4'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1434"><td class="code" id="p143code4"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">com.acme.domain.*</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> sessionFactory
sessionFactory <span style="color: #66cc66;">=</span> ctx.<span style="color: #006600;">sessionFactory</span>  <span style="color: #808080; font-style: italic;">// this only necessary if your are working with the Grails console/shell</span>
<span style="color: #000000; font-weight: bold;">def</span> session <span style="color: #66cc66;">=</span> sessionFactory.<span style="color: #006600;">currentSession</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> query <span style="color: #66cc66;">=</span> session.<span style="color: #006600;">createSQLQuery</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;select f.* from Foo where f.id = :filter)) order by f.name&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
query.<span style="color: #006600;">addEntity</span><span style="color: #66cc66;">&#40;</span>com.<span style="color: #006600;">acme</span>.<span style="color: #006600;">domain</span>.<span style="color: #006600;">Foo</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
query.<span style="color: #006600;">setInteger</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;filter&quot;</span>, <span style="color: #cc66cc;">88</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
query.<span style="color: #006600;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span>.<span style="color: #006600;">name</span><span style="color: #66cc66;">;</span></pre></td></tr></table></div>

<img src="http://weichhold.com/?ak_action=api_record_view&id=143&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2010/01/18/mapping-result-of-a-native-sql-query-to-grails-domain-class-collection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
