Discussions

EJB design: Composite Key in Entity Bean

  1. Composite Key in Entity Bean (2 messages)

    Hi,
         Can anybody guide me in overriding the hashcode() method in my PK class, if my composite key consists of a String, int and a boolean variable.

    Thanks & Regards,
    Vishal
  2. How about this? Just took it from weblogic samples !

    Let say the variables are: String strName, Boolean boolName, int intName



      private int hashCode = -1;

      public int hashCode(){

        if (hashCode == -1) {

          hashCode = strName.hashCode() ^ boolName.hashCode() ^ intName;
        }

        return hashCode;
      }

    Senthil.
  3. Hi Senthil,
          Thanks for your immediate response. This approach looks fine(except the fact that I should wrap my boolean variable to Boolean object in order to get the hashcode out of it). But is there not a concrete step of doing this ? In most of the sample examples, I could see the varibles getting concatinated and then taken the hashcode out of it.
    eg. (intName + strName + boolName).hashCode()
          Is this also a right approach ? This returns a different hashcode if the sequence of the variables concatinated is changed.