3 Replies Latest reply on Jan 9, 2002 6:22 PM by Emmanuel Cecchet

    Deployer warning equals() and hashCode() must be overridden.

    Ramin Akhbari Newbie

      I'm using JBoss 2.4.0/Tomcat 3.2.3 bundle.

      I'm getting these two warning messages when I deploy my jar file that has the code for BmpEntity1 in it:

      [Verifier]
      Bean : com/aplia/test/entity/BmpEntity1
      Section: 9.2.9
      Warning: The primary key class must override equals().

      [Verifier]
      Bean : com/aplia/test/entity/BmpEntity1
      Section: 9.2.9
      Warning: The primary key class must override hashCode().

      I've checked, double-checked, and triple-checked the PK class I'm using for this bean and it definitely does override both equals() and hashCode().

      The jar file does deploy fine ultimately but I'd like to know why it's giving me these messages. Anyone have any ideas why the Verifier would be giving this message if it's really not true?

      Thanks in advance,

      Ramin

        • 1. Re: Deployer warning equals() and hashCode() must be overrid
          David Jencks Master

          Are the following true?

          1. two primary key objects A and B with the same constructor arguments satisfy A.equals(B)

          2. if primary key objects A and B satisfy A.equals(B) then A.hashCode() == B.hashCode()

          • 2. Re: Deployer warning equals() and hashCode() must be overrid
            Emmanuel Cecchet Newbie

            > Are the following true?
            >
            > 1. two primary key objects A and B with the same
            > constructor arguments satisfy A.equals(B)

            I have a problem with JBoss about that. My beans are working fine but I get this warning telling me that I should override equals(). If you use the default constructor some instance variables may be null. Usually null should only be equal to null.
            So I think JBoss reports this warning even with a correct implementation of the equals() function. Here is an example of a working code where JBoss 2.4.3 reports a 9.2.9 warning:


            public class BidPK implements java.io.Serializable {

            public Integer id;

            public BidPK() {}

            public BidPK(Integer uniqueId){
            id = uniqueId;
            }

            public int hashCode() {
            if (id == null)
            return 0;
            else
            return id.intValue();
            }

            public boolean equals(Object other)
            {
            boolean isEqual = (id == other); // true if id and other are null
            if ((other instanceof BidPK) && (id != null)) {
            isEqual = (id.intValue() == ((BidPK)other).id.intValue()); // true only if both have same value
            }
            return isEqual;
            }

            }

            Is there anything incorrect in this implementation of equals ?

            Thanks for your help,
            Emmanuel

            • 3. Re: Deployer warning equals() and hashCode() must be overrid
              Emmanuel Cecchet Newbie

              Never mind the previous message. I didn't 'cvs update' and I used the wrong version. The correct code is :

              public boolean equals(Object other) {
              boolean isEqual = false;
              if (other instanceof BidPK) {
              if (id == null)
              isEqual = (id == ((BuyNowPK)other).id);
              else
              isEqual = (id.intValue() == ((BuyNowPK)other).id.intValue());
              }
              return isEqual;
              }

              This version works fine, don't worry!