I want to check if entity is in a Collection member (@OneToMany
or @ManyToMany
) of another entity:
if (entity2.getEntities1().conta
We tend to let IDE generate hashCode()
and equals()
for us. Be careful though. When you generate those methods for JPA Entities. Some versions of equals()
checks for class identity
// ... inside equals() - wrong approach for Entities (cause of generate proxies)
if (o == null || this.getClass() != o.getClass()) {
return false;
}
// ...
This would break your collections with some JPA libraries as those libraries create proxies to your entities (subclasses), like for example MyGreatEntity_$$_javassist_7
in Hibernate.
In Entities always allow subclasses in equals()
.