<?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>Technical Notes &#187; sql</title>
	<atom:link href="http://blog.anthonychaves.net/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.anthonychaves.net</link>
	<description>Life is software and jujitsu</description>
	<lastBuildDate>Tue, 16 Feb 2010 22:15:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bidirectional relationships &#8211; owning and inverse sides</title>
		<link>http://blog.anthonychaves.net/2009/12/16/bidirectional-relationships-owning-and-inverse-sides/</link>
		<comments>http://blog.anthonychaves.net/2009/12/16/bidirectional-relationships-owning-and-inverse-sides/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 03:11:18 +0000</pubDate>
		<dc:creator>Anthony Chaves</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://blog.anthonychaves.net/?p=157</guid>
		<description><![CDATA[I used to have a hard time remembering which side was which in a bidirectional relationship using ORM.  I was confused because the owning side was not the side I thought it was when thinking about a relationship.  Let&#8217;s say we have a Customer that has many Orders.  The English language semantics [...]]]></description>
			<content:encoded><![CDATA[<p>I used to have a hard time remembering which side was which in a bidirectional relationship using ORM.  I was confused because the owning side was not the side I thought it was when thinking about a relationship.  Let&#8217;s say we have a Customer that has many Orders.  The English language semantics suggest to me that the Customer side of the relationship is the owning side.  It &#8220;has many&#8221; Orders, right?</p>
<p>But every ORM framework will tell you that the &#8220;many&#8221; side of the relationship should be the owning side and the &#8220;one&#8221; side is the inverse.  Why?  Let&#8217;s look at some Java class definitions using JPA.</p>
<pre name="code" class="Java">
@Entity
public class Customer implements Serializable {
	@Id
	public String id;

	public String name;
	public String emailAddress;

	@OneToMany
	List&lt;Order> order = new ArrayList&lt;Order>();

	@Version
	public int version;

}
</pre>
<p>A Customer has a one-to-many relationship with Order.</p>
<pre name="code" class="java">
@Entity
@Table(name="orders")
public class Order implements Serializable {
	@Id
	public String id;

	@ManyToOne
	Customer customer;

}
</pre>
<p>And the Order references the customer to which it belongs.  Here is our bi-directional relationship.  But we don&#8217;t know why the Order is the owning-side of the relationship yet.  If we were to create a database schema based on these classes we would get three tables:</p>
<pre>
mysql> describe Customer;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id           | varchar(255) | NO   | PRI | NULL    |       |
| emailAddress | varchar(255) | YES  |     | NULL    |       |
| name         | varchar(255) | YES  |     | NULL    |       |
| version      | int(11)      | YES  |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+

mysql> describe customer_orders;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| CUSTOMER_ID | varchar(255) | YES  | MUL | NULL    |       |
| ORDER_ID    | varchar(255) | YES  | MUL | NULL    |       |
+-------------+--------------+------+-----+---------+-------+

mysql> describe orders;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id          | varchar(255) | NO   | PRI | NULL    |       |
| CUSTOMER_ID | varchar(255) | YES  | MUL | NULL    |       |
+-------------+--------------+------+-----+---------+-------+
</pre>
<p>The tables that map to our entities look fine.  We also get a join table to map from Customer to Order.  Even though the Order class can refer directly to its Customer the Customer must look at the join table to get to its Orders.</p>
<p>The SQL query generated by the ORM to get the Orders associated with a customer would join the customer_orders table to get a list of order ids associated with that customer, then join the orders table on those order ids.  There is a more direct way we can get there, especially since the orders table already references its customer id.  We need to indicate to the JPA runtime that the Customer can reference a column on the orders table.  We do that with the mappedBy attribute in @OneToMany.</p>
<pre name="code" class="Java">
@Entity
public class Customer implements Serializable {
	@Id
	public String id;

	public String name;
	public String emailAddress;

	@OneToMany(mappedBy="customer")
	List&lt;Order> order = new ArrayList&lt;Order>();

	@Version
	public int version;

}
</pre>
<p>When we add the mappedBy attribute on the @OneToMany annotation the JPA runtime knows that there is a field in the related object named customer.  The column this field maps to is the appropriate foreign key to use to find associated Orders.  Creating the database schema with this change to the Customer class @OneToMany annotation we get only two tables.</p>
<pre>
mysql> describe Customer;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id           | varchar(255) | NO   | PRI | NULL    |       |
| emailAddress | varchar(255) | YES  |     | NULL    |       |
| name         | varchar(255) | YES  |     | NULL    |       |
| version      | int(11)      | YES  |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> describe orders;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id          | varchar(255) | NO   | PRI | NULL    |       |
| CUSTOMER_ID | varchar(255) | YES  | MUL | NULL    |       |
+-------------+--------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
</pre>
<p>Now the JPA runtime knows that the customer_id column on the orders table can map between an Order and a Customer.  The owning-side of a bidirectional relationship is the one with the foreign key column on the table &#8211; the orders table.  The orders table owns the relationship because it is responsible for the column that maps between Customer and Order.  The &#8220;owning-side&#8221; is not a statement about a Customer has many Orders, the logical relationship between entities.  It is a statement about which underlying table is responsible for the mapping data between two tables and the entities mapped by those tables.  </p>
<p>I was just reminded of my initial questions about why we use these terms when I wrote a @OneToMany(mappedBy=&#8221;&#8230;&#8221;) a few days ago.  As always I&#8217;m open to comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anthonychaves.net/2009/12/16/bidirectional-relationships-owning-and-inverse-sides/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Scalability round-table/forum on January 28</title>
		<link>http://blog.anthonychaves.net/2009/01/13/scalability-round-tableforum-on-january-28/</link>
		<comments>http://blog.anthonychaves.net/2009/01/13/scalability-round-tableforum-on-january-28/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 21:03:28 +0000</pubDate>
		<dc:creator>Anthony Chaves</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[scalability]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.anthonychaves.net/?p=80</guid>
		<description><![CDATA[The Boston Scalability User Group is hosting a technology round-table meeting on Wednesday January 28th at 6 p.m.  The meeting is at the IBM Innovation Center in Waltham, MA.
This is the first time we&#8217;ve done the round-table style meeting and I&#8217;m excited to see how it goes.  Guests are encouraged to come prepared with questions, [...]]]></description>
			<content:encoded><![CDATA[<p>The Boston Scalability User Group is hosting a technology round-table meeting on Wednesday January 28th at 6 p.m.  The meeting is at the IBM Innovation Center in Waltham, MA.</p>
<p>This is the first time we&#8217;ve done the round-table style meeting and I&#8217;m excited to see how it goes.  Guests are encouraged to come prepared with questions, answers and opinions on application scalability tools, strategies and designs.  Hot topics will include platform and software stack, cloud computing and resources, vendor tools and support and CDNs. Those are my guesses about hot topics doesn&#8217;t mean the meeting is limited to those topics.</p>
<p>Guests, be the regular or first-timers, will drive the direction of the discussion.  We&#8217;ll talk about solving technical problems based on past experience or serve as an advisory panel on where and when to use a particular tool.</p>
<p>Full meeting details are at the <a href="http://www.bostonsug.org">BostonSUG web site</a>.  We ask that you sign up for the meeting at the <a href="http://www.bostonsug.org/meeting-registration/">meeting registration page</a> so we have an idea of how much food to buy.  There will be snacks and bottled water at this meeting.</p>
<p>Hope to see everyone on the 28th at 6 p.m.!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anthonychaves.net/2009/01/13/scalability-round-tableforum-on-january-28/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Evil Query</title>
		<link>http://blog.anthonychaves.net/2008/03/18/the-evil-query/</link>
		<comments>http://blog.anthonychaves.net/2008/03/18/the-evil-query/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 16:21:47 +0000</pubDate>
		<dc:creator>Anthony Chaves</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[scalability]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[criteria]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[query]]></category>

		<guid isPermaLink="false">http://blog.anthonychaves.net/java/2008/03/18/the-evil-query</guid>
		<description><![CDATA[Here is the use case: I want to search all of my bookmarks by multiple tags with an &#8220;and&#8221; operation.  That means I want to see bookmarks that are tagged with Java AND JavaEE, not just Java and not just JavaEE.  I want to see bookmarks tagged with C# AND 3.5 not just [...]]]></description>
			<content:encoded><![CDATA[<p>Here is the use case: I want to search all of my bookmarks by multiple tags with an &#8220;and&#8221; operation.  That means I want to see bookmarks that are tagged with Java AND JavaEE, not just Java and not just JavaEE.  I want to see bookmarks tagged with C# AND 3.5 not just C# and not just 3.5.  For most of the project I&#8217;m using Hibernate and the Criteria API with a lot of success.  In this case I was not able to massage Hibernate into creating the correct query and that&#8217;s fine.  Hibernate has its uses and it is acceptable to write custom SQL when the need arises.  I wrote a method to build up the query and execute it.  Here is part of the method that builds up the query:</p>
<table bgcolor="#ffffff" border="0" cellpadding="3" cellspacing="0">
<tr>
<td align="left" nowrap="nowrap" valign="top"><code><font color="#000000">StringBuilder sqlString =<br />
</font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong>new </strong></font><font color="#000000">StringBuilder</font><font color="#000000">(<br />
</font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#2a00ff">                                "select distinct(b.id)<br />
</font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#2a00ff">from bookmarks b "</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>int </strong></font><font color="#000000">counter = </font><font color="#990000">0</font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>for</strong></font><font color="#000000">(</font><font color="#000000">Tag tag : tags</font><font color="#000000">) {</font><br />
<font color="#ffffff">      </font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#000000">String alias = </font><font color="#2a00ff">"j" </font><font color="#000000">+ Integer.toString</font><font color="#000000">(</font><font color="#000000">counter</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">      </font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#000000">String joinFragment = </font><font color="#2a00ff">" inner join bookmarks_to_tags {alias}<br />
</font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#2a00ff"> on b.id = {alias}.bookmark_id<br />
</font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#2a00ff"> and {alias}.tag_id = ? "</font><font color="#000000">;</font><br />
<font color="#ffffff">      </font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#000000">joinFragment = joinFragment.replace</font><font color="#000000">(</font><font color="#2a00ff">"{alias}"</font><font color="#000000">, alias</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">      </font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#000000">sqlString.append</font><font color="#000000">(</font><font color="#000000">joinFragment</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">      </font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#7f0055"><strong> </strong></font></code><code><font color="#000000">counter++;</font><br />
<font color="#ffffff">    </font><font color="#000000">}</font></code></td>
</tr>
</table>
<p>Right away it looks like this query will get very expensive for large tag sets.  For smaller sets the query performs in a reasonable amount of time but more than 12 tags makes the query take quite a while.  This is exacerbated by the fact that this query will be executed multiple times if there is a sufficiently large result set (default is over 25) to give the user a paginated view.  This totally flies in the face of being nice to the database.  Hammering it with a complex query over and over won&#8217;t do the users any favors.</p>
<p>What are my options here?  I can work on making this query less complex (something that I will certainly do), but there is still the problem that, no matter how nice this query becomes, the query will be executed over and over as a user pages through the results.  The complexity of the query and its repeated execution makes the result set a good candidate for caching.  Caching the result set in memory with a key tied to the user or their session will allow the expensive query to be executed once with the bookmarks stored in a cache.  When the user pages through the results the cache will be consulted first where the result set will be stored.  I&#8217;m in the process of selecting a caching package for this project so I will post my selection and integration process soon.</p>
<p>Until then, I&#8217;d be happy to hear any suggestions on making this query a little nicer while preserving its &#8220;andness&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anthonychaves.net/2008/03/18/the-evil-query/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
