My equals() and hashcode() methods have been overwritten properly. But when
I am saving equal objects, they appear in the database. How can I solve this problem? I want to have unique set of elements in the database.
Thanks.
Here is the mapping file:
<hibernate-mapping>
<class name="vertical.model.filter.Filter" table="FILTER">
<id name="id" type="long">
<generator class="native"/>
</id>
<discriminator column="TYPE" type="string"/>
<subclass name="vertical.impl.filter.AbstractFilter" abstract="true">
<set name="options" table="FILTER_OPTION" cascade="all">
<key column="FILTER_ID"/>
<many-to-many column="OPTION_ID" class="vertical.model.filter.FilterOption"/>
</set>
<subclass name="vertical.impl.services.filter.GrfFilter"
discriminator-value="rabota">
</subclass>
</subclass>
</class>
</hibernate-mapping>
It is a classical problem with no theoretical solution but there are practical ways of solving it.
Let me take some lines from the article;
What you should do is simple, override the
equals()andhashcode()according to the business functions of the entities.Here is a suggested solution from the hibernate forum article;