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 7518643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:46:43+00:00 2026-05-30T01:46:43+00:00

I am using hibernate and have bi-directional relations. How should I correctly override equals()

  • 0

I am using hibernate and have bi-directional relations. How should I correctly override equals() for both classes.

Here is the code (using guava-Objects): (PS: This is a bad example and choice of entities, but I am interested in learning the recommended way to go about it)

Destination:

@Entity
@Table(name = "DESTINATION")
public class Destination{
    private Integer id;
    private String name;
    private Set<DestinationAlias> aliases = new HashSet<DestinationAlias>(0);


    @Override
    public boolean equals(Object obj) {
        if(obj == this) return true;

        if(obj instanceof Destination){
            final Destination otherDestination = (Destination) obj;
            return Objects.equal(getName().toUpperCase(), otherDestination.getName().toUpperCase()) 
                    && Objects.equal(getAliases(), otherDestination.getAliases());
        }
        return false;
    }
}

DestinationAlias:

@Entity
@Table(name = "DESTINATIONALIAS")
public final class DestinationAlias {
    private Integer idDestinationAlias;
    private String alias;   
    private Destination mainCity;

    @Override
    public boolean equals(Object obj) {
        if(obj == this) return true;

        if(obj instanceof DestinationAlias){
            final DestinationAlias otherAlias = (DestinationAlias) obj;
            return Objects.equal(getAlias().toUpperCase(), otherAlias.getAlias().toUpperCase())
                    && Objects.equal(getMainCity(), otherAlias.getMainCity());
        }
        return false;
    }
}

This is the test case:

@Test
public void testEqualsto(){
    Destination dest = new Destination("abc", 1.0f, 1.0f);
    dest.getAliases().add(new DestinationAlias("abc alias", dest));

    Destination dest1 = new Destination("abc", 1.0f, 1.0f);
    dest1.getAliases().add(new DestinationAlias("abc alias", dest1));

    assertEquals(dest, dest1);
}

As expected, stackoverflow occurs, since each equals() in turns calls the other equals() and a cycle occurs.

What is the recommended way to override equals() for bidirectional entities.

  • 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-05-30T01:46:44+00:00Added an answer on May 30, 2026 at 1:46 am

    We have to deconflict manually. In DestinationAlias I’d change the equals expression so that it will compare the Destionation IDs only (they should be unique):

    return Objects.equal(getAlias().toUpperCase(), otherAlias.getAlias().toUpperCase())
                && Objects.equal(getMainCity().getId(), otherAlias.getMainCity().getId());
                                              ^^^^^^^^                          ^^^^^^^^
    

    Further Reading

    • equals() method for classes with bidirectional association
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code using hibernate to throw a custom exception on error and
I am using Hibernate to do mapping. One of my classes have a set
One challenge using hibernate is that manged classes have to have a default constructor
I am using Hibernate with JPA annotations. I have a one-directional many-to-many association that
I am using list for one-to-many bi-directional mapping in hibernate.. following is my code
I am using Hibernate and I have these two classes. public class ApplicantYear{ @ManyToOne(fetch
I was using Hibernate 3.6 where I have a code like this: list =getSession().createSQLQuery(queryString)
I have a webservice using Hibernate as DAL - using MySql with InnoDB. Since
I'm using Hibernate as ORM and I have my DAOs annotated with @Repository .
I have a problem with a query using hibernate. The entity class EntityClass has

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.