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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:20:15+00:00 2026-05-30T07:20:15+00:00

I am trying to design an entity model using JPA2.0 via hibernate. I have

  • 0

I am trying to design an entity model using JPA2.0 via hibernate.

I have a domain like this: There are a bunch of restaurants, so obviously there is a restaurant entity. Each restaurant has a number of different dishes that it serves and 0-to-many restaurants may serve the same thing. Each dish has a number of ingredients, and some dishes will share ingredients. Each ingredients has several nutrients, and again each nutrient might be possessed by 0-to-many of the ingredients.

Im struggling because Im not even sure exactly what my problem is, but I know that I am missing something. I believe the question lies in how exactly to enforce the domain model and what are the implications for primary keys and .equals/.hashcode methods.

Originally I was going to use a generated surrogate key. Each entity would have something like

@Id
@GeneratedValue
public Long id;

And the .equals would test equality for all the entities members except id. For example a restaurant would be equal no matter the value of Long id, but in the equality of its List member. (and of course Dish would keep calling .equals all teh way down the hirarchy to nutrients.)

So when I give a DAO bean a restaurant, I only want it persisted iff there are no eqivalent restaurants in the data store (via .equals). But I guess that would involve a rather laborious jpql call, which I don’t find elegant or efficient (please let me know if I am wrong. I am often wrong… lol). Further along the way, if it encountered say an ingredient that was already in the data store (but part of a dish that was not and therefore newly persisted) it would associate new dish with the ingredient already there rather than creating an entirely new (and logically duplicate) ingredient.

The more I thought About it, the more painful it sounded to persist things higher up in this heirarchy.

I started to look into the idea of a composite Key (via @IdClass or @EmbeddedId) that would itself hold the child members. This seems like persisting things higher in the hierarchy would be as simple as finding by id and persisting if I dont find anything. But I’m very suspicious of this… It does not seem right. It would mean that I am creating entities where all members and entities themselves and part of the composite key.

Some things to note. There are a relatively finite amount of nutrients. The space for ingredients is {numOfNutrients}^{typesOfNutrients} which could be fairly large. by the time we get up to restaurants, it will be almost impossible for a client to construct a duplicate resaurant by acccident as the space will be truly large. There comes a point when I don’t actually care if a duplicate is in the data store, because it is so unlikely to happen. In fact it would almost be interesting to see if this occurred. Also not that the restaurant example is contrived. My real domain is similar in form though.

Sorry for the rambling. I know there are a lot of ideas in post. But I feel they are all so related, I wanted to bring them up all at once.

Can someone please offer some wizardry?

I am aware of this post: A class that behaves like @Entity and @Embeddable
Usually I see something like, don’t do this “except in special cases”. Is what Im talking about one of these cases. If not, what exactly would be the appropriate case to use this.

Also note that once a Restaurant is put in the database, its representation is immutable. I realize this is unrealistic with the contrived example, but thats the case of my domain. This makes me think that making all the members part of the composite key is the way to go….

  • 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-30T07:20:16+00:00Added an answer on May 30, 2026 at 7:20 am

    Hibernate doesn’t use equals and hashCode to know if an entity is already persistent or not. If the entity has an ID, it is persistent. Else, it’s new. And two entities are the same if they have the same ID. The equals method is only used to compare two detached entities, or to compare a detached entities with an attached one, in your code (and not in the Hibernate code).

    You also need equals and hashCode if the entities are stored in a Set.

    If you really need to have an equals and hashCode method, using the collection of children entities to implement them is a very bad idea. Not only do you need to load half of the database to be able to call them, but as soon as you add an ingredient to a dish, its hashCode changes, which “corrupts” the set where it’s stored.

    You should use a non-nullable immutable unique functional field of the entity to implement equals and hashCode (the restaurant’s name, the dish name, the nutrient’s name). And definitely use surrogate identifiers. If you don’t have such a field, you can use the ID. But just make sure to have it generated before storing the entity in a Set.

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

Sidebar

Related Questions

I'm trying to design an entity model for an application that uses ASP.Net membership
I'm trying to design my app's interface in IB using a storyboard and have
I'm trying to understand some fundamental best practices using Entity Framework. My EDM design
I have an App that I am trying to design the data model for.
I'm trying to visualize a 3-layer design with Entity Framework. If I have a
I am trying to design model associations in rails that consist of the following
I am trying to design an IDE-like (Non-Editable) program with a richtextbox control. Basically,
I am trying to design my Class Diagram. But I get stuck on this
I am trying to design a data model which can hold a very large
I'm trying to design an Android layout that would look like the following: Entity1

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.