Hello to everybody,
I have the problem concerning hashCode/equals. I have 2 entities DbRssSpider and DbRssProperty an the following relation:
Spider:
Code:
@OneToMany(mappedBy = "spider", cascade = { CascadeType.ALL}, targetEntity = DbRssProperty.class)
@OrderBy("id")
public Set<DbRssProperty> getProperties()
{
return propertySet;
}
Property:
Code:
@ManyToOne()
@JoinColumn(name = "spiderId")
public DbRssSpider getSpider()
{
return spider;
}
As specified with the cascade-annotation, I want a property to be persistet when added to the set of the spider. Additionally, I want two persitent instances of DbRssProperty to be equal if they have the same id. So I had to override equals and hashCode. The Problem is the following:
If I use the id in hashCode, its impossible to implement the properties-Set with a java.util.HashSet because the added instances a hashed first and persisted second(this changes the hash-value).
If I don't use the id in hashCode, it's possible that two properties with the same id have different hashCodes.
Does anyone have an idea of how to implement hashCode?
Thank you in advance,
Mathias