Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7759855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:43:55+00:00 2026-06-01T13:43:55+00:00

My equals() and hashcode() methods have been overwritten properly. But when I am saving

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:43:56+00:00Added an answer on June 1, 2026 at 1:43 pm

    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;

    The problem stems from differences between object identity in the virtual machine (VM) and object identity in the database. In the VM you do not get an ID for an object; you simply hold direct references to the object. Behind the scenes, the VM does assign an eight-byte ID, which is what a reference to an object really is. The problems start when you persist an object in a database. Say you create a Person object and save it to a database (person1). Somewhere else in your code you read in the Person data and instantiate a new Person object (person2). You now have two objects in memory that are mapped to the same row in the database. An object reference can only point to one or the other, but we need a way to show that these are really the same entity. This is where object identity comes in.

    What you should do is simple, override the equals() and hashcode() according to the business functions of the entities.

    Here is a suggested solution from the hibernate forum article;

    Separating object id and business key
    To avoid this problem we recommend using the “semi”-unique attributes of your persistent class to implement equals() (and hashCode()). Basically you should think of your database identifier as not having business meaning at all (remember, surrogate identifier attributes and automatically generated vales are recommended anyway). The database identifier property should only be an object identifier, and basically should be used by Hibernate only. Of course, you may also use the database identifier as a convenient read-only handle, e.g. to build links in web applications.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)
Let's say that you have overridden an object's equals() and hashCode() methods, so that
Say you have a key class (KeyClass) with overridden equals, hashCode and clone methods.
For the benefit of hashtable we have two methods hashcode and equals.Internally when we
Our project contains several classes that we have equals() and hashCode() methods generated by
I believe I read somewhere people generating equals / hashcode / toString methods during
why java Object class has two methods hashcode() and equals()? One of them looks
The Object class has a number of methods such as equals, hashCode, notify, wait
I have overridden equals() and hashCode() in my ClassA. ClassA { //overridden equals() and
I've overridden the hashCode() and equals() methods in my Person class so that two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.