Arturas M
Arturas M

Reputation: 4369

Do entity classes always need IDs?

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?

Upvotes: 0

Views: 845

Answers (1)

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.

Upvotes: 2

Related Questions