4 Replies Latest reply: Jan 13, 2011 6:44 AM by 643310 RSS

    Hibernate - Related to ID and hashCode

    824049
      Hi,

      I am new to Hibernate. I have a question as below related to id and hashCode.

      for many to many mapping, generally we store elements in "Set" for the other sode of the associated relationship.

      As per the Java API contract, to store elements properly inside Set, we should have correct implementation of hashCode() method.

      Keeping that in mind, lets say a session in hibernate is in open state and before I close that session, let me get one object(i.e y1 of Class Y) from the DB.

      now I have 1 object "y1" and the session is closed. This y1 now populated with its "ID" attribute value i.e 00047

      I will open another new session now and again try to retrieve the same object from DB(i.e y2 of class Y) .

      But the "ID" attribute of y2 will be same as y1 because both point to the same row in the DB.
      I do agree that different instances will be there because we are retrieving same data in 2 different Sessions and because of that y1==y2 will be "false".

      Now let's say I have implemented hashCOde() method inside class "Y" where I have used my "ID" attribute inside that method for giving me the hashCode value.

      SO I don't think any problem is there in this case if I will use primary key(i.e ID) in hashCOde because at any cost , Set will store only 1 element because hashCode for both y1 and y2 will be same i.e 00047.

      why then primary key is not used in hashCOde algorithim ? what is the issue here?
        • 1. Re: Hibernate - Related to ID and hashCode
          EJP
          What you use in the hashCode() algorithm is entirely up to you. So asking why it does or doesn't do something specific doesn't make any sense.
          • 2. Re: Hibernate - Related to ID and hashCode
            jtahlborn
            the one thing you need to be aware of when using the id in the hashCode (which is generally a good idea), is that new (not yet persisted) objects are kind of fragile. often the object id will not be assigned until the object has been persisted. so, you cannot put the object into a HashSet (or use its hashcode in any meaningful way) until it has first been persisted. as long as you take care of that, then using the id in the hashcode should be just fine.
            • 3. Re: Hibernate - Related to ID and hashCode
              796440
              821046 wrote:
              Set will store only 1 element because hashCode for both y1 and y2 will be same
              False.

              A Set can store many elements with the same hashCode. HashCode is not unique.
              • 4. Re: Hibernate - Related to ID and hashCode
                643310
                821046 wrote:
                As per the Java API contract, to store elements properly inside Set, we should have correct implementation of hashCode() method.
                As per API, "sets contain no pair of elements e1 and e2 such that e1.equals(e2)". Because of the equals() method, we also require a proper implementation of the hashCode() method.
                Set will store only 1 element because hashCode for both y1 and y2 will be same i.e 00047
                As jverd pointed out, only if you have respected the contract of both equals and the hashCode methods, will Set store the elements as expected. Only with the hashCode() it cannot be achieved.