<?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>Mike Desjardins&#039; Series of Tubes &#187; pizzashop</title>
	<atom:link href="http://www.mikedesjardins.net/content/category/pizzashop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikedesjardins.net/content</link>
	<description>freelance software developer consultant in portland, maine</description>
	<lastBuildDate>Wed, 02 Feb 2011 00:14:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Hibernate Criteria Subqueries: Exists</title>
		<link>http://www.mikedesjardins.net/content/2008/09/hibernate-criteria-subqueries-exists/</link>
		<comments>http://www.mikedesjardins.net/content/2008/09/hibernate-criteria-subqueries-exists/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 17:00:00 +0000</pubDate>
		<dc:creator>Mike Desjardins</dc:creator>
				<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[pizzashop]]></category>

		<guid isPermaLink="false">http://mikedesjardins.us/wordpress/?p=33</guid>
		<description><![CDATA[NOTE: Apologies for the recent blogging hiatus. I just started a new job and haven&#8217;t had a lot of time to devote to this blog lately! While working on a recent project, I came into a situation where I needed to do an &#8220;exists&#8221; query, using a Criteria-style query. The online documentation for this feature [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://mikedesjardins.us/blog/uploaded_images/national-pizza-shop-718025.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 169px; height: 210px;" src="http://mikedesjardins.us/blog/uploaded_images/national-pizza-shop-718008.jpg" border="0" alt="" /></a><span style="font-style: italic;">NOTE: Apologies for the recent blogging hiatus.  I just started a new job and haven&#8217;t had a lot of time to devote to this blog lately!</span></p>
<p>While working on a recent project, I came into a situation where I needed to do an &#8220;exists&#8221; query, using a Criteria-style query.  The online documentation for this feature is a little sparse, so I thought I&#8217;d share what I did.</p>
<p><span style="font-weight: bold;">The Pizza-shop Data Model (Again)</span></p>
<div style="text-align: left;">I keep reusing a data model for a Pizza shop in my posts, and this post will be no different.  This data model first appeared in my <a href="http://mikedesjardins.us/blog/2008/01/new-jpa-tutorial-pizza-shop.html">JPA mapping tutorial</a>.  Here&#8217;s an ERD of the model again:</div>
<p><a href="http://mikedesjardins.us/blog/uploaded_images/pizza-erd-737223.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px;" src="http://mikedesjardins.us/blog/uploaded_images/pizza-erd-737223.jpg" border="0" alt="" /></a><span style="font-weight: bold;">Find me Orders with Small Pizzas!</span><br />
Given this model, what if we needed to find each order that contained a small pizza?  Suppose your database had the following data:</p>
<p><a href="http://mikedesjardins.us/blog/uploaded_images/table-752077.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://mikedesjardins.us/blog/uploaded_images/table-752035.jpg" border="0" alt="" /></a><br />
As with <a href="http://mikedesjardins.us/blog/2008/01/new-jpa-tutorial-pizza-shop.html">my earlier posting</a>, the object model has a PizzaOrder class that contains a Set of Pizza objects which correspond to each customer order.  Your first inclination might be to do a criteria-within-a-criteria, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Criteria criteria <span style="color: #339933;">=</span> Criteria.<span style="color: #006633;">forClass</span><span style="color: #009900;">&#40;</span>PizzaOrder.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
criteria.<span style="color: #006633;">createCriteria</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pizza&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pizza_size_id&quot;</span>,<span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
List<span style="color: #339933;">&lt;</span>PizzaOrder<span style="color: #339933;">&gt;</span> ordersWithOneSmallPizza <span style="color: #339933;">=</span> criteria.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You&#8217;d be in for a bit of a surprise, though.  While you might expect only two Pizza orders to be returned (namely, orders #1 and #2), you&#8217;ll actually have three orders in the result set; because order #2 has two small pizzas in it, order #2 will appear twice in your results!</p>
<p>The reason why this happens is pretty simple, and it becomes clear if you enable Hibernate&#8217;s SQL output feature.  To locate all of the pizza orders which contain a small pizza, Hibernate needs to do an inner join to the PIZZA table.  This is true regardless of whether you&#8217;ve mapped the Pizza objects to be fetched lazily; the join is required because of your query criteria, not because of your mappings.  <span style="font-style: italic;">Note: it&#8217;d be really nice if Hibernate were clever enough to identify from the result set that it had duplicate PIZZA_ORDER records, and build the Set of Pizza objects accordingly, but I suspect that this would be a very difficult thing to do, so I&#8217;m not holding my breath.</span></p>
<p><span style="font-weight: bold;">The Right Way to Do It</span><br />
What you&#8217;re really trying to do is to obtain all Pizza Orders where an associated small pizza exists.  In other words, the SQL query that you&#8217;re trying to emulate is</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span>
  <span style="color: #993333; font-weight: bold;">FROM</span> PIZZA_ORDER
 <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span>
                 <span style="color: #993333; font-weight: bold;">FROM</span> PIZZA
                <span style="color: #993333; font-weight: bold;">WHERE</span> PIZZA<span style="color: #66cc66;">.</span>pizza_size_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
                  <span style="color: #993333; font-weight: bold;">AND</span> PIZZA<span style="color: #66cc66;">.</span>pizza_order_id <span style="color: #66cc66;">=</span> PIZZA_ORDER<span style="color: #66cc66;">.</span>pizza_order_id<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The way that you do that is by using an &#8220;exists&#8221; Subquery, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Criteria criteria <span style="color: #339933;">=</span> Criteria.<span style="color: #006633;">forClass</span><span style="color: #009900;">&#40;</span>PizzaOrder.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
DetachedCriteria sizeCriteria <span style="color: #339933;">=</span> DetachedCriteria.<span style="color: #006633;">forClass</span><span style="color: #009900;">&#40;</span>Pizza.<span style="color: #000000; font-weight: bold;">class</span>,<span style="color: #0000ff;">&quot;pizza&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
sizeCriteria.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pizza_size_id&quot;</span>,<span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
criteria.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Subqueries.<span style="color: #006633;">exists</span><span style="color: #009900;">&#40;</span>sizeCriteria.<span style="color: #006633;">setProjection</span><span style="color: #009900;">&#40;</span>Projections.<span style="color: #006633;">property</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pizza.id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
List<span style="color: #339933;">&lt;</span>PizzaOrder<span style="color: #339933;">&gt;</span> ordersWithOneSmallPizza <span style="color: #339933;">=</span> criteria.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And <span style="font-style: italic;">voila</span>, the result will contain two PizzaOrders!<br />
<span style="font-style: italic;">Photo Credit: </span><a href="http://flickr.com/people/squeakymarmot/"><span style="font-style: italic;">Squeaky Marmot</span></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikedesjardins.net/content/2008/09/hibernate-criteria-subqueries-exists/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.mikedesjardins.net/content/2008/01/new-jpa-tutorial-pizza-shop/</link>
		<comments>http://www.mikedesjardins.net/content/2008/01/new-jpa-tutorial-pizza-shop/#comments</comments>
		<pubDate>Fri, 01 Feb 2008 01:15:00 +0000</pubDate>
		<dc:creator>Mike Desjardins</dc:creator>
				<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.mikedesjardins.net/content/2008/01/new-jpa-tutorial-pizza-shop/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

