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

  • Home
  • SEARCH
  • 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 6385967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:57:54+00:00 2026-05-25T02:57:54+00:00

I am developing an application in Hibernate where I have model classes like these:

  • 0

I am developing an application in Hibernate where I have model classes like these:

public class Employee
{
    private int ID;
    private String name;
    private Department department;
    //other properties
    //constructors, getters and setters
}

Note that the ID is not a value populated by the user and is populated using GenerationType.Identity as the strategy.

Also I have another class Department as follows:

public class Department
{
    private int ID;
    private String name;

    private Set<Employee> employees; //this is actually a HashSet

    //other implementations
}

There is a ManyToOne bi-directional relationship between an Employee and a Department.

So to add a new Employee to an existing Department, I do the following

Department existingDepartment = ...;
Employee newEmployee = ...;

existingDepartment.addEmployee(newEmployee);
employee.setDepartent(existinDepartment);

session.save(newEmployee);

Now conceptually two Employee objects are the same if they have the same ID. So my equals() method in the Employee class looks like this:

public boolean equals(Object o)
{
    if(!(o instanceOf Employee))
    {
        return false;
    }

    Employee other = (Employee)o;

    if(this.ID == o.etID())
    {
        return true;
    }

    return false;
}

Now the problem is when I create a new Employee(); I do not have its ID, since it will be assigned when it will be persisted. So when I say

existingDepartment.addEmployee(newEmployee);

the internal HashSet of the Department object is effectively using an equals() method which is broken [since it uses a member variable to determine equality that as not been initialized properly].

This seems like a very basic problem but, how do I solve it? Or am I designing my classes totally wrong? Or should my equals method be re-written to compare other values instead of ID, which I guess would be absurd.

  • 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-25T02:57:55+00:00Added an answer on May 25, 2026 at 2:57 am

    This seems like a very basic problem but, how do I solve it? Or am I
    designing my classes totally wrong? Or should my equals method be
    re-written to compare other values instead of ID, which I guess would
    be absurd.

    There are two different philosophies concerning this.

    a) equals() / hashCode() based on DB id

    Drawback: you can’t compare persistent and non-persistent objects

    b) equals() / hashCode() based on contents

    Drawback: two objects with the same id may turn out to be non-equal.

    I prefer the second approach, it makes more sense from a Java point of view (although admittedly not from a DB point of view). The only thing I’d want to make sure is that you never mix the approaches.

    This has been discussed many times before, btw:

    • Should I write equals() methods in JPA entities?
    • What is the best practice when implementing equals() for entities with generated ids
    • JPA : not overriding equals() and hashCode() in the entities?
      (etc.)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a web application using Struts 2.1.2 and Hibernate 3.2.6.GA. I have
Hey I am developing an desktop application using Spring and Hibernate, and I have
I am developing an application using oracle 11g, Java(struts2) and Hibernate. I have table
I am developing an application in struts 2 and hibernate 3. I have 3
I've a web application which uses framework like Struts and Hibernate. Currently I'm developing
I am developing an application using Hibernate and am trying to model the following
I am developing a hibernate application that connects to a MySQL database. The customers
we are developing a J2SE application and we are using Hibernate for our persistence
We are developing a web application using Spring framework and Hibernate ORM. As far
I'm developing application with GWT 2 and would like to add float panel that

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.