Entities in JPA

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello, We have seen how to create a JPA project in eclipse. In this article we will learn entities in jpa.

Entities in JPA:

According to the definition given in the book Mastering Java Persistence API by Apress, An entity is essentially a noun or a grouping of state associated together as a single unit.

An entity is a something that has attributes and relationships.It will have an identifier attribute so that it is recognized in persistence context. They need to be persisted in some data store. Entities in JPA need to have the property of persistability in database.

Consider a POJO Employee as follows.

As of now, this is not an Entity, its simple POJO. When you annotate this POJO using  @Entity  and  @Id  then it will become persistable and recognizable in persistence context.

The characteristics of Entities in JPA are:

  1. Persistability: The state of an entity can be represented in a data store to be accessed later.
  2. Identity: An object identity must be possessed by entity. When an object exists in the database, it has persistent identity.
  3. Transactionality: Entities can be created, updated and deleted in any context. These operations are normally done in the context of a javax.persistence.EntityTransaction
  4. Granularity: Entities are not primitives. They are objects. They have relationships and states. Entities in JPA are stored in a single row in database.

The annotations in the code i.e.  @Entity  and  @Id  are not part of the Entity, they are not stored in database. This is called as entity metadata. It enables the persistence layer to recognize, interpret and manage the entity from the time it is loaded into runtime.

 

References for further study :

 

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Leave a Reply

Your email address will not be published. Required fields are marked *