<?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>Artem&#039;s blog &#187; equals</title>
	<atom:link href="http://artemgolubev.com/tag/equals/feed/" rel="self" type="application/rss+xml" />
	<link>http://artemgolubev.com</link>
	<description>Thoughts on software</description>
	<lastBuildDate>Sat, 17 Dec 2011 01:52:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Some approach for writing equials() and hashCode()</title>
		<link>http://artemgolubev.com/some-approach-for-writing-equials-and-hashcode/</link>
		<comments>http://artemgolubev.com/some-approach-for-writing-equials-and-hashcode/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 03:40:24 +0000</pubDate>
		<dc:creator>Artem</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[DTO]]></category>
		<category><![CDATA[entity]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[hashCode]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://artemgolubev.com/some-approach-for-writing-equials-and-hashcode/</guid>
		<description><![CDATA[The problem described in details in http://www.hibernate.org/109.html
The problem is that if we implement equals()/hashCode() as comparison and hashCode() of entity&#8217;s ids (see http://djeang.blogspot.com/2005/08/override-equals-and-hashcode-methods.html) then the hashCode() (and, possibly, equals()) will change after saving entities if ids are generated by DB.
To solve this issue I can suggest to use inner classes with equals()/hashCode(). It will not [...]]]></description>
			<content:encoded><![CDATA[<p>The problem described in details in <a href="http://www.hibernate.org/109.html">http://www.hibernate.org/109.html</a><br />
The problem is that if we implement equals()/hashCode() as comparison and hashCode() of entity&#8217;s ids (see <a href="http://djeang.blogspot.com/2005/08/override-equals-and-hashcode-methods.html">http://djeang.blogspot.com/2005/08/override-equals-and-hashcode-methods.html</a>) then the hashCode() (and, possibly, equals()) will change after saving entities if ids are generated by DB.<br />
To solve this issue I can suggest to use inner classes with equals()/hashCode(). It will not affect Hibernate&#8217;s internal collections taskflow, but will allow you to use ids in equals()/hashCode() <span style="font-style: italic">after</span> entities are persisted. On other hand hashCode() and equals() will not change in your persistent objects after you have saved it.</p>
<pre>
public class Test3 {
   static class SomeEntity {
       private Integer id;

       private String data;

       public SomeEntity() {
           super();
       }

       public SomeEntity(Integer id, String data) {
           super();
           this.id = id;
           this.data = data;
       }

       public Integer getId() {
           return id;
       }

       public void setId(Integer id) {
           this.id = id;
       }

       public String getData() {
           return data;
       }

       public void setData(String data) {
           this.data = data;
       }

       public class SomeEntityId {
           private SomeEntity link = null;

           public SomeEntityId(SomeEntity link) {
               super();
               this.link = link;
           }

           @Override
           public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if ((obj == null) || (obj.getClass() != this.getClass()))
   return false;
  if (this.link.getClass() != ((SomeEntityId) obj).getLink().getClass())
   return false;

               Integer thisId = link.getId();
               Integer otherId = ((SomeEntityId) obj).getLink().getId();
               if ((thisId == null) || (otherId == null))
                   throw new RuntimeException("Id not defined!");

               return thisId.equals(otherId);
           }

           @Override
           public int hashCode() {
               Integer thisId = link.getId();
               if (thisId == null)
                   throw new RuntimeException("Id not defined!");

               return thisId.hashCode();
           }

           public SomeEntity getLink() {
               return link;
           }
       };

       public SomeEntityId getDTO() {
           return new SomeEntityId(this);
       }
   }

   public static void main(String[] args) {
       SomeEntity e0 = new SomeEntity(null, "no id");
       SomeEntity e1 = new SomeEntity(1, "e1");
       SomeEntity e2 = new SomeEntity(2, "e2");
       SomeEntity e3 = new SomeEntity(3, "e3");
       SomeEntity e33 = new SomeEntity(3, "e33");

       Set<someentity.someentityid></someentity.someentityid> aSet = new HashSet<someentity.someentityid></someentity.someentityid>();
       // aSet.add(e0.getDTO());
       aSet.add(e1.getDTO());
       aSet.add(e2.getDTO());
       aSet.add(e3.getDTO());
       aSet.add(e33.getDTO());

       System.out.println("set size: " + aSet.size());
       System.out.println("set:");
       for (SomeEntity.SomeEntityId seId : aSet) {
           SomeEntity se = seId.getLink();
           System.out.println("entity: " + se.getData());
       }
   }
}</pre>
<p>I even could suggest you to add it to entities code generation in such tools as Hibernate Tools.<br />
I would also like to mention also that SomeEntityId object could be stored in transient field of an entity.<br />
Comments are welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://artemgolubev.com/some-approach-for-writing-equials-and-hashcode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

