<?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>Web2.0 Tutorials &#187; hashcode</title>
	<atom:link href="http://www.zulutown.com/blog/tag/hashcode/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zulutown.com/blog</link>
	<description>All the Guides You Need to Become a Web2.0 Expert</description>
	<lastBuildDate>Mon, 13 Jul 2009 07:50:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hibernate, hashCode, equals and Eclipse</title>
		<link>http://www.zulutown.com/blog/2009/04/12/hibernate-hashcode-equals-and-eclipse/</link>
		<comments>http://www.zulutown.com/blog/2009/04/12/hibernate-hashcode-equals-and-eclipse/#comments</comments>
		<pubDate>Sun, 12 Apr 2009 09:18:57 +0000</pubDate>
		<dc:creator>Zulutown Webmaster</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[automatic generation]]></category>
		<category><![CDATA[bean]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[contains]]></category>
		<category><![CDATA[embeddedId]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[fetch]]></category>
		<category><![CDATA[getters]]></category>
		<category><![CDATA[hashcode]]></category>
		<category><![CDATA[instanceof]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[lazy loading]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[proxy class]]></category>

		<guid isPermaLink="false">http://www.zulutown.com/blog/?p=115</guid>
		<description><![CDATA[Java programmers use often Eclipse to write their code, and it&#8217;s common to rely on the Eclipse features to quickly generate the hashCode() and equals(Object obj) methods of the beans.
Working on a Hibernate based application, I often noticed unexpected issues, and after some in depth debugging I&#8217;ve found that probably some of these problems were [...]]]></description>
			<content:encoded><![CDATA[<p>Java programmers use often Eclipse to write their code, and it&#8217;s common to rely on the Eclipse features to quickly generate the <em>hashCode()</em> and <em>equals(Object obj)</em> methods of the beans.</p>
<p>Working on a Hibernate based application, I often noticed unexpected issues, and after some <em>in depth</em> debugging I&#8217;ve found that probably some of these problems were caused by the Eclipse automatic generation of <code>equals</code> method.</p>
<p>In my opinion, this is mostly caused by the fact that Hibernate generates on top of our <em>entity bean</em> classes other <em>proxy classes</em> that overrides the getters, and other methods in order to provide the <em>lazy loading</em> of data.</p>
<p>I.e.: When Hibernate loads from the DB a <code>Movie</code> bean, it will have, for example, an associated list of <code>MovieActor</code> objects. Obviously Hibernate will not fetch all the data related to every single actor, but anyway it will generate the <em>stub</em> for the MovieActor objects.</p>
<p>The programmer would be tempted to use code like this:</p>
<pre>Movie aMovie = getEntityManager.find(...);
MovieActor aMovieActor = getEntityManager.find(...);
if (aMovie.getActors().contains(aMovieActor)) {
	System.out.println(aMovieActor.getActor().getName() + " acted in the movie " + aMovie.getTitle());
}</pre>
<p>I noticed that <code>contains</code> method often returns <code>false</code> even if that actor really acted in that movie!<br />
After some long debugging sessions, my final idea is that the problem is how Eclipse automatically generated the <code>equals</code> method of <code>MovieActor</code> class:</p>
<pre>	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof MovieActor))
			return false;
		MovieActor other = (MovieActor) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}</pre>
<p>In this code <code>id</code> is an <code>@EmbeddedId</code> composed by the Actor and the Movie associations. What I believe that causes problems is: <strong><code>other.id</code></strong>!</p>
<p>This is because probably the Id of the <code>other</code> object hasn&#8217;t been loaded yet, and its fetching will happen on the calling of <code>getId()</code> of the proxy class built on MovieActor.</p>
<p>So, I changed the <code>equals</code> method this way:</p>
<pre>	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof MovieActor))
			return false;
		MovieActor other = (MovieActor) obj;
		if (id == null) {
			if (other.getId() != null)
				return false;
		} else if (!id.equals(other.getId()))
			return false;
		return true;
	}</pre>
<p>And after this little change, <code>contains</code> began to work correctly!</p>
<p>Another <strong>very important</strong> thing is to check <em>use &#8216;instanceof&#8217; to compare types</em> otherwise some ugly code would be generated.<br />
Some checks like <code>if (getClass() != obj.getClass())</code> would miserably fail when hibernate proxy classes are used.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zulutown.com/blog/2009/04/12/hibernate-hashcode-equals-and-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
