What are Entities in JPA 2.1?
In this article we will understand about the Entities in Java Persistence API 2.1. Entities are POJO's class which is managed by the associated EntityManager. The EntityManager provides the API for performing various operations such as Create, Retrieve, Update and Delete operations.
The instance of an Entity represents one column in the table and it is identified by the primary key, which corresponds to the primary key of the table.
Here are few things you must know about Entities:
- The entity class is simple POJO class that must be annotated
with the JPA Entity annotation. It can also be defined using the
XML descriptor as an
entity.
- There is a requirement to have the no-argument constructor
in the Entity class. The entity class may have other
constructors with argument(s). The no-argument constructor must
be public or protected.
- If you are defining an entity class then it must a top-level
class. You can't designate an enum or interface as an entity.
- While defining an entity class make sure it is not not final
as it is not supported by the JPA specification. Entity class
must not contain any final variables or methods.
- You should implement the Serializable interface if the
entity instance is to be passed by value or used as detached
object.
- The JPA entities class supports inheritance, polymorphic
associations, and polymorphic queries which very useful for
developing applications object-oriented way.
- In JPA both abstract and concrete classes can use as an
entities class.
- The instance variables of the entity should only be accessed
through setters and getters methos.
Accessing the Persistence Fields and Properties
The entities is managed by the persistence provide runtime and the state of an entity can be access by the persistence provider's runtime. You can load an entity and then access the state only using setters and getters methods of the Entity instance.
Here are the method signatures for accessing the single-valued persistence properties:
- T getProperty()
- void setProperty(T t)
Here is an example of "Product.java" Entity:
package net.roseindia.model; import javax.persistence.*; /** * @author Deepak Kumar * More tutorials at http://www.roseindia.net */ @Entity @Table(name="products") public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name="product_name") private String productName; @Column(name="product_description") private String productDescription; @Column(name="stock_qty") private Double stockQty; @Column(name="price") private Double price; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getProductDescription() { return productDescription; } public void setProductDescription(String productDescription) { this.productDescription = productDescription; } public Double getStockQty() { return stockQty; } public void setStockQty(Double stockQty) { this.stockQty = stockQty; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } }
You can see the entity is annotated with the @Entity annotation. The variables are private and can be accessed only through setters and getters methods.
Primary Keys and Entity Identity
Each entity is identified by a primary key and it is unique. The primary key corresponds to the primary key of the table in database. The primary key is annotated with the @Id annotation.
Here are the few important rules for Primary Key
- The class used for primary key must have a public, no-arg
constructor.
- The class used for primary key must implement serializable
interface.
- If a property-based primary class in used then the
properties of the primary key class must be public or protected.
- You entities class may extend both entity and non-entity classes, and non-entity classes may extend entity classes.
You can check more details in the JPA 2.1 specification.
How to persist an Entity?
The em.persist(obj) method is used to persist an entity.
Check the tutorial CRUD application in JPA 2.1 for learning more details about performing various operations on the Entity class.
Multiplicity in Entity Relationships
Entities supports following types of relationships:
- One-to-one
- One-to-many
- Many-to-one
- Many-to-many