java persistence API
Java

JAVA PERSISTENCE API

Object Relational Mapping (ORM) is referred to as mapping of Java objects into the database tables and vice versa. JPA i.e., Java Persistence API is one of the possible approaches for ORM. Any developer can map, save, update and get data from relational database tables to Java objects and vice versa. Java enterprise applications and Java Standalone applications use JPA.

The main and important implementations of JPA are Hibernate, EclipseLink and Apache OpenJPA. EclipseLink is known as the reference implementation of JPA. The mapping between Java objects and database tables is defined by Persistence meta data. JPA allows programmers to work directly with objects rather than working with structured Query Language(SQL) statements.

In Java class JPA meta data is typically defined by annotations Or at the same time, XML can be used to define the meta data or a combination of both annotation and XML. A XML configuration overwrites the annotations.

javax.persistence.Entity is used to annotate the classes that are persisted in a database. Such a class is called Entity. All entity classes must define a primary key. Keys can be defined as a collection of the fields or as a single field. @GeneratedValue annotation in JPA allows the programmers to autogenerate the primary key in the database. By default, one can easily fetch the class name from the table name itself. In-order to replace table with new name @Table (name=”NEWTABLENAME”)

JPA can use either your instance variables (fields) or the corresponding getters and setters to access the fields stored in the database. By default, each field is mapped to a column with the name of the field. Each column is defined with a default value and one can change the default name via @Column (name=”newColumnName”). In order to identify the unique ID of the database entry, one can use @id annotation. @Transient Field will not be saved in database.

Classes can have one to one, one to many, many to one, and many to many relationships with other classes. JPA allows the programmers to define relationships between classes. A relationship can be defined in terms of bidirectional or unidirectional. Example for defining a relation is as follows: @ManyToMany(mappedBy=”attributeOfTheOwningClass”).

The operations from and to the database are provided by the Entities which are managed by the entity manager javax.persistence. EntityManager. Changes are automatically propagated to the database by the Entity Manager. The persistence context describes all Entities of one Entity manager. persistence.xml file describes the persistence unit, which is in turn described via the in the META-INF directory of the source folder and it in turns contains EntityManagerFactory which creates the EntityManager.

JPA defines a set of concepts that can be implemented by any tool or framework and it’s not a is tool or framework; While JPA’s object-relational mapping (ORM) model was originally based on Hibernate, it has since evolved.

Author: STEPS

Leave a Reply

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