<?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>Jeff's Musings &#187; Hibernate</title>
	<atom:link href="http://blog.jeffshurts.com/tags/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jeffshurts.com</link>
	<description>Observations and accumulated wisdom</description>
	<lastBuildDate>Mon, 16 Apr 2012 16:39:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Of Entities and Identities</title>
		<link>http://blog.jeffshurts.com/2009/02/21/of-entities-and-identities/</link>
		<comments>http://blog.jeffshurts.com/2009/02/21/of-entities-and-identities/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 15:18:30 +0000</pubDate>
		<dc:creator>Jeff Shurts</dc:creator>
				<category><![CDATA[Professional]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Redpoint]]></category>

		<guid isPermaLink="false">http://blog.jeffshurts.com/?p=32</guid>
		<description><![CDATA[<p>I really like Hibernate.  Sure, it can be frustrating at times, the learning curve can be steep, and some of the exceptions you get when you do stupid things aren&#8217;t all that helpful.  But the fact that it&#8217;s frustrating and hard to learn are manifestations of the fact that it&#8217;s tackling some really thorny problems.  And the cryptic-exception issue does get a little better with each release.</p>
<p>There is one thing, however, that has bothered me since I started using Hibernate several years ago.  A deep bother.  Not a &#8220;this is a nuisance, but I can live with it&#8221; bother, but more of a &#8220;this is so fundamental an issue that there just <em><strong>has </strong></em>to be a better solution to it than is being offered by Hibernate&#8217;s authors&#8221; bother.</p>
<p>The problem:  bridging the gap between object identity and database identity.  In most modern database schemas, tables are defined with a numeric primary key column, which is populated through some unique key assignment mechanism (Identity columns in SQL Server or MySQL, sequences in Oracle, etc).</p>
<p>This works great at the database layer.  But problems arise in the JVM (or CLR for you NHibernate folks).  In order for objects representing database entities (those managed by Hibernate) to be handled properly in collections (including Hibernate cache), objects that have the same database identity need to be recognized as equals as well.  In Java, this means overriding equals() and hashCode() such that any two objects that represent the same database entity will be equal, and return the same hash code.</p>
<p>The &#8220;obvious&#8221; solution, at first blush, would be to base <strong>equals() </strong>and <strong>hashCode()</strong> on entity ID values.  This works great for entities <em>created </em>by Hibernate, which represent existing rows in a database, all with unique ID&#8217;s.  The problem has to do with <em>new</em> entites that you create programmatically, which haven&#8217;t yet been saved.  Those entities will typically have a <em>null</em> ID value &#8211; and if you base equality on ID values, any two unsaved instances will be considered equal.</p>
<p>There are actually two problems when dealing with unsaved entity objects.  The first, as stated above, is that they don&#8217;t yet have a unique identity &#8211; so multiple new instances will collide in collections like Maps and Sets.  The other problem is that the identity of these objects <em>will be set at some point</em>, which violates part of the contract of <strong>hashCode()</strong> &#8211; the part that says its value must not mutate over time.</p>
<p>To avoid all of thes problems, Hibernate&#8217;s authors have long recommended a workaround &#8211; use &#8220;natural keys&#8221; instead of identity values for<strong> equals() </strong>and <strong>hashCode(). </strong>The problem with this is that many entities simply don&#8217;t have a natural key &#8211; that is, a set of one or more attributes whose values uniquely identify the entity.  And those that do often fail the hashCode() qualification that the value should not change over time.</p>
<p>There are other possible solutions I&#8217;ve seen or come up with myself, but all had at least one glaring weakness.  It seemed that there was no silver bullet for dealing with database-generated identity values in Hibernate.</p>
<p>And therein lies the silver bullet.  Don&#8217;t use the database (or Hibernate) to generate identity values.</p>
<p>I stumbled across a wonderful article on O&#8217;Reilly&#8217;s OnJava site that quite clearly describes this problem (in more detail than I do here), and offers what I believe to be the only real solution.  Check it out at <a title="http://www.onjava.com/pub/a/onjava/2006/09/13/dont-let-hibernate-steal-your-identity.html" href="http://www.onjava.com/pub/a/onjava/2006/09/13/dont-let-hibernate-steal-your-identity.html" target="_blank">http://www.onjava.com/pub/a/onjava/2006/09/13/dont-let-hibernate-steal-your-identity.html</a>.</p>
<p>And now, as I look out the window at the late February snowstorm, I think maybe it&#8217;s time for <strong>me</strong> to hibernate&#8230;</p>
]]></description>
		<wfw:commentRss>http://blog.jeffshurts.com/2009/02/21/of-entities-and-identities/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

