<?xml version="1.0" encoding="UTF-8"?>

<rss version='2.0'
     xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">
    <channel>
        <!-- This XML Feed shows details for the page Using SQL From Within Hibernate -->
        <creativeCommons:license>http://creativecommons.org/licenses/by-sa/2.5/
          </creativeCommons:license>
        <title>Using SQL From Within Hibernate</title>
        <description>&lt;p&gt;Most of the time &lt;a class=&quot;wikilink&quot; href=&quot;http://swik.net/Hibernate&quot;&gt;Hibernate&lt;/a&gt; &amp;#8220;just works&amp;#8221;. Therefore when it is not that easy is comes as somewhat of a surprise. Hibernate documentation says it is easy to use &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; from within Hibernate, but lets try it.&lt;/p&gt;


	&lt;p&gt;We can do it with using named &lt;a class=&quot;wikilink&quot; href=&quot;http://swik.net/SQL&quot;&gt;&lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt;&lt;/a&gt; queries, or simply by embedding our &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; instructions directly in the source code.&lt;/p&gt;


	&lt;h2&gt;Embedding example : SQLCooperationTest.java&lt;/h2&gt;


&lt;pre&gt;
public void runQuery(){ 
39       SQLQuery q = TestUtilities.getHSession().createSQLQuery
                       ( &quot;SELECT street, zip FROM sql_addresses&quot;); 
40       q.addScalar( &quot;street&quot;, Hibernate.STRING); 
41       q.addScalar( &quot;zip&quot;, Hibernate.STRING); 
42       printResults( q ); 
43     }
&lt;/pre&gt;

	&lt;h2&gt;Named &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; call example:
SQLCooperationTest.java&lt;/h2&gt;


&lt;pre&gt; 
46    
47     public void runNamedQuery(){ 
48       SQLQuery q1 = ( SQLQuery ) TestUtilities.getHSession().getNamedQuery( &quot;select_address&quot; ); 
49       printResults( q1 ); 
50     }
&lt;/pre&gt;

	&lt;p&gt;&lt;strong&gt;and the definition of the named query is:
sql.hbm.xml&lt;/strong&gt;&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
5     
6    &amp;lt;hibernate-mapping package=&quot;com.sourcelabs.hibernate.bhw.bags&quot; &amp;gt; 
7     
8    &amp;lt;sql-query name=&quot;select_address&quot;&amp;gt; 
9      &amp;lt;return-scalar column=&quot;street&quot; type=&quot;java.lang.String&quot;/&amp;gt; 
10     &amp;lt;return-scalar column=&quot;zip&quot; type=&quot;java.lang.String&quot;/&amp;gt; 
11     select street, zip from sql_addresses 
12   &amp;lt;/sql-query&amp;gt; 
13   &amp;lt;/hibernate-mapping&amp;gt;
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;as we can see in both cases we need to specify aliases and types for the query before it can be executed. If we do not do it then we will get exception like this:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  Caused by: org.hibernate.QueryException: addEntity() or addScalar() must be called on a sql query
   before executing the query. [SELECT street, zip FROM sql_addresses]
    at org.hibernate.impl.SQLQueryImpl.verifyParameters(SQLQueryImpl.java:169)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:140)
    at com.sourcelabs.hibernate.bhw.sql.SQLCooperationTest.printResults(SQLCooperationTest.java:52)
    at com.sourcelabs.hibernate.bhw.sql.SQLCooperationTest.runQuery(SQLCooperationTest.java:43)
    at com.sourcelabs.hibernate.bhw.sql.SQLCooperationTest$$FastClassByCGLIB$$79fd0338.
    invoke(&amp;lt;generated&amp;gt;)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept
    (Cglib2AopProxy.java:635)
    at com.sourcelabs.hibernate.bhw.sql.SQLCooperationTest$$EnhancerByCGLIB$$511db1d7.
    runQuery(&amp;lt;generated&amp;gt;)
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;This default behavior is somehow odd and counterintuitive because H can return an array of object by default, but for unclear reasons H does not do that. Another potentially useful feature could be returning an array of maps. And again there is enough information at runtime to make intelligent mapping of columns to keys and values.&lt;/p&gt;


	&lt;p&gt;Hibernate perhaps could learn something from &lt;a class=&quot;wikilink&quot; href=&quot;http://swik.net/iBatis&quot;&gt;iBatis&lt;/a&gt;. If you need to use complex and/or &lt;span class=&quot;caps&quot;&gt;RDBMS&lt;/span&gt; specific &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; extensively throughout then you might consider using Hibernate and iBatis together.&lt;/p&gt;


	&lt;p&gt;I don&amp;#8217;t understand why that is a bad thing and why you would want to mix in two different &lt;span class=&quot;caps&quot;&gt;ORM&lt;/span&gt; tools in your application. Just always use aliases, and if it isn&amp;#8217;t to an object already mapped then include the types.&lt;/p&gt;
</description>
                <category>Hibernate</category>
        <category>SQL</category>
        <category>example</category>
        <category>tag4sree</category>

        <pubDate>Wed, 07 Jun 2006 11:00:55 -0700</pubDate>
        <lastBuildDate>Tue, 12 Aug 2008 03:28:19 -0700</lastBuildDate>
            
    </channel>
</rss>
