<?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>Source Allies Blog &#187; hibernate</title>
	<atom:link href="http://blogs.sourceallies.com/tag/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.sourceallies.com</link>
	<description>Technical and process thinking from Source Allies employees</description>
	<lastBuildDate>Mon, 26 Jul 2010 15:01:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hibernate Embeddable Objects</title>
		<link>http://blogs.sourceallies.com/2010/01/hibernate-embeddable-objects/</link>
		<comments>http://blogs.sourceallies.com/2010/01/hibernate-embeddable-objects/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 14:00:06 +0000</pubDate>
		<dc:creator>Max  Kuipers</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Embeddable Objects]]></category>
		<category><![CDATA[hibernate]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=579</guid>
		<description><![CDATA[Hibernate Embeddable Objects are a really neat way to organize your data model.  Especially, if you have the same few columns in a number of different tables, that all pertain to the same thing. The example commonly used is Addresses.  You may have a number of tables that each have fields pertaining to address information [...]]]></description>
			<content:encoded><![CDATA[<p>Hibernate Embeddable Objects are a really neat way to organize your data model.  Especially, if you have the same few columns in a number of different tables, that all pertain to the same thing. The <a title="example commonly used" href="http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e569">example commonly used </a>is Addresses.  You may have a number of tables that each have fields pertaining to address information and you don&#8217;t want to have to do all the mappings for each entity again and again.  If the column names are the same across each table, you can just add an @Embeddable annotation.<br />
<span id="more-579"></span><br />
Consider the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Embeddable
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Address <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span> <span style="color: #009900;">&#123;</span>
...
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lineOne<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lineTwo<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> city<span style="color: #339933;">;</span>
....
   @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;LINEONE&quot;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getLineOne<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lineOne</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
...
   @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CITY&quot;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getCity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">city</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
...</pre></div></div>

<p>This creates address as an embeddable object. Then to actually embed it in your entity, you simply do something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
@Table<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Person&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span><span style="color: #009900;">&#123;</span>
...
   <span style="color: #000000; font-weight: bold;">private</span> Address address<span style="color: #339933;">;</span>
...
   @Embedded
   <span style="color: #000000; font-weight: bold;">public</span> getAddress<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">address</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There is, however, one huge problem with this, especially when working with JSF.  If all of the fields in the embeddable object are null for some reason or another, then the embeddable object never actually gets instantiated.  There is a solution/hack that works around this &#8211; you modify the setter method to look something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
@Table<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Person&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span><span style="color: #009900;">&#123;</span>
...
   <span style="color: #000000; font-weight: bold;">private</span> Address address<span style="color: #339933;">;</span>
...
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setAddress<span style="color: #009900;">&#40;</span>Address address<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>address <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">address</span> <span style="color: #339933;">=</span> address<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">address</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Address<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> 
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It&#8217;s not particularly pretty, but it will dry your tears in a pinch. More on this solution can be found <a title="here" href="https://jira.jboss.org/jira/browse/HIBERNATE-50">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/01/hibernate-embeddable-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building Multi-Criteria Search Queries in Hibernate</title>
		<link>http://blogs.sourceallies.com/2009/12/building-multi-criteria-search-queries-in-hibernate/</link>
		<comments>http://blogs.sourceallies.com/2009/12/building-multi-criteria-search-queries-in-hibernate/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 18:02:53 +0000</pubDate>
		<dc:creator>Vaithi  Subramanian</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[Hql]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=399</guid>
		<description><![CDATA[
In this post I am going to show how to write queries multi-criteria search screens. There are two approaches for making this possible.


HQL for building the Query
Building Query using Criteria API

HQL for building the Query
Here I am going to show 2 approaches to building the HQL and try to point out the better approach.
Approach I:String [...]]]></description>
			<content:encoded><![CDATA[<p>
In this post I am going to show how to write queries multi-criteria search screens. There are two approaches for making this possible.
</p>
<ul>
<li>HQL for building the Query</li>
<li>Building Query using Criteria API</li>
</ul>
<p><b>HQL for building the Query</b></p>
<p>Here I am going to show 2 approaches to building the HQL and try to point out the better approach.</p>
<p><strong>Approach I:String concatenation</strong><br />
This approach uses String concatenation and setting up the values directly in the query.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>startDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>firstClause<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      query <span style="color: #339933;">=</span> query <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; where &quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      query <span style="color: #339933;">=</span> query <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; and &quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   query <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot; s.date &gt;= '&quot;</span> <span style="color: #339933;">+</span> startDate <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Using the above approach  there might be a chance of SQL Injection attack and using string concatenation is inherently error-prone.</p>
<p><strong>Approach 2: Criteria as Named Parameters</strong></p>
<p>In this one we create two map to hold parameter name and value which could be binded to the HQL during the execution.</p>
<p>Here is brief example of the approach.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">List</span> search<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   StringBuilder aQuery <span style="color: #339933;">=</span> 
      <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; from document p where p.id is not null &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">HashMap</span> parameterMap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">HashMap</span> parameterListMap <span style="color: #339933;">=</span>  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//Collection Criteria</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>countyList <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      buildCollectionCriterion<span style="color: #009900;">&#40;</span>aQuery, parameterListMap, <span style="color: #0000ff;">&quot;listID&quot;</span>, countyList<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//Date Criteria</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>startDate<span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> endDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      buildDateCriterion<span style="color: #009900;">&#40;</span>aQuery, parameterMap, <span style="color: #0000ff;">&quot;dateField&quot;</span>,startDate, endDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   Query query <span style="color: #339933;">=</span> hibSession.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>aQuery.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key <span style="color: #339933;">:</span> parameterMap.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      query.<span style="color: #006633;">setParameter</span><span style="color: #009900;">&#40;</span>key, parameterMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key <span style="color: #339933;">:</span> parameterListMap.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      query.<span style="color: #006633;">setParameterList</span><span style="color: #009900;">&#40;</span>key, parameterListMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #003399;">List</span> results <span style="color: #339933;">=</span> query.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Helper Methods for different type of criteria</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> buildCollectionCriterion<span style="color: #009900;">&#40;</span>StringBuilder aQuery, 
                                        <span style="color: #003399;">Map</span> parameterListMap, 
                                        <span style="color: #003399;">String</span> aFieldName, 
                                        <span style="color: #003399;">Collection</span> aList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>aList <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>aList.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      aQuery
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; and p.&quot;</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; in (:&quot;</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterListMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>aFieldName, aList<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> buildDateCriterion<span style="color: #009900;">&#40;</span>StringBuilder aQuery, 
                               <span style="color: #003399;">Map</span> parameterMap, 
                               <span style="color: #003399;">String</span> aFieldName, 
                               <span style="color: #003399;">Date</span> aStartDate, 
                               <span style="color: #003399;">Date</span> anEndDate<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>aStartDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> anEndDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      aQuery
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; and ( p.&quot;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; between :aStartDate and :anEndDate)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;aStartDate&quot;</span>, aStartDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;anEndDate&quot;</span>, anEndDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>aStartDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      aQuery
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; and  (p.&quot;</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &gt;= :aStartDate)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;aStartDate&quot;</span>, aStartDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> 
   <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>anEndDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      aQuery
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; and (p.&quot;</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &lt;=:anEndDate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;anEndDate&quot;</span>, anEndDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><b>Building Query using Criteria API</b></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">List</span> search<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Criteria c <span style="color: #339933;">=</span> hibSession.<span style="color: #006633;">createCriteria</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Document</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        c.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">notNull</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>countryList <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                c.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">in</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;listId&quot;</span>, countryList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>startDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                c.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">ge</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dateField&quot;</span>, startDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>endDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                c.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">le</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dateField&quot;</span>, endDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> c.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Conclusion</strong></p>
<p>The Hibernate Criteria API is a powerful and elegant library which is well adapted for implementing multi-criteria search functionality and also HQL queries must be built &#8216;on-the-fly&#8217;. Using it in appropriate circumstances will result in cleaner, clearer, more reliable and more maintainable code. </p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2009/12/building-multi-criteria-search-queries-in-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hibernate Criteria trick</title>
		<link>http://blogs.sourceallies.com/2009/10/hibernate-criteria-trick/</link>
		<comments>http://blogs.sourceallies.com/2009/10/hibernate-criteria-trick/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 14:32:55 +0000</pubDate>
		<dc:creator>Aaron King</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[criteria]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[not in]]></category>
		<category><![CDATA[restrictions]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=61</guid>
		<description><![CDATA[So here&#8217;s the situation.
Let&#8217;s say I have this query here:

SELECT * FROM employees
WHERE employee_id NOT IN &#40; 1234 , 3456 , 5678 &#41;;

How do we do that with the Hibernate Criteria object with a Restriction?   You would think that the Restrictions API would have a &#8220;not in&#8221; method, since it does have a not [...]]]></description>
			<content:encoded><![CDATA[<p>So here&#8217;s the situation.</p>
<p>Let&#8217;s say I have this query here:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> employees
<span style="color: #993333; font-weight: bold;">WHERE</span> employee_id <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">1234</span> <span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">3456</span> <span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">5678</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>How do we do that with the Hibernate Criteria object with a Restriction?   You would think that the Restrictions API would have a &#8220;not in&#8221; method, since it does have a not equals method(ne), but alas, there is nothing&#8230;</p>
<p>Well, here&#8217;s the solution:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Create the criteria</span>
Criteria crit <span style="color: #339933;">=</span> factory.<span style="color: #006633;">getCurrentSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">createCriteria</span><span style="color: #009900;">&#40;</span>Employee.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//add my restriction where idList is a list of emp ids that need to be excluded</span>
crit.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">not</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">in</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;employeeId&quot;</span>, idList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//get some results</span>
<span style="color: #003399;">List</span> employees <span style="color: #339933;">=</span> crit.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There you go!  Now you know this neat little trick and you can use it in your own app&#8230;  Be forewarned though, it can be slow&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2009/10/hibernate-criteria-trick/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
