-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: equals/hash code problems
PostPosted: Tue Apr 29, 2008 6:20 am 
Newbie

Joined: Tue Apr 29, 2008 6:12 am
Posts: 3
In my application all the objects stored in the database have an integer code. I've developed the following functions:
Code:
@Override
   public int hashCode() {
      //This method has been reimplemented because of Hibernate. See Hibernate tutorials for more info.
      
      final int prime = 31;
      int result = 1;
      result = prime * result + code;
      // This is done to make the same as the equals method
      if (code == 0)
         result = prime * result + super.hashCode();
      else
         result = prime * result;
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      //This method has been reimplemented because of Hibernate. See Hibernate tutorials for more info.      
      
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      final BusinessObject other = (BusinessObject) obj;
      // the following is done in order to use objects which have not been
      // stored yet to the db.
      if (code == 0) {
         if (other.code != 0)
            return false;
         super.equals(obj);
      }
      if (code != other.code)
         return false;
      return true;
   }


With this functions I say that two objects are equal if the code is equal, or, in case there is no code, that they are the same instance.

When I use this code my application does not work properly. Anyone has any ideas?

Thanks


Top
 Profile  
 
 Post subject: questions
PostPosted: Tue Apr 29, 2008 6:04 pm 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
"My application does not work properly" doesn't cut it, sorry :-)
what is your test code, what is the output, what are the exceptions, what hibernate are you using, what dbms????


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 4:14 am 
Newbie

Joined: Fri Apr 25, 2008 5:09 am
Posts: 4
I have the same problem at the moment.

The problem may be


Code:
if (getClass() != obj.getClass())
         return false;


obj.getClass returns the proxy class which is not equal to the persistent class. You can get the actual class with HibernateProxyHelper.getClassWithoutInitializingProxy(obj) but I´d like my business tier code to be free of any hibernate specific code ...


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 10:12 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
It definitely looks like you're doing a comparison on the proxy. This will not give you the desired behaviour.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 10:14 am 
Newbie

Joined: Fri Apr 25, 2008 5:09 am
Posts: 4
How can I compare with the object behind the proxy?

Edit:
Wrong question, I´ve given the answer for this myself. What I wanted to know was how to get the Object behind the proxy. My Problem ist that the test for object identity in the equals method fails because the object is compared with its proxy.

Code:
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      ...
   }


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 10:33 am 
Newbie

Joined: Wed May 28, 2008 6:06 am
Posts: 6
elch78 wrote:
How can I compare with the object behind the proxy?



Code:
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      ...
   }


i think the error here is using the ( ==) it checks if the reference of the objects are the same or not.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 09, 2008 9:37 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
== is simply comparing the memory locations of two objects. If they are on different JVMs, they will have a different == result, even if they represent the same row of data in the database.

Use .equals in your comparisons.

This tutorial discusses the requirements and why we need to override .equals and hashcode in a JPA or Hibernate entity. It uses the compound primary key class as a backdrop, but it applies for all entity based classes you will run into:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=15usingcompoundprimarykeys

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.