1 Reply Latest reply on Aug 6, 2007 4:20 AM by jc7442

    Equals and hashCode on Entities fails due to javassist

    ejb3workshop

      On a recent project we thought it would be a good idea to inherit a generic implementation of equals and hashCode, as well as the primariy key from an abstract entity. In the equals method we evaluate the class as well as the primary key. In most cases this worked really well for us, but in certain cases equality was not evaluated correctly. After looking further into the problem we found that in the cases it did not evaluate correctly the problem was caused by the classes not matching. It tried to compare one class (Test for example) against another (Test_$$_javassist_), which didn't equate and produces a false response.

      I am guessing that the javassist originates from entities being loaded LAZY.

      Is there a better way of achieving what we are trying to do ?

      We are using JBoss 4.0.5 with EJB3 RC9.

      Any suggestions welcome
      Alex

        • 1. Re: Equals and hashCode on Entities fails due to javassist
          jc7442

          I suppose that in your equals you invoke getClass. I had the same problem. There is a Wiki on hibernate about equals and hashcode (http://www.hibernate.org/109.html).

          For my entites, I never invoke getClass, I use a workaround to be sure not to have proxy. That's ugly, but I did not find something more elegant in javassist publi API.


          public static Class getBaseClass(Class srcClass) {
           if (srcClass.getName().contains(CGLIB)
           || srcClass.getName().contains(JAVASSIST)) {
           return srcClass.getSuperclass();
           }
           return srcClass;
          
           }