<?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; not in</title>
	<atom:link href="http://blogs.sourceallies.com/tag/not-in/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.sourceallies.com</link>
	<description>Technical and process thinking from Source Allies employees</description>
	<lastBuildDate>Thu, 19 Aug 2010 18:35:29 +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 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>
