I need to mix saved and unsaved objects in Set.
I have the following equals method:
Code:
public boolean equals(Object obj) {
if (!(obj instanceof MyClass))
return false;
else {
if (obj==this) return true;
MyClass myEntity= (MyClass) obj;
if (null == this.getId() || null == myEntity.getId())
return false;
else
return (this.getId().equals(myEntity.getId()));
}
}
Eg. objects with tha same ID are equal (whether or not loaded in the same session).
How to write compareTo and hasCode methods to be compatible with HashSet and TreeSet?
The problem is that hashCode (compareTo) method result have to change after my object is saved (eg. id!=null).
Or should I not mix saved and unsaved objects in TreeSet (HashSet)?
I have ID's assigned by database.
Thanks helping me.