In this article introduces you with Entities in Java Persistence API 2.1. The Entities are managed by the EntityManager. You can perform CRUD operations in JPA using EntityManager instance.
In this article introduces you with Entities in Java Persistence API 2.1. The Entities are managed by the EntityManager. You can perform CRUD operations in JPA using EntityManager instance.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:
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:
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
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:
Ads