<?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>Ceres Logic :: Blog &#187; programming</title>
	<atom:link href="http://www.cereslogic.com/pages/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cereslogic.com/pages</link>
	<description>Mobile and Web App Development</description>
	<lastBuildDate>Tue, 23 Aug 2011 14:45:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Is Your Programming Team Your Rock Band?</title>
		<link>http://www.cereslogic.com/pages/2009/03/24/is-your-programming-team-your-rock-band/</link>
		<comments>http://www.cereslogic.com/pages/2009/03/24/is-your-programming-team-your-rock-band/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 18:50:52 +0000</pubDate>
		<dc:creator>mdesjardins</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://mikedesjardins.us/wordpress/?p=234</guid>
		<description><![CDATA[I&#8217;m sure that I&#8217;m not the person to make this connection, but it occurred to me the other day that being on a small team of coders is a lot like being in a band.  I&#8217;ve been in a couple bands that never went far beyond the garage (I&#8217;m allegedly a bass player), so perhaps [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure that I&#8217;m not the person to make this connection, but it occurred to me the other day that being on a small<img class="alignright size-medium wp-image-235" title="tereu-tereu-ian-matthew-soper" src="http://mikedesjardins.us/wordpress/wp-content/uploads/2009/03/tereu-tereu-ian-matthew-soper-300x200.jpg" alt="tereu-tereu-ian-matthew-soper" width="300" height="200" /> team of coders is a lot like being in a band.  I&#8217;ve been in a couple bands that never went far beyond the garage (I&#8217;m allegedly a bass player), so perhaps I&#8217;m not the foremost authority on this topic.  However, I think there are a few parallels between building, e.g., a small MVC web application, and writing and performing the next standard verse-chorus-verse rock anthem.</p>
<p>In particular, I think there are parallels between the specific members of a prototypical Rock band, and the members of a team who create MVC applications:</p>
<ul>
<li><strong>Drummer</strong> &#8211; The Anchor.  Provides the foundation for the music, onto which the other layers are stacked and woven.  In an MVC app, this is your database guy/gal.  The person modeling your data and managing the schematic underpinnings of your application is your drummer.  And if you&#8217;re using a weird schema-less database like CouchDB, then you have yourself a sloppy jazz drummer, which means more work for the Bassist.</li>
<li><strong>Bass Player</strong> &#8211; The Bass Player sets the groove for the song, and maps the primal beats that the drummer is hammering out into something melodic for the rest of the band to work with.  The bassist is also crucial for carrying the beat for the rest of the band when the drummer is screwing around (see note about jazz drummers, above).  In our web application, this is the domain layer, where your ORM, caching, and validation all chill out.  And like the bass player, this is rarely the sexiest or most glamorous part of the application.</li>
<li><strong>Guitarist(s) </strong>- This is your business logic developer.  Some bands choose to break this up into your traditional Angus/Malcom roles of &#8220;rhythm&#8221; and &#8220;lead&#8221; guitar.  Likewise, you may choose to have separate model and controller layers in your app (particularly if you&#8217;re a three-tier app: the web tier controller is your Angus and the middle tier model is your Malcom).  Bass players can sometimes get by as guitarists in a pinch, and vice versa.  Likewise, you&#8217;ll see business logic guys doing domain work and vice versa.  Just don&#8217;t let them try to play their parts on the wrong instrument &#8211; you&#8217;ve gotta separate your concerns, dude.  Aside from the lyrics, this is the part of the song that your listeners are the most likely to hear.</li>
<li><strong>Lead Singer</strong> &#8211; This is your AJAXy, CSS laden, poetic presentation of your band&#8217;s message.  It&#8217;s what your listeners hear (and see) first.  And the singer gets all flustered when the rest of the band screws up (the singer is a real primadonna, very sensitive). Changing your lead singer will almost certainly alieniate your fans (think Facebook redesign &#8211; the Sammy Hagar of UI decisions).</li>
</ul>
<p>I tend to get carried away with analogies, and this one is no different &#8211; I could keep going (your roadies are your project managers, your label is the marketing and executives who make all the money) but I&#8217;ll try to show some restraint.  But given this analogy, I find it interesting that the role I play on software teams is often similar to the role I play in a band.  I like bass.</p>
<p><em>Photo Credit: <a href="http://www.flickr.com/people/iansoper/">Ian Matthew Soper</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cereslogic.com/pages/2009/03/24/is-your-programming-team-your-rock-band/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Test your EJBs with JMeter</title>
		<link>http://www.cereslogic.com/pages/2008/12/22/test-your-ejbs-with-jmeter/</link>
		<comments>http://www.cereslogic.com/pages/2008/12/22/test-your-ejbs-with-jmeter/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 01:14:21 +0000</pubDate>
		<dc:creator>mdesjardins</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[ejb]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jmeter]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://mikedesjardins.us/wordpress/?p=196</guid>
		<description><![CDATA[Note: This is my first blog post on my newly upgraded WordPress blog (prior to this, I used Blogger). Hopefully everything goes OK! Sometimes it&#8217;s helpful to do some performance benchmarks on your EJBs. There are a few different ways to do this, but I&#8217;ve found that Apache&#8217;s JMeter is an excellent tool for benchmarking. [...]]]></description>
			<content:encoded><![CDATA[<p><em>Note: This is my first blog post on my newly upgraded WordPress blog (prior to thi</em><em><img class="alignright size-thumbnail wp-image-198" title="jaguar-tachometer.jpg" src="http://mikedesjardins.us/wordpress/wp-content/uploads/2008/12/jaguar-tachometer-150x150.jpg" alt="jaguar-tachometer.jpg" width="150" height="150" /></em><em>s, I used </em><em>Blogger).  Hopefully everything goes OK!</em></p>
<p>Sometimes it&#8217;s helpful to do some performance benchmarks on your EJBs.  There are a few different ways to do this, but I&#8217;ve found that Apache&#8217;s <a href="http://jakarta.apache.org/jmeter">JMeter</a> is an excellent tool for benchmarking.  Unfortunately, JMeter doesn&#8217;t come with a general-purpose sampler for testing arbitrary EJBs.  Luckily, it isn&#8217;t very difficult to create one.</p>
<p>For this article, I&#8217;m using the JBoss application server to host my EJBs.  The process for using other containers should be quite similar.</p>
<h3>1.) Create a factory to lookup your EJBs.</h3>
<p>The first thing that you&#8217;ll probably want to do is create a simple singleton factory class to create instances of your EJB client for your test.  I use something like the following:</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: #000000; font-weight: bold;">class</span> MyServiceFactory <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Log log <span style="color: #339933;">=</span> LogFactory.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span>MyServiceFactory.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> MyService service<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> MyServiceFactory me<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> MyServiceFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span>
    MyServiceFactory.<span style="color: #006633;">me</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyServiceFactory<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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> MyServiceFactory getInstance<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> MyServiceFactory.<span style="color: #006633;">me</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> MyService getService<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;">if</span> <span style="color: #009900;">&#40;</span>MyService.<span style="color: #006633;">service</span> <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: #666666; font-style: italic;">// Get the remote interface of the music search service</span>
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Loading the service...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// JNDI the old-fashioned way:</span>
        <span style="color: #003399;">Context</span> ctx <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InitialContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        service <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>MyService<span style="color: #009900;">&#41;</span>ctx.<span style="color: #006633;">lookup</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyAction/remote&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>service <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>
          log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Didn't get the service!&quot;</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: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NamingException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error looking up the remote service&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> service<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>2.) Write the test</h3>
<p>Next, we&#8217;ll need to write the test itself.  To do this, we&#8217;ll extend the AbstractJavaSamplerClient class in JMeter&#8217;s org.apache.jmeter.protocol.java.sampler package.  This abstract class has a runTest method that we will override, and this method implements the actual test.   We will also override the getDefaultParameters method to provide some reasonable defaults values which will be displayed in JMeter&#8217;s GUI application.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">us.mikedesjardins.demo.jmeter</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.jmeter.config.Arguments</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.jmeter.protocol.java.sampler.JavaSamplerContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.jmeter.samplers.SampleResult</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DigitalContentServiceEJBTestSampler <span style="color: #000000; font-weight: bold;">extends</span> AbstractJavaSamplerClient <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> SampleResult runTest<span style="color: #009900;">&#40;</span>JavaSamplerContext context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    SampleResult results <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SampleResult<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    MyService service <span style="color: #339933;">=</span> MyServiceFactory.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getService</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    results.<span style="color: #006633;">sampleStart</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Long</span> param1 <span style="color: #339933;">=</span> context.<span style="color: #006633;">getLongParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PARAM_1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> param2 <span style="color: #339933;">=</span> context.<span style="color: #006633;">getStringParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PARAM_2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    MyResult result <span style="color: #339933;">=</span> service.<span style="color: #006633;">myMethod</span><span style="color: #009900;">&#40;</span>param1, param2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>result <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>
       results.<span style="color: #006633;">setSuccessful</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       results.<span style="color: #006633;">setResponseCodeOK</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       results.<span style="color: #006633;">setResponseMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;'myResult:&quot;</span> <span style="color: #339933;">+</span> myResult<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: #009900;">&#123;</span>
       results.<span style="color: #006633;">setSuccessful</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    results.<span style="color: #006633;">sampleEnd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> results<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> Arguments getDefaultParameters<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Arguments args <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Arguments<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    args.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PARAM_1&quot;</span>, <span style="color: #0000ff;">&quot;4815162342&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    args.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PARAM_2&quot;</span>, <span style="color: #0000ff;">&quot;Iculus&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> args<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>3.) Setup JMeter</h3>
<p>JMeter&#8217;s extra libs directory is ${JMETER_INSTALL_LIB}/lib/ext.  Into that directory you will need to copy any jars that your EJB client will need.  In you&#8217;re using JBoss, you will want to copy the jbossall-client.jar into that directory as well (for the JNDI client and other remoting goodies) &#8211; presumably other application servers have similar client jar files available.</p>
<p>When you fire up JMeter, your new sampler should appear in the Samplers menu.  Enjoy!</p>
<p><em>Photo Credit: <a href="http://flickr.com/people/billjacobus1/">Bill Jacobus</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cereslogic.com/pages/2008/12/22/test-your-ejbs-with-jmeter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Eight Rules of Conference Call Etiquette</title>
		<link>http://www.cereslogic.com/pages/2008/09/29/eight-rules-of-conference-call/</link>
		<comments>http://www.cereslogic.com/pages/2008/09/29/eight-rules-of-conference-call/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 19:29:00 +0000</pubDate>
		<dc:creator>mdesjardins</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[industry]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://mikedesjardins.us/wordpress/2008/09/eight-rules-of-conference-call-etiquette/</guid>
		<description><![CDATA[At my past few jobs, I&#8217;ve had to spend a lot of time on conference calls with developers and project managers. While I don&#8217;t profess to be a &#8220;conference call expert&#8221; (or, for that matter, an etiquette expert), I think there are some general ground rules for conference calls, and I&#8217;m going to deviate a [...]]]></description>
			<content:encoded><![CDATA[<p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/conference-call-746961.jpg"><img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer;" src="http://mikedesjardins.us/blog/uploaded_images/conference-call-746944.jpg" alt="" border="0" /></a>At my past few jobs, I&#8217;ve had to spend a lot of time on conference calls with developers and project managers.  While I don&#8217;t profess to be a &#8220;conference call expert&#8221; (or, for that matter, an etiquette expert), I think there are some general ground rules for conference calls, and I&#8217;m going to deviate a bit from my usual Hibernate/JEE topics to itemize the ones that I think are important.</p>
<p>At my current job, I&#8217;m actually pretty lucky.  Almost all of us work remotely, so we&#8217;re pretty accustomed to how to behave in the ubiquitous con-call.  At a previous engagement, I wasn&#8217;t so lucky &#8211; I&#8217;ll omit their names to protect the guilty.</p>
<p>So here&#8217;s an arbitrary list from me, a random guy on the internet of questionable credentials, of what you should and shouldn&#8217;t do on a conference call.  Some of these probably seem really obvious, but I&#8217;m listing even the obvious ones because some people still break them:</p>
<p><span style="font-weight: bold;">1.) Use the Mute Button, especially on a speakerphone.</span> The most important reason for using Mute is to prevent echo-y feedback, particularly on speaker phones.  If you aren&#8217;t talking, please put your speaker phone on mute for the sake of everyone else listening.  Note that following this rule will invariably lead to the embarrassing situation where you start talking and don&#8217;t realize that you&#8217;ve left your phone on mute.  Don&#8217;t worry, we&#8217;ve all done it and we&#8217;ll all forgive you!<br /><span style="font-weight: bold;">2.) Introduce yourself when you enter the conference.</span>  Some conference systems will force you to state your name when you log in, and then the robo-operator will announce you when you join.  I actually find this to be quite jarring, especially when robo-operator interrupts what everyone is saying.  Better systems will play a quiet tone when people enter or leave the call.  Some, like one that a client had at my previous job, will do nothing at all.  This would allow people to lurk on the line unbeknownst to the participants, which is <span style="font-style: italic;">terribly</span> rude.  Likewise, if you enter a conference room while a conference call is already underway, announce yourself at the next free moment.  Ideally, the moderator of the conference will also take a moment to introduce everybody at the beginning of the call if there is a chance that everyone doesn&#8217;t know each other.  This is a <span style="font-style: italic;">great idea</span>.<br /><span style="font-weight: bold;">3.) Close the Door.</span>  It&#8217;s best to go to a closed room to join a conference call to eliminate background noise.  If you can go to such a place, do it.  Don&#8217;t join a conference call from, e.g., a street cafe or airport gate.  In doing so you risk a passing ambulance, street parade, or TSA threats to destroy unattended luggage interrupting the call.<br /><span style="font-weight: bold;">4.) Don&#8217;t use a mobile phone if you can help it.</span>  Occasionally you can get away with using a mobile for a con-call in a pinch.  But if it&#8217;s a super-important meeting where you are trying to make an impression or do something complicated, nothing will be more frustrating than a garbled, broken up, or dropped call.  Try to use a land-line if you can.<br /><span style="font-weight: bold;">5.) Don&#8217;t eat on the phone.</span>  I guess this is just a personal pet peeve of mine.  I can&#8217;t even listen to NPR if the announcer&#8217;s voice is all moist and saliva-sounding.  This is more important if you&#8217;re wearing a headset.<br /><span style="font-weight: bold;">6.) Have an agenda that everyone receives beforehand.</span>  This goes for all meetings, but it&#8217;s just as critical for a conference call. This helps you set expectations and stay on-topic during the call.<br /><span style="font-weight: bold;">7.) Get some screen-sharing software.</span>  Better yet, get some screen sharing software and test it out before the meeting.  With it, you can use something as simple as Notepad or TextMate as a virtual whiteboard.  For some reason, we never did this at my previous jobs.  We&#8217;ve done it at my current job and it works great.<br /><span style="font-weight: bold;">8.) Pick a good time for all participants. </span> This is particularly important if you&#8217;re working in different time zones.  Generally you can depend on developers being available between 10am and 4pm in their respective time zones &#8211; you can argue that programmers need to grow up and work regular business hours like everyone else.  But until the voodoo of deadline creation becomes a perfected science, software people will need to occasionally work late into the night or early in the morning to meet schedules.  Or (like anyone else) we may have daycare responsibilities and have to leave a little early.  So we stay flexible.  This means that if you&#8217;re working on, e.g., both coasts of the United States, your best bet is between 1pm and 3pm, eastern time.  If you&#8217;re working in the U.S. and the far east, you&#8217;re usually stuck with getting the U.S. participants to work early, and the others late in their workday.  Similarly between Europe and the U.S., the U.S. participants will probably need to be available late in the day, and the Europeans earlier.</p>
<p>What do you think?  Have I missed any conference call ground rules?</p>
<p><span style="font-style: italic;">Photo Credit: </span><a style="font-style: italic;" href="http://flickr.com/people/spcummings/">Stephen Cummings</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cereslogic.com/pages/2008/09/29/eight-rules-of-conference-call/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Hibernate Tools: Tips for Reverse Engineering at the Command Line</title>
		<link>http://www.cereslogic.com/pages/2008/08/05/hibernate-tools-tips-for-reverse/</link>
		<comments>http://www.cereslogic.com/pages/2008/08/05/hibernate-tools-tips-for-reverse/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 16:24:00 +0000</pubDate>
		<dc:creator>mdesjardins</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://mikedesjardins.us/wordpress/?p=32</guid>
		<description><![CDATA[One of the ancillary projects of the Hibernate framework is the Hibernate Tools toolset. Using Hibernate Tools, you can automatically generate your mapping files (or, if you prefer, JPA annotations), POJOs, and DDL from your database schema. I&#8217;ve been enamored with the &#8220;Convention over Configuration&#8221; web frameworks lately (e.g., Grails, Django), and wondered how hard [...]]]></description>
			<content:encoded><![CDATA[<p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/tools-geishaboy500-716138.jpg"><img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer;" src="http://mikedesjardins.us/blog/uploaded_images/tools-geishaboy500-716103.jpg" alt="" border="0"></a>One of the ancillary projects of the Hibernate framework is the <a href="http://www.hibernate.org/255.html">Hibernate Tools</a> toolset.  Using Hibernate Tools, you can automatically generate your mapping files (or, if you prefer, JPA annotations), POJOs, and DDL from your database schema.</p>
<p>I&#8217;ve been enamored with the &#8220;Convention over Configuration&#8221; web frameworks lately (e.g., Grails, Django), and wondered how hard it&#8217;d be to reproduce some of their magic ORM functionality using Hibernate Tools.  I&#8217;ve concluded that I&#8217;ve still got a <font style="font-weight: bold;">long</font> ways to go, but that I might have some worthwhile tips to share in the meantime.</p>
<p>My progress has been impeded a bit because the documentation for Hibernate Tools is pretty weak.  Hibernate Tools was really generated with Eclipse users in mind.  A large portion of the documentation is devoted to explaining how to use the IDE.  I&#8217;m not using Eclipse, and I intend to use the tools at the command-line, and command-line use really isn&#8217;t targeted.</p>
<p><font style="font-weight: bold;">0.) Get the tools and dependencies.</font><br />I&#8217;m still in the dark ages &#8211; I use ant instead of maven for builds, and need to track down jar dependencies the old fashioned way.  In addition to the usual Hibernate jars, you&#8217;ll also need <a href="http://www.hibernate.org/30.html">hibernate-tools</a>, <a href="http://freemarker.sourceforge.net/">freemarker</a>, and <a href="http://jtidy.sourceforge.net/download.html">jtidy</a>.</p>
<p><font style="font-weight: bold;"><br />
1.) Setup an Ant task</font><br />This is pretty straightforward, and it is explained fairly well in the documentation.  First, define yourself a Hibernate Tools task.  Mine looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;taskdef</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernatetool&quot;</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">&quot;org.hibernate.tool.ant.HibernateToolTask&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;classpath<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">refid</span>=<span style="color: #ff0000;">&quot;hibernate.libs&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">refid</span>=<span style="color: #ff0000;">&quot;hibernatetools.libs&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">refid</span>=<span style="color: #ff0000;">&quot;app.libs&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">refid</span>=<span style="color: #ff0000;">&quot;compile.libs&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pathelement</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;${build}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pathelement</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;${basedir}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/classpath<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/taskdef<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Next, create a task that uses our new definition.  We&#8217;re going to be creating the mapping files and POJOs in this example.  It will look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;gen_hibernate&quot;</span> <span style="color: #000066;">depends</span>=<span style="color: #ff0000;">&quot;compile&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;delete</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${genhbm}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${genhbm}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hibernatetool<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jdbcconfiguration</span></span>
<span style="color: #009900;">        <span style="color: #000066;">configurationfile</span>=<span style="color: #ff0000;">&quot;${src}/hibernate-connection.cfg.xml&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">revengfile</span>=<span style="color: #ff0000;">&quot;hibernate.reveng.xml&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">packagename</span>=<span style="color: #ff0000;">&quot;us.mikedesjardins.data&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">detectmanytomany</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">detectoptimisticlock</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hbm2hbmxml</span> <span style="color: #000066;">destdir</span>=<span style="color: #ff0000;">&quot;${genhbm}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hbm2java</span> <span style="color: #000066;">destdir</span>=<span style="color: #ff0000;">&quot;${genhbm}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;jdk5&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;ejb3&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hbm2java<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hibernatetool<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You&#8217;ll note that I&#8217;ve created a hibernate configuration file above which only contains connection information.  This is because I ran into problems when I tried to use a hibernate configuration with cache configuration elements in it.  You&#8217;ll also note a reference to a file called hibernate.reveng.xml.  This file controls various aspects of the reverse engineering process.  Mine is very simple &#8211; I&#8217;m working with MS SQL Server, and I don&#8217;t want it to include any of the system tables which begin with the prefix sys:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hibernate-reverse-engineering<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #808080; font-style: italic;">&lt;!-- Eliminate system tables  --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table-filter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sys.*&quot;</span> <span style="color: #000066;">exclude</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hibernate-reverse-engineering<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>That should be all that you need to do to get started.  When you run this ant target, your domain object POJOs and Mappings should end up in the ${genhbm} directory.</p>
<p><font style="font-weight: bold;">2.) First Problem &#8211; Table Names</font><br />Let&#8217;s say you&#8217;re in a company that prefixes most of it&#8217;s tables with something silly, like &#8220;tb_&#8221;.  In that case, Hibernate tools will happily create all kinds of POJOs for you named TbAccount, TbAddress, etc.  This is probably not what you want.  Fortunately, Hibernate tools let you provide your own &#8220;Reverse Engineering Strategy&#8221; class to deal with situations just like this.</p>
<p>First, update your build target in Ant to include a reference to your strategy class as an attribute of the jdbcconfiguration element:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;gen_hibernate&quot;</span> <span style="color: #000066;">depends</span>=<span style="color: #ff0000;">&quot;compile&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;delete</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${genhbm}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${genhbm}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hibernatetool<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jdbcconfiguration</span></span>
<span style="color: #009900;">        <span style="color: #000066;">configurationfile</span>=<span style="color: #ff0000;">&quot;${src}/hibernate-connection.cfg.xml&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">revengfile</span>=<span style="color: #ff0000;">&quot;hibernate.reveng.xml&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">packagename</span>=<span style="color: #ff0000;">&quot;us.mikedesjardins.data&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">detectmanytomany</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">detectoptimisticlock</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">reversestrategy</span>=<span style="color: #ff0000;">&quot;us.mikedesjardins.hibernate.CustomReverseEngineeringStrategy&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hbm2hbmxml</span> <span style="color: #000066;">destdir</span>=<span style="color: #ff0000;">&quot;${genhbm}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hbm2java</span> <span style="color: #000066;">destdir</span>=<span style="color: #ff0000;">&quot;${genhbm}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;jdk5&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;ejb3&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hbm2java<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hibernatetool<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Next, we&#8217;ll need to create the strategy class.  The documentation recommends that you extend the <font style="font-weight: bold;">DelegatingReverseEngineeringStrategy</font> class to do this, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">us.mikedesjardins.hibernate</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.cfg.reveng.ReverseEngineeringStrategy</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CustomReverseEngineeringStrategy <span style="color: #000000; font-weight: bold;">extends</span> DelegatingReverseEngineeringStrategy <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> CustomReverseEngineeringStrategy<span style="color: #009900;">&#40;</span>ReverseEngineeringStrategy delegate <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>delegate<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>Unfortunately, I was unable to find any JavaDoc documentation for the Hibernate Tools classes, so I had to do a bit of trial-and-error.  It turns out there is a method called tableToClassName that can be overridden.  This class accepts a TableIdentifier as its input parameter, and returns the class name in a String.  Thus, to remove the tb_ from the class, we could do something like this:</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;">String</span> tableToClassName<span style="color: #009900;">&#40;</span>TableIdentifier tableIdentifier<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">String</span> packageName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;us.mikedesjardins.data&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #003399;">String</span> className <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">tableToClassName</span><span style="color: #009900;">&#40;</span>tableIdentifier<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>className.<span style="color: #006633;">startsWith</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    className <span style="color: #339933;">=</span> className.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">return</span> className<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Compile your CustomReverseEngineeringStrategy class, make sure it&#8217;s on the hibernatetools task definition&#8217;s classpath, and re-run the task.  Voila!  The Tb&#8217;s are gone!</p>
<p>There&#8217;s a similar method for column names named <font style="font-weight: bold;">columnToPropertyName</font> that handles naming your persisted class&#8217;s member variables.  In my current project, the primary key columns are named the same as the table name, with &#8220;_id&#8221; appended to the end.  However, in the object model, we like to just name the primary key property &#8220;id&#8221; to simplify the creation of generic DAOs.  I use the columnToPropertyName method for this.</p>
<p><font style="font-weight: bold;">2.) Next Problem &#8211; The Generated POJOs need tweaking.</font><br />Perhaps the code that Hibernate generates is not to your liking.  Maybe you work with standards-compliance-nazis who want a copyright header at the top of each class file.  Or maybe all of your persisted objects implement the same interface for use with generic DAOs.</p>
<p>Under the covers, Hibernate uses <a href="http://freemarker.sourceforge.net/">freemarker</a> to generate POJOs and mapping files.  The templates that it uses can be edited to your liking, but doing it is not very straightforward.</p>
<p>First, unzip the hibernate tools jar into a temporary directory.  Once unzipped, you&#8217;ll find a directory named pojo.  This directory contains all of the templates used for POJO generation.  Copy that directory into your build area and name it something like hib_templates.</p>
<p>The hbm2java task is actually just an alias for another hibernate tools target called hbmtemplate.  With hbmtemplate, you can configure the location of the source templates.  So we&#8217;ll remove the hbm2java task from the gen_hibernate target, and replace it with an equivalent hbmtemplate task:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;gen_hibernate&quot;</span> <span style="color: #000066;">depends</span>=<span style="color: #ff0000;">&quot;compile&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;delete</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${genhbm}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${genhbm}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hibernatetool<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jdbcconfiguration</span></span>
<span style="color: #009900;">        <span style="color: #000066;">configurationfile</span>=<span style="color: #ff0000;">&quot;${src}/hibernate-connection.cfg.xml&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">revengfile</span>=<span style="color: #ff0000;">&quot;hibernate.reveng.xml&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">packagename</span>=<span style="color: #ff0000;">&quot;us.mikedesjardins.data&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">detectmanytomany</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">detectoptimisticlock</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">reversestrategy</span>=<span style="color: #ff0000;">&quot;us.mikedesjardins.hibernate.CustomReverseEngineeringStrategy&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hbm2hbmxml</span> <span style="color: #000066;">destdir</span>=<span style="color: #ff0000;">&quot;${genhbm}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hbmtemplate</span> <span style="color: #000066;">templateprefix</span>=<span style="color: #ff0000;">&quot;pojo/&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">destdir</span>=<span style="color: #ff0000;">&quot;${genhbm}&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">template</span>=<span style="color: #ff0000;">&quot;hib_templates/Pojo.ftl&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">filepattern</span>=<span style="color: #ff0000;">&quot;{package-name}/{class-name}.java&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;jdk5&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;ejb3&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hbmtemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hibernatetool<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now that we&#8217;ve told hibernate tools where our template lives, we can edit it to our liking.  For example, if we want to add a copyright notice to the top of each generated POJO, we could do so thusly:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">// Copyright 2008 Big Amalgamated Mega Global Software Corp.  All Rights Reserved.</span>
<span style="color: #666666; font-style: italic;">//</span>
$<span style="color: #009900;">&#123;</span>pojo.<span style="color: #006633;">getPackageDeclaration</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// Generated ${date} by Hibernate Tools ${version}</span>
&nbsp;
<span style="color: #339933;">&lt;</span>#assign classbody<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>#include <span style="color: #0000ff;">&quot;PojoTypeDeclaration.ftl&quot;</span><span style="color: #339933;">/&gt;;</span> <span style="color: #009900;">&#123;</span>
.
.
.
<span style="color: #009900;">&#40;</span>etc<span style="color: #009900;">&#41;</span></pre></div></div>

<p>Now that we&#8217;ve done this, we can create more templates that, e.g., generate empty DAO classes, automatically create derived classes, etc.</p>
<p>Hope that helps!</p>
<p><font style="font-style: italic;">Photo Credit: </font><a style="font-style: italic;" href="http://flickr.com/people/geishaboy500/">geishaboy500</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cereslogic.com/pages/2008/08/05/hibernate-tools-tips-for-reverse/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Twenty minutes with Hibernate Search; A Cheesy Example</title>
		<link>http://www.cereslogic.com/pages/2008/07/12/twenty-minutes-with-hibernate-search-a-cheesy-example/</link>
		<comments>http://www.cereslogic.com/pages/2008/07/12/twenty-minutes-with-hibernate-search-a-cheesy-example/#comments</comments>
		<pubDate>Sat, 12 Jul 2008 13:48:00 +0000</pubDate>
		<dc:creator>mdesjardins</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://mikedesjardins.us/wordpress/?p=31</guid>
		<description><![CDATA[In a recent post, I showed a trick for determining which users in a system are running a long-running query. One commenter suggested that using a full-text search system made a lot of sense in those situations, and I wholeheartedly agree. So I decided to devote a new post to Hibernate Search! In this example, [...]]]></description>
			<content:encoded><![CDATA[<p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/cheese-blog-719088.jpg"><img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer;" src="http://mikedesjardins.us/blog/uploaded_images/cheese-blog-719057.jpg" alt="" border="0" /></a>In a <a href="http://mikedesjardins.us/blog/2008/06/who-is-running-that-horrible-hibernate.html">recent post</a>, I showed a trick for determining which users in a system are running a long-running query.  One commenter suggested that using a full-text search system made a lot of sense in those situations, and I wholeheartedly agree. So I decided to devote a new post to <a href="http://www.hibernate.org/410.html">Hibernate Search</a>!</p>
<p>In this example, I provide a live, <a href="http://mikedesjardins.us/cheese/search">online interactive search</a> of an online cheese database, and you can download the example (more on that at the end of the post).</p>
<p><span style="font-weight: bold;">Step One &#8211; The Test Data</span><br />I spent the most time on this project was creating test data for the example program.  I headed over to <a href="http://freebase.com/">Freebase</a> to see what was available for data sets.  Among lots of other things, they have a free database of <a href="http://www.freebase.com/view/food/cheese">cheese</a>.  First, I downloaded the data in TSV format, dumped it into a raw table, and massaged it into a relational, normalized schema.  I ended up with this when I was done:<br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/cheese-search-erd-785039.jpg"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://mikedesjardins.us/blog/uploaded_images/cheese-search-erd-785035.jpg" alt="" border="0" /></a>So, a CHEESE can have only one ORIGIN (a country or region where the cheese is made), is made from one-to-many types of MILK, and may have zero-to-many TEXTURES associated with it.</p>
<p><span style="font-weight: bold;">Step Two &#8211; Build the Domain Model</span><br />The domain model for this system is very simple.  Each Cheese class has an Origin, and a set of Milk and Textures:</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;MILK&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> Milk <span style="color: #009900;">&#123;</span>
  @Id @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;milk_id&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
&nbsp;
  @Basic @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
  @Version @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> version<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Accessors omitted</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p></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;ORIGIN&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> Origin <span style="color: #009900;">&#123;</span>
  @Id @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;origin_id&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
&nbsp;
  @Basic @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
  @Version @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> version<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Accessors omitted</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p></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;TEXTURE&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> Texture <span style="color: #009900;">&#123;</span>
  @Id @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;texture_id&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
&nbsp;
  @Basic @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> description<span style="color: #339933;">;</span>
&nbsp;
  @Version @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> version<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Accessors omitted</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p></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;CHEESE&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> Cheese <span style="color: #009900;">&#123;</span>
  @Id @GeneratedValue<span style="color: #009900;">&#40;</span>strategy<span style="color: #339933;">=</span>GenerationType.<span style="color: #006633;">IDENTITY</span><span style="color: #009900;">&#41;</span>
  @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;cheese_id&quot;</span>,nullable<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">false</span>,unique<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
&nbsp;
  @Basic @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
  @ManyToOne<span style="color: #009900;">&#40;</span>cascade<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>CascadeType.<span style="color: #006633;">ALL</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  @JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;origin_id&quot;</span>,nullable<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> Origin origin<span style="color: #339933;">;</span>
&nbsp;
  @ManyToMany<span style="color: #009900;">&#40;</span>cascade<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>CascadeType.<span style="color: #006633;">PERSIST</span>,CascadeType.<span style="color: #006633;">MERGE</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  @JoinTable<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;CHEESE_MILK_MAP&quot;</span>,
             joinColumns<span style="color: #339933;">=</span>@JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;cheese_id&quot;</span><span style="color: #009900;">&#41;</span>,
             inverseJoinColumns<span style="color: #339933;">=</span>@JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;milk_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> Set<span style="color: #339933;">&lt;</span>milk<span style="color: #339933;">&gt;</span> milks <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>milk<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  @ManyToMany<span style="color: #009900;">&#40;</span>cascade<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>CascadeType.<span style="color: #006633;">PERSIST</span>,CascadeType.<span style="color: #006633;">MERGE</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  @JoinTable<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;CHEESE_TEXTURE_MAP&quot;</span>,
             joinColumns<span style="color: #339933;">=</span>@JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;cheese_id&quot;</span><span style="color: #009900;">&#41;</span>,
             inverseJoinColumns<span style="color: #339933;">=</span>@JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;texture_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> Set<span style="color: #339933;">&lt;</span>texture<span style="color: #339933;">&gt;</span> textures <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>texture<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  @Version @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> version<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Accessors omitted</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span style="font-weight: bold;">Step Three &#8211; Add Search Annotations and Configure Lucene</span><br />Behind the scenes, Hibernate Search uses the <a href="http://lucene.apache.org/">Apache Lucene</a> search engine to do its indexing. In short, it maintains a mapping of object IDs to search terms in an external file, and updates the file when objects are added, updated, or deleted.  To start using Hibernate Search, you&#8217;ll need to configure the location of these index files, as well as a search directory provider (we&#8217;ll just use the default).  This is done in your Hibernate properties file, or (if you use JPA, like me), in persistence.xml:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.search.default.directory_provider&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;org.hibernate.search.store.FSDirectoryProvider&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.search.default.indexBase&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/var/lucene/cheese-indexes&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Next, you need to indicate to Lucene which classes need to be indexed.  You also need to indicate which data fields 1.) contain the document ID, and 2.) contain relevant search text.  In our example, we only need to index the Cheese objects.  We want to allow users to search on cheese name, milk name, origin, and texture.</p>
<p>First, we indicate that we want to index the Cheese objects by applying the <span style="font-weight: bold;">@Indexed</span> annotation to the class, and we elect to use the Id field to identify the Cheese objects to Lucene by applying a <span style="font-weight: bold;">@DocumentId</span> annotation to it.  Next, we indicate that the cheese name contains searchable text by adding the <span style="font-weight: bold;">@Field(index=Index.TOKENIZED, store=Store.NO)</span> annotation to it.  The annotation parameters are informing Hibernate Search to use Lucene&#8217;s default tokenizer to summarize the text, and not to store a copy of the document content.</p>
<p>We also want to allow users to search on Origin, Milk Name, and Texture.  This text is not contained within the Cheese object, instead they&#8217;re in related object.  So we need to add the <span style="font-weight: bold;">@IndexEmbedded</span> annotation to the member variables in the Cheese class that refer to the objects which contain the searchable text, and we also need to add the <span style="font-weight: bold;">@Field(index=Index.TOKENIZED, store=Store.NO)</span> annotation to the Milk, Origin, and Texture classes to indicate which fields are searchable.</p>
<p>When you&#8217;re done, the modified domain classes will look like this:</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;MILK&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> Milk <span style="color: #009900;">&#123;</span>
  @Id @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;milk_id&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
&nbsp;
  @<span style="color: #003399;">Field</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">=</span>Index.<span style="color: #006633;">TOKENIZED</span><span style="color: #009900;">&#41;</span>
  @Basic @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
  @Version @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> version<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Accessors omitted</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p></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;ORIGIN&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> Origin <span style="color: #009900;">&#123;</span>
  @Id @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;origin_id&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
&nbsp;
  @<span style="color: #003399;">Field</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">=</span>Index.<span style="color: #006633;">TOKENIZED</span><span style="color: #009900;">&#41;</span>
  @Basic @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
  @Version @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> version<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Accessors omitted</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p></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;TEXTURE&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> Texture <span style="color: #009900;">&#123;</span>
  @Id @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;texture_id&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
&nbsp;
  @<span style="color: #003399;">Field</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">=</span>Index.<span style="color: #006633;">TOKENIZED</span><span style="color: #009900;">&#41;</span>
  @Basic @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> description<span style="color: #339933;">;</span>
&nbsp;
  @Version @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> version<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Accessors omitted</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Indexed
@<span style="color: #003399;">Entity</span> @Table<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;CHEESE&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> Cheese <span style="color: #009900;">&#123;</span>
  @DocumentId
  @Id @GeneratedValue<span style="color: #009900;">&#40;</span>strategy<span style="color: #339933;">=</span>GenerationType.<span style="color: #006633;">IDENTITY</span><span style="color: #009900;">&#41;</span>
  @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;cheese_id&quot;</span>,nullable<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">false</span>,unique<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
&nbsp;
  @Basic @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span>
  @<span style="color: #003399;">Field</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">=</span>Index.<span style="color: #006633;">TOKENIZED</span>, store<span style="color: #339933;">=</span>Store.<span style="color: #006633;">NO</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
  @IndexedEmbedded
  @ManyToOne<span style="color: #009900;">&#40;</span>cascade<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>CascadeType.<span style="color: #006633;">ALL</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  @JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;origin_id&quot;</span>,nullable<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> Origin origin<span style="color: #339933;">;</span>
&nbsp;
  @IndexedEmbedded
  @ManyToMany<span style="color: #009900;">&#40;</span>cascade<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>CascadeType.<span style="color: #006633;">PERSIST</span>,CascadeType.<span style="color: #006633;">MERGE</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  @JoinTable<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;CHEESE_MILK_MAP&quot;</span>,
             joinColumns<span style="color: #339933;">=</span>@JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;cheese_id&quot;</span><span style="color: #009900;">&#41;</span>,
             inverseJoinColumns<span style="color: #339933;">=</span>@JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;milk_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> Set<span style="color: #339933;">&lt;</span>milk<span style="color: #339933;">&gt;</span> milks <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>milk<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  @IndexedEmbedded
  @ManyToMany<span style="color: #009900;">&#40;</span>cascade<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>CascadeType.<span style="color: #006633;">PERSIST</span>,CascadeType.<span style="color: #006633;">MERGE</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  @JoinTable<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;CHEESE_TEXTURE_MAP&quot;</span>,
             joinColumns<span style="color: #339933;">=</span>@JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;cheese_id&quot;</span><span style="color: #009900;">&#41;</span>,
             inverseJoinColumns<span style="color: #339933;">=</span>@JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;texture_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> Set<span style="color: #339933;">&lt;</span>texture<span style="color: #339933;">&gt;</span> textures <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>texture<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  @Version @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> version<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Accessors omitted</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span style="font-weight: bold;">Step Four &#8211; The Servlets</span><br />In this example, I didn&#8217;t want to rely on any web frameworks or even on JSPs, so I wrote a good old-fashioned servlet to exercise the search function.  No sane person would ever do it this way.   There are actually two servlets in the example &#8211; one for application initialization and one for the page itself.</p>
<p>The initialization servlet does the work of indexing all of the database data the first time through.  For our small data set, this takes less than a minute.  For larger data sets, it wouldn&#8217;t make sense to re-index everything every time you start the application.  The initialization code iterates over all of the Cheese objects ant tells the FullTextEntityManger to index it:</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: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  Dao<span style="color: #339933;">&lt;</span>cheese<span style="color: #339933;">&gt;</span> dao <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CheeseDao<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  EntityManager em <span style="color: #339933;">=</span> dao.<span style="color: #006633;">getEntityManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  FullTextEntityManager fullTextEntityManager <span style="color: #339933;">=</span> Search.<span style="color: #006633;">createFullTextEntityManager</span><span style="color: #009900;">&#40;</span>em<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  List<span style="color: #339933;">&lt;</span>cheese<span style="color: #339933;">&gt;</span> cheeses <span style="color: #339933;">=</span> em.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select c from Cheese as c&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Cheese cheese <span style="color: #339933;">:</span> cheeses<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    fullTextEntityManager.<span style="color: #006633;">index</span><span style="color: #009900;">&#40;</span>cheese<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>The main page servlet has some code in the doPost method to perform the search based on the contents of the text form field.  That code looks like this (I&#8217;ve shortened it up a bit here by removing some error checking and HTML output):</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: #000066; font-weight: bold;">void</span> doPost<span style="color: #009900;">&#40;</span>HttpServletRequest request,
                   HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
  response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003399;">PrintWriter</span> out <span style="color: #339933;">=</span> response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  emitHeader<span style="color: #009900;">&#40;</span>out<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003399;">String</span> searchTerm <span style="color: #339933;">=</span> request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;searchterm&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  EntityManager em <span style="color: #339933;">=</span> dao.<span style="color: #006633;">getEntityManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  FullTextEntityManager fullTextEntityManager <span style="color: #339933;">=</span>
    org.<span style="color: #006633;">hibernate</span>.<span style="color: #006633;">search</span>.<span style="color: #006633;">jpa</span>.<span style="color: #006633;">Search</span>.<span style="color: #006633;">createFullTextEntityManager</span><span style="color: #009900;">&#40;</span>em<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  MultiFieldQueryParser parser <span style="color: #339933;">=</span>
    <span style="color: #000000; font-weight: bold;">new</span> MultiFieldQueryParser<span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;name&quot;</span>,
                                            <span style="color: #0000ff;">&quot;origin.name&quot;</span>,
                                            <span style="color: #0000ff;">&quot;milks.name&quot;</span>,
                                            <span style="color: #0000ff;">&quot;textures.description&quot;</span><span style="color: #009900;">&#125;</span>,
                               <span style="color: #000000; font-weight: bold;">new</span> StandardAnalyzer<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;">try</span> <span style="color: #009900;">&#123;</span>
    org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">lucene</span>.<span style="color: #006633;">search</span>.<span style="color: #006633;">Query</span> query <span style="color: #339933;">=</span> parser.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    javax.<span style="color: #006633;">persistence</span>.<span style="color: #006633;">Query</span> hibQuery <span style="color: #339933;">=</span>
      fullTextEntityManager.<span style="color: #006633;">createFullTextQuery</span><span style="color: #009900;">&#40;</span>query,Cheese.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    List<span style="color: #339933;">&lt;</span>cheese<span style="color: #339933;">&gt;</span> result <span style="color: #339933;">=</span> hibQuery.<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    emitTable<span style="color: #009900;">&#40;</span>out,result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">ParseException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Got a parse exception&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> ServletException<span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  emitFooter<span style="color: #009900;">&#40;</span>out<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  out.<span style="color: #006633;">close</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>That&#8217;s all there is to it!  As you can see, setting up Hibernate Search is very simple.  Most of the effort for this project was spent creating the data and making the servlets work.</p>
<p><span style="font-weight: bold;">Enjoy the Finished Product</span><br />To see this less-than-world-changing application in action, visit it <a href="http://mikedesjardins.us/cheese/search">here</a>.  You can also <a href="http://mikedesjardins.us/cheese-search-1.0.tar.gz">download the whole eclipse project</a> and try it out for yourself.  It comes with SQL dumps suitable for MySQL and PostgreSQL, and it has been tested with both environments under Tomcat 5.5.</p>
<p><span style="font-style: italic;">Photo Credit: </span><a href="http://www.flickr.com/people/cuse/"><span style="font-style: italic;font-size:100%;" ><span class="RealName"><span class="fn n"><span class="given-name">Chris</span> <span class="family-name">Buecheler</span></span></span></span></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cereslogic.com/pages/2008/07/12/twenty-minutes-with-hibernate-search-a-cheesy-example/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Use Hibernate&#8217;s Custom Loaders to fake an aggregation view</title>
		<link>http://www.cereslogic.com/pages/2008/06/23/use-hibernates-custom-loaders-to-fake/</link>
		<comments>http://www.cereslogic.com/pages/2008/06/23/use-hibernates-custom-loaders-to-fake/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 17:37:00 +0000</pubDate>
		<dc:creator>mdesjardins</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://mikedesjardins.us/wordpress/2008/06/use-hibernates-custom-loaders-to-fake-an-aggregation-view/</guid>
		<description><![CDATA[Your ProblemYou have a data model with table that contains data you want to aggregate. For instance (returning to my venerable Pizza Shop example), let&#8217;s say you have a PRODUCT table that enumerates the items your pizza shops sells, and a LOCATION table that contains all of your retail locations: We also have a table [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-weight: bold;">Your Problem</span><br />You have a data model with table that contains data you want to aggregate.  For instance (returning to my venerable Pizza Shop example), let&#8217;s say you have a PRODUCT table that enumerates the items your pizza shops sells, and a LOCATION table that contains all of your retail locations:</p>
<p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/blog-location-product-791817.png"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://mikedesjardins.us/blog/uploaded_images/blog-location-product-791813.png" alt="" border="0" /></a><br />We also have a table that contains the sum of all of the previous days&#8217; sales, broken down by PRODUCT and LOCATION:</p>
<p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/blog-yesterday-sales-786534.png"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://mikedesjardins.us/blog/uploaded_images/blog-yesterday-sales-786525.png" alt="" border="0" /></a>This is all well and good, but we&#8217;ve been asked to create an Executive Dashboard for the President of the pizza chain, and she would like to see daily sales by product.  She is not interested in a breakdown by location.</p>
<p><span style="font-weight: bold;">We could tally it up client side&#8230;</span><br />What if we just loaded the entire table into the client, and iterated over the per-product results, and present that?  Unfortunately, ORM libraries are pretty stupid in situations like these, and will generate all kinds of expensive reads to the database when you try to solve the problem this way.  If you want to learn more about lazy loading, and why you shouldn&#8217;t iterate over a collection, check out my earlier post <a href="http://mikedesjardins.us/blog/2008/03/pizza-shop-2-totaling-jpa-order-use.html">here</a>.</p>
<p><span style="font-weight: bold;">We could just create a view in the Database&#8230;</span><br />We <span>could</span><span style="font-weight: bold;"> </span>just create a view, and aggregate the data there.  Then we can easily create a Hibernate mapping to that view.  The query for the view is simple:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">VIEW</span> sales_by_product_view <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> product_id<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>total_sales<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> total_sales
  <span style="color: #993333; font-weight: bold;">FROM</span> YESTERDAY_SALES
 <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> product_id</pre></div></div>

<p>However, we are working with a tyrannical DBA.  She is not keen on proliferating views throughout our otherwise pristine schema every time the President has decided that the company needs a new widget for the executive dashboard application.</p>
<p><span style="font-weight: bold;">&#8230;or we could fake it with a custom loader</span><br />Instead, let&#8217;s create a Hibernate mapping that generates the same results as the view.  First, let&#8217;s create a simple POJO to contain the results:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">us.mikedesjardins</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SalesByProduct <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> productId<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">BigDecimal</span> sales<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// accessors omitted</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The corresponding mapping file would look like this.  I re-used the product_id for the ID in this  example.  Note the loader element:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hibernate-mapping</span> <span style="color: #000066;">package</span>=<span style="color: #ff0000;">&quot;us.mikedesjardins&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;SalesByProduct&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">dynamic-insert</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">dynamic-update</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">mutable</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;int&quot;</span> <span style="color: #000066;">unsaved-value</span>=<span style="color: #ff0000;">&quot;null&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;__id&quot;</span> <span style="color: #000066;">sql-type</span>=<span style="color: #ff0000;">&quot;int identity&quot;</span> <span style="color: #000066;">not-null</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">unique</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;productId&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;int&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;__product_id&quot;</span> <span style="color: #000066;">not-null</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sales&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.math.BigDecimal&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;__total_sales&quot;</span> <span style="color: #000066;">not-null</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;loader</span> <span style="color: #000066;">query-ref</span>=<span style="color: #ff0000;">&quot;salesByProductQuery&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sql-query</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;salesByProductQuery&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;return</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;SalesByProduct&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">    select product_id as __id</span>
<span style="color: #339933;">         , product_id as __product_id</span>
<span style="color: #339933;">         , sum(total_sales) as __total_sales</span>
<span style="color: #339933;">      from YESTERDAY_SALES</span>
<span style="color: #339933;">     where product_id = :product_id</span>
<span style="color: #339933;">    group by product_id</span>
<span style="color: #339933;">   ]]&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sql-query<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hibernate-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Note that you need to put a positional argument in the query, or Hibernate will get nasty about parsing it.  Hope that helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cereslogic.com/pages/2008/06/23/use-hibernates-custom-loaders-to-fake/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Why you should not not use ORM</title>
		<link>http://www.cereslogic.com/pages/2008/06/16/why-you-should-not-not-use-orm/</link>
		<comments>http://www.cereslogic.com/pages/2008/06/16/why-you-should-not-not-use-orm/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 23:32:00 +0000</pubDate>
		<dc:creator>mdesjardins</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://mikedesjardins.us/wordpress/2008/06/why-you-should-not-not-use-orm/</guid>
		<description><![CDATA[Yesterday I read a blog post by Kenneth Downs entitled &#8220;Why I Do Not Use ORM&#8221; on his The Database Programmer blog. It wasn&#8217;t the first blog post with gripes about Object Relational Mapping, and it certainly won&#8217;t be the last. For me, this particular article highlighted a few misconceptions about why and how ORM [...]]]></description>
			<content:encoded><![CDATA[<p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/2522766037_44d8e2e709-731771.jpg"><img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer;" src="http://mikedesjardins.us/blog/uploaded_images/2522766037_44d8e2e709-731749.jpg" alt="" border="0" /></a>Yesterday I read a blog post by Kenneth Downs entitled &#8220;<a href="http://database-programmer.blogspot.com/2008/06/why-i-do-not-use-orm.html">Why I Do Not Use ORM</a>&#8221; on his <a href="http://database-programmer.blogspot.com/">The Database Programmer</a> blog.  It wasn&#8217;t the first blog post with gripes about Object Relational Mapping, and it certainly won&#8217;t be the last.  For me, this particular article highlighted a few misconceptions about why and how ORM should be used, and I thought I might chime in with my own perspective.</p>
<p><span style="font-weight: bold;">ORM is not a way to avoid SQL</span><br />The first thing that stood out for me was this quote:<br />
<blockquote>&#8220;The language SQL is the most widely supported, implemented, and    used way to connect to databases.  But since most of us have long    lists of complaints about the language, we end up writing abstraction    layers that make it easier for us to avoid coding SQL directly.&#8221;</p></blockquote>
<p>To his credit, the author wasn&#8217;t actually addressing ORM in this paragraph.  However, anyone writing business logic which interacts with a database, who is unable to write some basic SQL, is like a bull in a china shop; nothing good will come of it regardless of the tools and abstraction layers employed.</p>
<p><span style="font-weight: bold;">The Simple Example</span><br />The next example in the post shows how one would write a row to a database in only four lines of PHP code &#8211; it looks something like this (I removed the comments):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> getPostStartingWith<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;inp_&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$table_id</span> <span style="color: #339933;">=</span> myGetPostVarFunction<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'table_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>SQLX_Insert<span style="color: #009900;">&#40;</span><span style="color: #000088;">$table_id</span><span style="color: #339933;">,</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 myFrameworkErrorReporting<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>I don&#8217;t know PHP, but I think the code is reading every field in a posted form that starts with inp_, generates an insert statement straight from the ID&#8217;s on the form fields, and writes an insert statement with the results.</p>
<p>Perhaps it&#8217;s unfair to criticize this code because &#8220;it&#8217;s just a simple example,&#8221; but if this code is being held up as an example of how short and simple non-ORM code can be, one does have to wonder
<ul>
<li>When the database schema changes, does the HTML need to be updated so that the form fields match the database schema?</li>
<li>Where are the transactions?  What if I need to insert into several tables and roll back the transaction if one fails?</li>
<li>Does the handy SQLX_insert method prevent SQL injection attacks?</li>
</ul>
<p>He goes on to say that this task is made even easier by <a href="http://database-programmer.blogspot.com/2008/06/using-data-dictionary.html">using a data dictionary</a> to generate the SQL.  After reading the &#8220;Using a Data Dictionary&#8221; article, one has to wonder whether or not the author realizes that it is a very crude form of ORM.</p>
<p><span style="font-weight: bold;">What about Business Logic?</span><br />Kenneth Downs tries to head-off any arguments about business logic before they come up, knowing that ORM evangelists will argue that the domain objects can encapsulate essential business logic for the application.  His response?<br />
<blockquote>&#8220;The SQLX_Insert()    routine can call out to functions (fast) or objects     (much slower) that massage data before and after the    insert operation.  I will be demonstrating some of these    techniques in future essays, but of course the best     permforming and safest method is to use triggers.&#8221;</p></blockquote>
<p>For me, this sounds alarm bells.  Triggers slow down transactions.  Triggers are in your database, which is often your system&#8217;s biggest bottleneck.  Triggers silently do things behind your back without telling you.  Triggers change databases from vast, efficient places to store relational data, into a lumbering behemoth interpreting procedural code inside big iron.</p>
<p>Conversely, business logic that can be easily distributed across many smaller web servers scales horizontally.  The domain layer is a fantastic place to embed simple data massaging &#8211; sadly, I often see a pile of persistent entities with getters and setters that don&#8217;t do anything.</p>
<p><span style="font-weight: bold;">Counter Example: The Disaster Scenario</span><br />Lastly, Ken (can I call him that?  What is the ettiquite for this sort of thing, anyway?) shows an example of a piece of code that is likely to cause hundreds of unneeded reads to the database in an untuned ORM-based system.  I don&#8217;t dispute this; in fact, I <a href="http://mikedesjardins.us/blog/2008/03/pizza-shop-2-totaling-jpa-order-use.html">posted</a> about an almost identical nightmare scenario myself a while ago.</p>
<p>For this, I go back to my &#8220;bull in a china shop&#8221; analogy.  Programmers can write horrible code in any language, with any tool.  Layers of abstraction are a double-edged sword, because you need to understand what they are doing for you.  But it&#8217;s not the tool&#8217;s fault; it&#8217;s the person misusing it.</p>
<p><span style="font-weight: bold;">Computer Science&#8217;s Vietnam</span><br />In 2004, Ted Neward famously called ORM <a href="http://blogs.tedneward.com/2006/06/26/The+Vietnam+Of+Computer+Science.aspx">Computer Science&#8217;s Vietnam</a>.  Encouraged by early successes, we got sucked into the quagmire.  There are plenty of reasons to be frustrated with ORM, but I&#8217;m not sure I agree with Ken&#8217;s.  I try to hit the 80/20 rule with ORM, and use it where it makes sense.  When I get into a convoluted transaction or need to do a large batch of operations, I&#8217;m not afraid to dive into SQL and do the work in a stored procedure.  I think it&#8217;s a good mix.  How about you?</p>
<p><span style="font-style: italic;">Photo Credit: </span><a style="font-style: italic;" href="http://www.flickr.com/people/meesterdickey/">Ryan Dickey</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cereslogic.com/pages/2008/06/16/why-you-should-not-not-use-orm/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>The Hibernate Song</title>
		<link>http://www.cereslogic.com/pages/2008/06/06/the-hibernate-song/</link>
		<comments>http://www.cereslogic.com/pages/2008/06/06/the-hibernate-song/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 14:32:00 +0000</pubDate>
		<dc:creator>mdesjardins</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://mikedesjardins.us/wordpress/2008/06/the-hibernate-song/</guid>
		<description><![CDATA[(to the tune of The Spiderman Theme Song from the original cartoon series) Hibernate, HibernateO/R Mapping sure is greatGavin King, and H-Q-LMake my life a living hellLook out! Here comes Hibernate! Mapping Files in HibernateWish my team would annotateAre its queries optimized?Who knows &#8211; it&#8217;s always a surpriseHey there, there goes Hibernate! Don&#8217;t fetch data [...]]]></description>
			<content:encoded><![CDATA[<p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/spiderman-704658.png"><img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer;" src="http://mikedesjardins.us/blog/uploaded_images/spiderman-704633.png" alt="" border="0" /></a>(to the tune of <span style="font-style: italic;">The Spiderman Theme Song</span> from the original cartoon series)</p>
<p>Hibernate, Hibernate<br />O/R Mapping sure is great<br />Gavin King, and H-Q-L<br />Make my life a living hell<br />Look out!  Here comes Hibernate!</p>
<p>Mapping Files in Hibernate<br />Wish my team would annotate<br />Are its queries optimized?<br />Who knows &#8211; it&#8217;s always a surprise<br />Hey there, there goes Hibernate!</p>
<p>Don&#8217;t fetch data now<br />Lazy load, &#8216;cuz you might guess<br />I don&#8217;t need it now<br />Hibernate, you do know best!</p>
<p>Hibernate, Hibernate<br />Friendly neighborhood Hibernate<br />Grab the jars, and climb aboard<br />OO models are your reward<br />To it&#8230;<br />Relational DBs are old-school<br />When you need a complex tool<br />You need the Hibernate!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cereslogic.com/pages/2008/06/06/the-hibernate-song/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Enumerated Types with JPA, and your Sock Drawer</title>
		<link>http://www.cereslogic.com/pages/2008/02/18/enumerated-types-with-jpa-and-your-sock/</link>
		<comments>http://www.cereslogic.com/pages/2008/02/18/enumerated-types-with-jpa-and-your-sock/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 23:39:00 +0000</pubDate>
		<dc:creator>mdesjardins</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://mikedesjardins.us/wordpress/?p=26</guid>
		<description><![CDATA[One feature of JPA that didn&#8217;t exist in plain-vanilla Hibernate is support for Enumerated types. I haven&#8217;t seen a lot of examples of this in practice or on the internet, so in this post I&#8217;ll show one example of how to use JDK 5 enumerations with JPA. For our example, we are going to create [...]]]></description>
			<content:encoded><![CDATA[<p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/sockdrawer-773600.jpg"><img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 226px; height: 341px;" src="http://mikedesjardins.us/blog/uploaded_images/sockdrawer-773596.jpg" alt="" border="0" /></a>One feature of JPA that didn&#8217;t exist in plain-vanilla Hibernate is support for Enumerated types.  I haven&#8217;t seen a lot of examples of this in practice or on the internet, so in this post I&#8217;ll show one example of how to use JDK 5 enumerations with JPA.</p>
<p>For our example, we are going to create an inventory system for our sock drawer.  It is comprised of only two tables.  The first table, called SOCK, contains one row per sock in our drawer.  The columns of the table are:</p>
<ul>
<li>sock_id &#8211; an auto-incrementing identity column.</li>
<li>sock_description &#8211; a varchar column for a free-form text description of the sock.</li>
<li>sock_pattern_id &#8211; a reference to a row in the SOCK_PATTERN table.</li>
</ul>
<p>As you may have guessed, the SOCK_PATTERN table looks like this:
<ul>
<li>sock_pattern_id &#8211; an integer primary key.  It&#8217;s not auto-incrementing, because we will want to have control over the contents of the field.</li>
<li>sock_pattern_description &#8211; a varchar column for a free-form text description of the pattern.</li>
</ul>
<p>We need to &#8220;prime&#8221; our SOCK_PATTERN table with the valid patterns and create a foreign key relationship between the two tables:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> SOCK_PATTERN <span style="color: #66cc66;">&#40;</span>sock_pattern_id<span style="color: #66cc66;">,</span>sock_pattern_description<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'SOLID'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> SOCK_PATTERN <span style="color: #66cc66;">&#40;</span>sock_pattern_id<span style="color: #66cc66;">,</span>sock_pattern_description<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'STRIPES'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> SOCK_PATTERN <span style="color: #66cc66;">&#40;</span>sock_pattern_id<span style="color: #66cc66;">,</span>sock_pattern_description<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'POLKA_DOT'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> SOCK_PATTERN <span style="color: #66cc66;">&#40;</span>sock_pattern_id<span style="color: #66cc66;">,</span>sock_pattern_description<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'ARGYLE'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Note that we populated sock_pattern_id starting with zero; this is important because the Enumeration below is zero-indexed.</p>
<p>Next, let&#8217;s create the classes:</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: #000000; font-weight: bold;">enum</span> SockPattern <span style="color: #009900;">&#123;</span>
 SOLID, STRIPES, POLKA_DOT, ARGYLE
<span style="color: #009900;">&#125;</span>
&nbsp;
@<span style="color: #003399;">Entity</span> @Table<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SOCK&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> Sock <span style="color: #009900;">&#123;</span>
 @Id @GeneratedValue<span style="color: #009900;">&#40;</span>strategy<span style="color: #339933;">=</span>GenerationType.<span style="color: #006633;">IDENTITY</span><span style="color: #009900;">&#41;</span>
 @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sock_id&quot;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
&nbsp;
 @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sock_description&quot;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> description<span style="color: #339933;">;</span>
&nbsp;
 @Enumerated @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sock_pattern_id&quot;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #000000; font-weight: bold;">private</span> SockPattern pattern<span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> getId<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> id<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> id<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;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getDescription<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> description<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDescription<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> description<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;">description</span> <span style="color: #339933;">=</span> description<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> SockPattern getPattern<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> pattern<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setPattern<span style="color: #009900;">&#40;</span>SockPattern pattern<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;">pattern</span> <span style="color: #339933;">=</span> pattern<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, to use our wonderful contraption, you&#8217;d do something like the following:</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: #000000; font-weight: bold;">class</span> Demo <span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> EntityManagerFactory emf<span style="color: #339933;">;</span>
 <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span>
     Demo.<span style="color: #006633;">emf</span> <span style="color: #339933;">=</span> Persistence.<span style="color: #006633;">createEntityManagerFactory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sockdrawer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 @Test
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> socksOne<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     Sock sock <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Sock<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     sock.<span style="color: #006633;">setDescription</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;My favorite sock.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     sock.<span style="color: #006633;">setPattern</span><span style="color: #009900;">&#40;</span>SockPattern.<span style="color: #006633;">ARGYLE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     EntityManager em <span style="color: #339933;">=</span> Demo.<span style="color: #006633;">emf</span>.<span style="color: #006633;">createEntityManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     em.<span style="color: #006633;">getTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     em.<span style="color: #006633;">persist</span><span style="color: #009900;">&#40;</span>sock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     em.<span style="color: #006633;">getTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     em.<span style="color: #006633;">close</span><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></pre></div></div>

<p>There are some obvious pitfalls to this approach.  The object/relational mapping is very brittle; for this code to work, the IDs in the database always need to match the values that the ORM tool gets from the enumeration.  Changing these values at a later date could cause some surprising results, and you can&#8217;t insert new rows without updating the Enumeration and recompiling.  It does obviate the need to map the SOCK_PATTERN table, and you won&#8217;t need to worry about the details of cascading the persistent state of related Sock and SockPattern objects.</p>
<p>It&#8217;s just a new tool in the JPA toolbox.</p>
<p>(The code in this blog post was tested w/ Postgres and Toplink)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cereslogic.com/pages/2008/02/18/enumerated-types-with-jpa-and-your-sock/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Delicious and Simple JPA Mapping tutorial: The Pizza Shop</title>
		<link>http://www.cereslogic.com/pages/2008/01/31/new-jpa-tutorial-pizza-shop/</link>
		<comments>http://www.cereslogic.com/pages/2008/01/31/new-jpa-tutorial-pizza-shop/#comments</comments>
		<pubDate>Fri, 01 Feb 2008 00:15:00 +0000</pubDate>
		<dc:creator>mdesjardins</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[pizzashop]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://mikedesjardins.us/wordpress/2008/01/a-delicious-and-simple-jpa-mapping-tutorial-the-pizza-shop/</guid>
		<description><![CDATA[So, another JPA tutorial. What makes this one different? Well, for one thing, this one comes with a working, downloadable project that works with Eclipse, NetBeans, and IntelliJ IDEA 7. It&#8217;s packaged with Hibernate, Toplink, and OpenJPA. And it&#8217;s been tested with MySQL, PostgreSQL, MS SQL Server, and Sybase. In other words, it works with [...]]]></description>
			<content:encoded><![CDATA[<p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/pizzasign-715213.jpg"><img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer;" src="http://mikedesjardins.us/blog/uploaded_images/pizzasign-715207.jpg" alt="" border="0" /></a>So, another JPA tutorial.  What makes this one different?  Well, for one thing, this one comes with a working, downloadable project that works with Eclipse, NetBeans, and IntelliJ IDEA 7.  It&#8217;s packaged with Hibernate, Toplink, and OpenJPA.  And it&#8217;s been tested with MySQL, PostgreSQL, MS SQL Server, and Sybase.  In other words, it works with 36 different IDE/JPA Provider/Database combinations!</p>
<p>Another thing that makes this tutorial different is the subject matter: Pizza!  Who doesn&#8217;t love pizza?  Except lactose intolerant people. And people who can&#8217;t eat gluten.  But <span style="font-style: italic;">other than them</span>, who doesn&#8217;t love pizza?  So we&#8217;re going to create a simple database model for a pizza shop&#8217;s point-of-sale system.</p>
<p><span style="font-weight: bold;font-size:130%;" >The Schema</span><br />Unsurprisingly, the starting point for any ORM task is usually the database schema (there are people who start with the Objects and work &#8220;backward&#8221; to the schema, but I haven&#8217;t worked with any of them yet).  In our example, we have a pristine, consistent, completely normalized schema.  In other words, it&#8217;s probably nothing like you&#8217;ll ever be lucky enough to see in the real world! Here&#8217;s our simple little ERD:<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/pizza-erd-737227.jpg"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 454px; height: 326px;" src="http://mikedesjardins.us/blog/uploaded_images/pizza-erd-737223.jpg" alt="" border="0" /></a>From this ERD, we can infer the following:  1.) An order is comprised of zero or more pizzas.  2.) A pizza is associated with one size.  3.) A pizza may be associated with a string of text containing &#8220;special instructions.&#8221;  4.) A pizza may have zero or more toppings.  You probably also notice that each table has a column called version.  This will be used for an <a href="http://en.wikipedia.org/wiki/Optimistic_concurrency_control">optimistic locking strategy</a>.</p>
<p>The first question is, &#8220;Where should we start?&#8221; There&#8217;s no right answer for this, but I find that it&#8217;s easiest to start working with the entities with the fewest dependencies.   For example, you can&#8217;t have an order without a pizza, and you can&#8217;t have a pizza without a size, so maybe it makes sense to start with the size.  But before that, we&#8217;ll want an ID interface.</p>
<p><span style="font-size:130%;"><span style="font-weight: bold;">An ID Interface</span></span><br />First things first.  You&#8217;ll notice that, in our schema, every table has an integer ID. It&#8217;s often a good idea to have all of your objects implement the same interface for accessing the ID, because it makes it easier to create Generic DAOs (more about that in a future post). For now, let&#8217;s make a really simple interface like this:</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: #000000; font-weight: bold;">interface</span> IdObject <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> getId<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><span style="font-weight: bold;font-size:130%;" >Many-to-One Unidirectional Relationships</span><br />Now that we&#8217;ve gotten that out of the way, let&#8217;s create the Size class.  It&#8217;s a simple POJO littered with annotations, like this:</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;PIZZA_SIZE&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> Size <span style="color: #000000; font-weight: bold;">implements</span> IdObject <span style="color: #009900;">&#123;</span>
  @Id
  @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza_size_id&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
&nbsp;
  @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza_size_description&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> description<span style="color: #339933;">;</span>
&nbsp;
  @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza_size_base_price&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">BigDecimal</span> basePrice<span style="color: #339933;">;</span>
&nbsp;
  @Version @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> version<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> getId<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> id<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> id<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;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getDescription<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> description<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDescription<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> description<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;">description</span> <span style="color: #339933;">=</span> description<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">BigDecimal</span> getBasePrice<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> basePrice<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setBasePrice<span style="color: #009900;">&#40;</span><span style="color: #003399;">BigDecimal</span> basePrice<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;">basePrice</span> <span style="color: #339933;">=</span> basePrice<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> getVersion<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> version<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setVersion<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> version<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;">version</span> <span style="color: #339933;">=</span> version<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here are some notes on the annotations that were used in the above class:  <span style="font-weight: bold;"><br />@Entity</span> tells the JPA provider that this is a managed object.<br /><span style="font-weight: bold;">@Table</span> specifies the table name.  The JPA provider will attempt to default the table name to a sane value based on the class name, but I like to be explicit.  I&#8217;m funny that way.  Perhaps it&#8217;s <a href="http://en.wikipedia.org/wiki/Obsessive-compulsive_disorder">OCD</a>.  Or a <a href="http://en.wikipedia.org/wiki/Dick_cheney">power-trip</a>.<br /><span style="font-weight: bold;">@Column</span> indicates the name of the column, and can include other attributes about the column (you&#8217;ll see a few additional attributes later on).  Again, JPA can try to default this to sane values for you, but I like to be explicit.<br /><span style="font-weight: bold;">@Version</span> indicates that a particular column is used for indicating when a row is updated.  This column can then be used in an <a href="http://en.wikipedia.org/wiki/Optimistic_concurrency_control">optimistic locking</a> scheme.</p>
<p>Next, let&#8217;s do the Pizza object to show how we map a Pizza to a Size.<br />
<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/iStock_000004523455XSmall-738223.jpg"><img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 400px; height: 264px;" src="http://mikedesjardins.us/blog/uploaded_images/iStock_000004523455XSmall-738216.jpg" alt="" border="0" /></a>      One thing I that always tripped me up when I started out with ORM<br />tools was the difference between &#8220;One-to-Many&#8221; and &#8220;Many-to-One.&#8221; I never knew, if I call a relationship many-to-one in my metadata, is  <span style="font-style: italic;">this</span> object the one that there are many of, or is it the other way around?  The answer is that &#8220;this&#8221; object always comes first.  <span>Many</span>ToOne means that &#8220;there are many of <span style="font-style: italic;">this</span> object to one of <span style="font-style: italic;">those</span> objects.&#8221;  The &#8220;Many&#8221; side is often the side that has the foreign key.</p>
<p>In our case, there will be many Pizzas that are the same size.  So when we make our Pizza object, we will want to use the <span style="font-weight: bold;">@ManyToOne</span> annotation.  Here&#8217;s what the Pizza object looks like so far.  I&#8217;ve omitted the getters and setters to save space:</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;PIZZA&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> Pizza <span style="color: #000000; font-weight: bold;">implements</span> IdObject <span style="color: #009900;">&#123;</span>
  @Id
  @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza_id&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
&nbsp;
  @ManyToOne<span style="color: #009900;">&#40;</span>cascade<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>CascadeType.<span style="color: #006633;">ALL</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  @JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza_size_id&quot;</span>,nullable<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> Size size<span style="color: #339933;">;</span>
&nbsp;
  @Version @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> version<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// (Accessor methods omitted)</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that, when we made our Size object, we did not include a reference to the Pizza. That was an intentional design decision. In this application, it&#8217;s unlikely that we will want to instantiate a Size object, and get a collection containing all of the Pizzas of that size, so we don&#8217;t bother with mapping it. This is called <span style="font-style: italic;">unidirectional association</span>.</p>
<p>The <span style="font-weight: bold;">@ManyToOne</span> annotation specifies a cascade attribute.  There are several different settings for this attribute, which you can read more about <a href="http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html#ManyToOne">here</a>.  I tend to cascade the persistent state to all related objects because it reduces the amount of redundant API calls.  By default, JPA does <span style="font-style: italic;">not</span> cascade pers istence to related objects.  I&#8217;ll cover the cascade attribute in future posts, but for now, we&#8217;ll go with my personal preference, because I&#8217;m writing the article!</p>
<p>The <span style="font-weight: bold;">@JoinColumn</span> annotation indicates the column name that defines the linkage between the Pizza and the Size.  You&#8217;ll also note that we&#8217;ve included some additional attributes on our <span style="font-weight: bold;">@Column</span> and <span style="font-weight: bold;">@JoinColumn</span> annotations.  The unique and nullable attributes are particularly useful if you use tools to generate schema DDL from your mappings.</p>
<p><span style="font-size:130%;"><span style="font-weight: bold;">One-to-Many bidirectional relationships</span></span><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/iStock_000004945600XSmall-743572.jpg"><img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 229px; height: 157px;" src="http://mikedesjardins.us/blog/uploaded_images/iStock_000004945600XSmall-743564.jpg" alt="" border="0" /></a> Both the SpecialInstruction and the Order objects are examples of   One-to-Many bidirectional relationships.  In the case of SpecialInstruction, it is likely that we will care about which Pizza an instruction is associated with, and likewise for the Order.  A bidirectional one-to-many relationship implies that one object has a <span style="font-style: italic;">collection</span> of other o bjects.  For example, an Order has a collection of Pizzas.  First, lets add an order attribute to our Pizza object:</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;PIZZA&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> Pizza <span style="color: #000000; font-weight: bold;">implements</span> IdObject <span style="color: #009900;">&#123;</span>
.
.
  @ManyToOne<span style="color: #009900;">&#40;</span>cascade<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>CascadeType.<span style="color: #006633;">ALL</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  @JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza_order_id&quot;</span>,nullable<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> Order order<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> Order getOrder<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> order<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setOrder<span style="color: #009900;">&#40;</span>Order order<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;">order</span> <span style="color: #339933;">=</span> order<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
.
.
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Next, let&#8217;s create an Order object to contain our collection of Pizzas, like this:</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;PIZZA_ORDER&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> Order <span style="color: #000000; font-weight: bold;">implements</span> IdObject <span style="color: #009900;">&#123;</span>
  @Id
  @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza_order_id&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
&nbsp;
  @OneToMany<span style="color: #009900;">&#40;</span>cascade<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>CascadeType.<span style="color: #006633;">ALL</span><span style="color: #009900;">&#125;</span>,mappedBy<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;order&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Set</span> pizzas <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashSet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  @Version @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> version<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// (version and id accessors omitted)</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Set</span> getPizzas<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> pizzas<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setPizzas<span style="color: #009900;">&#40;</span><span style="color: #003399;">Set</span> pizzas<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;">pizzas</span> <span style="color: #339933;">=</span> pizzas<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addPizza<span style="color: #009900;">&#40;</span>Pizza pizza<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  pizza.<span style="color: #006633;">setOrder</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pizzas</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>pizza<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>There are a couple of things you worth noting about this mapping:<br />1.) The <span style="font-weight: bold;">@OneToMany</span> annotation uses the <span style="font-style: italic;">mappedBy</span> attribute to indicate which member of the related object defines the linkage between the two tables.  In this case, we are saying that the Pizza object contains a member named order, which defines the linkage between the two objects.<br />2.) I&#8217;ve created a utility method called <span style="font-weight: bold;">addPizza</span>.  This simplifies setting both sides of the bidirectional relationship by setting the Order object on the Pizza <span style="font-style: italic;">and</span> adding the Pizza to the Order&#8217;s collection.  Users of this class will only need to make one method call to do both.</p>
<p><span style="font-size:130%;"><span style="font-weight: bold;">Many-to-Ma</span></span><span style="font-size:130%;"><span style="font-weight: bold;">ny relationships via a Join Table</span></span><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mikedesjardins.us/blog/uploaded_images/cohdra100_1404-732725.JPG"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 233px; height: 174px;" src="http://mikedesjardins.us/blog/uploaded_images/cohdra100_1404-732719.JPG" alt="" border="0" /></a> The last thing we&#8217;ll cover is how to map Join Tables.  In our ERD, you can see that Toppings are modeled in the database with a TOPPING table that contains all of the valid toppings, a PIZZA table that contains all of the valid Pizzas, and a PIZZA_TOPPING table in the middle that maps all of the valid Pizzas to all of the valid Toppings.  You <span style="font-style: italic;">could</span> create an object called PizzaTopping that corresponds to the PIZZA_TOPPING table.  Then you could have a One-to-Many relationship from the Pizza to the PizzaTopping, and a One-to-One from each PizzaTopping to a Topping.  That would be very cumbersome to work with!  Fortunately, there&#8217;s a better way.</p>
<p>Logically, a Pizza has a collection of Toppings.  In our Java code, we really shouldn&#8217;t care about the fact that there is a PIZZA_TOPPING join table in the middle.  First, let&#8217;s create a simple Topping class:</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;TOPPING&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> Topping <span style="color: #000000; font-weight: bold;">implements</span> IdObject <span style="color: #009900;">&#123;</span>
  @Id
  @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;topping_id&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id<span style="color: #339933;">;</span>
&nbsp;
  @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;topping_description&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> description<span style="color: #339933;">;</span>
&nbsp;
  @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;topping_price&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">BigDecimal</span> price<span style="color: #339933;">;</span>
&nbsp;
  @Version @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> version<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// (Accessors omitted)</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is how the association would be mapped in the Pizza class:</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;PIZZA&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> Pizza <span style="color: #000000; font-weight: bold;">implements</span> IdObject <span style="color: #009900;">&#123;</span>
.
.
  @ManyToMany<span style="color: #009900;">&#40;</span>cascade<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>CascadeType.<span style="color: #006633;">ALL</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  @JoinTable<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;PIZZA_TOPPING&quot;</span>,
             joinColumns<span style="color: #339933;">=</span>@JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza_id&quot;</span><span style="color: #009900;">&#41;</span>,
             inverseJoinColumns<span style="color: #339933;">=</span>@JoinColumn<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;topping_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Set</span> toppings <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashSet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Set</span> getToppings<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> toppings<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setToppings<span style="color: #009900;">&#40;</span><span style="color: #003399;">Set</span> toppings<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;">toppings</span> <span style="color: #339933;">=</span> toppings<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addTopping<span style="color: #009900;">&#40;</span>Topping topping<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;">toppings</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>topping<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>The @JoinTable annotation defines three key attributes:<br /><span style="font-weight: bold;">name</span>: Identifies the name of the join table.<br /><span style="font-weight: bold;">joinColumns</span>: This attribute identifies the column name in the join table that points to <span style="font-style: italic;">this</span> object.<br /><span style="font-weight: bold;">inverseJoinColumns</span>: This attribute defines the column name in the join table that points to <span style="font-style: italic;">the other</span> objects.</p>
<p>From your Java code, the semantics for dealing with toppings on a pizza are just like any other set, much like you&#8217;d work with a One-to-many object.</p>
<p><span style="font-size:130%;"><span style="font-weight: bold;">Conclusion</span></span><br />Since the introduction of annotations, object/relational mapping is one of the easiest aspects of working with JPA over other frameworks.  You can see this whole project in action on your IDE of choice by downloading it <a href="http://www.mediafire.com/?2jgrzkizfy9"><span style="text-decoration: underline;">here</span></a> (or from <a href="http://mikedesjardins.us/blog/pizzashop-1.1.tar.gz">here</a> if that doesn&#8217;t work for some reason).  It should be a pretty reasonable starting point if you want a reference project to start playing with JPA.  I hope to revisit the Pizza Shop project to cover other JPA topics in future posts.<br /><a href="http://mikedesjardins.us/blog/pizzashop-1.0.tar.gz"><br /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cereslogic.com/pages/2008/01/31/new-jpa-tutorial-pizza-shop/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

