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?

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

is containsAll() for this check ok?
@RobertNiestroj No. containsAll() will also return true if other is a "subset" of this.
millimoose is True. No. Returns true if this list contains all of the elements you passed.
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.

2 Comments

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?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.