matbrown wrote:
Problems with collections are often related to equals() and hashCode() implementations. Can you post your equals() and hashCode() method(s) for the object(s) that are being collected in the Set?
The hashCode and equals methods of the objects we put into the set are taking into account all attributes except the ID (the database id). For example:
The class MultipleImageUpload:
Attributes:
Code:
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private Long ticket;
@Lob
private URI location;
//and more attributes and associations which are not used in the equals and hashCode methods
The hashCode is computed as:
Code:
@Override
public int hashCode()
{
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((location == null) ? 0 : location.hashCode());
result = PRIME * result + ((ticket == null) ? 0 : ticket.hashCode());
return result;
}
The equals method looks like:
Code:
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final MultipleImageUpload other = (MultipleImageUpload) obj;
if (location == null)
{
if (other.location != null)
return false;
}
else if (!location.equals(other.location))
return false;
if (ticket == null)
{
if (other.ticket != null)
return false;
}
else if (!ticket.equals(other.ticket))
return false;
return true;
}
The location and ticket attributes are actually never null, we are sure of that. Also, the database entries for ticket and location are different for each entry. So no two MultipleImageUpload objects should have the same hashCode and be equal.
Do we have to add the id to the hashCode and equals computations? I would understand that we have to add the id to those computations if the objects were added to the set before their (other) attributes are set. Is that so?
Thanks in advance for any pointers.
--
Joerg