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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:54:30+00:00 2026-06-14T08:54:30+00:00

Well, I would like to know whether we always need to have an id

  • 0

Well, I would like to know whether we always need to have an id field inside an entity class? I somewhat heard it’s a bad practice to always have an id field in your entity class, because then it’s bound to the database, even though you might not need the database at all.

In my case I have such class:

public class User {
private String firstName = "";
private String lastName = "";
private String personCode = "";
private Date birthDate = new Date();
private Gender gender = Gender.MALE;
private String email = "";
private String password = "";
private UserState userState = UserState.UNAPPROVED;

In my case I identify users with their email addresses.
The equivalent of this class as a table in database does have an extra userId field, which identifies it uniquely.
Now I’m asking whether I need to add an idUser field in this java class?

I’ve managed to map it with Hibernate like this:

<class
    name="User"
    table="User"
    discriminator-value="U" >

    <!-- <id column="idUser" type="long" /> this one wouldn't even work... -->
    <id name="email" />

    <discriminator column="discriminator" type="string" />

    <property name="firstName" />
    <property name="lastName" />
    <property name="personCode" />
    <property name="birthDate" type="date"/>

   <property name="userState" column="userState" length="15">
         <type name="org.hibernate.type.EnumType">
              <param name="enumClass">com.nortal.pirs.datamodel.enumeration.UserState</param>
              <param name="type">12</param>
          </type>
    </property>

    <property name="password" />

     <property name="gender" column="gender" length="15">
         <type name="org.hibernate.type.EnumType">
              <param name="enumClass">com.nortal.pirs.datamodel.enumeration.Gender</param>
              <param name="type">12</param>
          </type>
    </property>


     <subclass name="Patient" extends="User" discriminator-value="P" >
         <property name="additionalInfo" column="additionalInfo" />

     </subclass>

     <subclass name="SpiProfessional" extends="User" discriminator-value="S" />


</class>

Now it’s kind of also stupid that the Patient and SpiProfessional who extend the User have their unique id, which are also only visible in the database.

So far I didn’t manage to save the Patient and SpiProfessional with hibernate. I get this error:

2012-11-14 07:36:44,265 [main] WARN  org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1054, SQLState: 42S22
2012-11-14 07:36:44,266 [main] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unknown column 'additionalInfo' in 'field list'

Can anyone explain me about having id’s in java classes? Also does anyone have an idea how to solve this problem with hibernate?

  • 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-14T08:54:31+00:00Added an answer on June 14, 2026 at 8:54 am

    It may help to define your understanding of Entity. If you’re thinking of “Just an Object With a database backend”, you’re not getting the whole picture.

    Eric Evans does an outstanding job defining Entity in Domain Driven Design as :

    ““An object defined primarily by its identity is called an ENTITY.”
    [Evans 2003]”

    An ENTITY is anything that has continuity through a life cycle and
    distinctions independent of attributes that are important to the
    application’s user. [Evans 2003]

    What’s important to note here is that, at any level in an application that an object is expected to be uniquely identifiable and have a life cycle where it will be changing state (and perhaps triggering events as a result), then it makes sense to have some notion of attributes that uniquely identify it.

    Integer based ids have historically been used for efficiency: it is computationally easier to tell if 2 numbers are equal than do repeated comparisons on different attributes.

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

Sidebar

Related Questions

I would like to know whether the actual code of a C# class gets
I would like to know whether this JSON script example I made is well
I would like to know how difficult it would be to merge the well-designed
Ok well heres what I would like to do in PHP: http://www.wordle.net/ I know
I have requirement where some times I would like to load children as well
I have a well div, and I would like to attach a small text/image
I have a well-developed application for a phone, but I would like to add
I have an app using SQLite3. It's running pretty well, but I would like
I would like to know whether this works in worst case as Θ(n+m) ?
I just shoot myself in the foot and would like to know whether there

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.