0

I have trouble writing correct equals() function in POJOs to use with hierbante. My main problem is with List's.

For example I have an Entity Request which has a list of Persons

   @ManyToMany(cascade = CascadeType.ALL)
   @LazyCollection(LazyCollectionOption.FALSE)
   @JoinTable(name = "JOIN_TABLE_REQUEST_TO_PERSON", joinColumns =
           @JoinColumn(name = "REQUEST_ID"), inverseJoinColumns =
           @JoinColumn(name = "PERSON_ID"))
   private List<Person> proxyList = new ArrayList<Person>();

Now I use Netbeans 7.3 generated equals() and for this list it generates me the code:

...
if (this.proxyList != other.proxyList && (this.proxyList == null || !this.proxyList.equals(other.proxyList))) {
         return false;
}
...

However this does not work correctly when I add Persons to the list. I had to change this code to:

...
if (!this.proxyList.containsAll(proxyList)) {
    return false;
}
...

How should it be done correctly? Do you have any other best practices for writing equals for hibernate?

yvonnezoe
  • 7,129
  • 8
  • 30
  • 47
Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119

2 Answers2

0

Problem is here

!this.proxyList.equals(other.proxyList))

Its testing the equals method of List object not individual proxy Object.

Inside the condition iterate them and check weather they are equal or not.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0
  1. You generated an equals() method for the Request class, so Netbeans, just like any other IDE implemented the method such that all instance level references should point to the same instance of an object So it takes the proxyList reference and checks it with the same property of the Request being checked for equality.

  2. List (being an interface) does not implement an equals method. So the when you call an equals method on a list reference, it basically compares if the two references point to the same object.

  3. Your logic dictates that two Request objects should be considered equal, if the Person objects contained in the requests are the same. Then that is how you should implement your Request equals() method. You cannot expect an IDE to do that for you.

I don't understand the connection between your equals() problem and Hibernate. To me your problem seems to be a logical one, not related to any persistence framework.

This is how I would do it:

  1. Implement hashCode and equals method for the Person class. Considering only those fields that uniquely identify each Person

  2. In the equals() of Request, In addition to this.proxyList.containsAll(proxyList), I would also check to make sure the size of the two proxyList instances is the same.

Hope this helps. Let me know if I'm off base.

Akshay
  • 3,158
  • 24
  • 28
  • Yeah the point to Hibernate seems unnecessary here. I wanted to know if there are other things i have to consider when i write `equals` in POJO that are used by hibernate. Does equals matter in any way for hibernate? – Robert Niestroj Jun 13 '13 at 08:35
  • No it shouldn't.. hibernate uses the field mapped as ID to determine identity of an object. It does not use equals method. However, once instances are loaded by hibernate, you are in the pure Java world, where the only correct way to say two objects are the same is the hashCode and equals methods. – Akshay Jun 13 '13 at 08:37