{"id":489,"date":"2010-08-20T17:03:00","date_gmt":"2010-08-20T17:03:00","guid":{"rendered":"https:\/\/opstree.com\/blog\/\/2010\/08\/20\/equals-implementation-for-lazy-loaded-objects-in-hibernate\/"},"modified":"2019-07-11T07:49:04","modified_gmt":"2019-07-11T07:49:04","slug":"equals-implementation-for-lazy-loaded-objects-in-hibernate","status":"publish","type":"post","link":"https:\/\/opstree.com\/blog\/2010\/08\/20\/equals-implementation-for-lazy-loaded-objects-in-hibernate\/","title":{"rendered":"Equals implementation for Lazy loaded objects in Hibernate"},"content":{"rendered":"<p>Most of you already know about the standard way to implement equals and hashCode for a class. As an example lets consider a User class<\/p>\n<pre style=\"background-color:#eeeeee;border:1px dashed rgb(153,153,153);color:black;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;line-height:14px;overflow:auto;padding:5px;width:100%;\"><code>\npublic class User {\nprivate Long id;\nprivate String name;\nprivate String address;\n}\n<\/code>\n<\/pre>\n<p><a name=\"more\"><\/a><\/p>\n<p>If we consider Id as unique identifier for logical equality of users then the equals and hashCode method can be written over &#8220;id&#8221;, as given below<\/p>\n<pre style=\"background-color:#eeeeee;border:1px dashed rgb(153,153,153);color:black;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;line-height:14px;overflow:auto;padding:5px;width:100%;\"><code>\n@Override\npublic int hashCode() {\nfinal int prime = 31;\nint result = 1;\nresult = prime * result + ((id == null) ? 0 : id.hashCode());\nreturn result;\n}\n\n\n@Override\npublic boolean equals(Object obj) {\nif (this == obj)\nreturn true;\nif (obj == null)\nreturn false;\nif (getClass() != obj.getClass())\nreturn false;\nUser other = (User) obj;\nif (id == null) {\nif (other.id != null)\nreturn false;\n} else if (!id.equals(other.id))\nreturn false;\nreturn true;\n}\n<\/code>\n<\/pre>\n<p>Now lets assume User is an entity thats managed by hibernate and is lazily loaded. So actually you will be interacting with the proxy(HibernateProxy) of User and not the actual User class.<\/p>\n<p>Now in this the equals implementation of User class will fail. If we check for equality of normal instance of User class with the proxy instance of User the equals method will return false. The reason for failure is the class check i.e<\/p>\n<pre style=\"background-color:#eeeeee;border:1px dashed rgb(153,153,153);color:black;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;line-height:14px;overflow:auto;padding:5px;width:100%;\"><code>\nif (getClass() != obj.getClass())\nreturn false;\n<\/code>\n<\/pre>\n<p>Since the class of Proxy user object is not User but a HibernateProxy class.<\/p>\n<p>To overcome this problem hibernate comes with a utility class HibernateProxyHelper which is used to get the actual class if the object passed to it is of type HibernateProzy. The above piece of code can be replaced with<\/p>\n<pre style=\"background-color:#eeeeee;border:1px dashed rgb(153,153,153);color:black;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;line-height:14px;overflow:auto;padding:5px;width:100%;\"><code>\nif (  HibernateProxyHelper.getClassWithoutInitializingProxy(obj) != getClass() ) {\nreturn false;\n}\n<\/code>\n<\/pre>\n<p>Another problem with the implementation of equals in case of proxy objects is we  cannot directly access the fields i.e<\/p>\n<pre style=\"background-color:#eeeeee;border:1px dashed rgb(153,153,153);color:black;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;line-height:14px;overflow:auto;padding:5px;width:100%;\"><code>\nif (!id.equals(other.id))\nreturn false;\n<\/code>\n<\/pre>\n<p>as proxy objects can access only the methods of the underlying object so instead  of directly accessing the field we have to use the getter method<\/p>\n<pre style=\"background-color:#eeeeee;border:1px dashed rgb(153,153,153);color:black;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;line-height:14px;overflow:auto;padding:5px;width:100%;\"><code>\nif (!id.equals(other.getId()))\nreturn false;\n<\/code>\n<\/pre>\n<p>So the updated equals implemention that should work perfectly fine in case of proxy objects also will be as given below<\/p>\n<pre style=\"background-color:#eeeeee;border:1px dashed rgb(153,153,153);color:black;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;line-height:14px;overflow:auto;padding:5px;width:100%;\"><code>\npublic boolean equals(Object obj) {\nif (this == obj)\nreturn true;\nif (obj == null)\nreturn false;\nif (  HibernateProxyHelper.getClassWithoutInitializingProxy(obj) != getClass() ) {\nreturn false;\n}\nUser other = (User) obj;\nif (id == null) {\nif (other.getId() != null)\nreturn false;\n} else if (!id.equals(other.getId()))\nreturn false;\nreturn true;\n}\n<\/code>\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Most of you already know about the standard way to implement equals and hashCode for a class. As an example lets consider a User class public class User { private Long id; private String name; private String address; } If we consider Id as unique identifier for logical equality of users then the equals and &hellip; <a href=\"https:\/\/opstree.com\/blog\/2010\/08\/20\/equals-implementation-for-lazy-loaded-objects-in-hibernate\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Equals implementation for Lazy loaded objects in Hibernate&#8221;<\/span><\/a><\/p>\n","protected":false},"author":150552946,"featured_media":29900,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[17907,2592186,114254396],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/11\/DevSecOps-1.jpg","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pfDBOm-7T","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/489"}],"collection":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/users\/150552946"}],"replies":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/comments?post=489"}],"version-history":[{"count":2,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/489\/revisions"}],"predecessor-version":[{"id":852,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/489\/revisions\/852"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media\/29900"}],"wp:attachment":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media?parent=489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/categories?post=489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/tags?post=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}