How long have you used Hibernate? So do you know what JPA is?

Tram Ho

What is JPA?

JPA stands for Java Persistence API, describing a mechanism that allows Java Objects to exist when an application is shut down or completely shut down, simply being saved to the database.

But not all java objects are stored in the database, JPA allows us to define the corresponding objects to be stored in the database, how will they be stored? and how to pull them from database to application for further processing etc.

Speaking of which, please conclude that JPA is not a tool or a framework at all, JPA is a set of concepts using the mapping between java object and database.

Do you guys know Hibernate ORM is one of the famous JPA implementations and widely used in the Java community although we still have some other JPA implementaion like Apache OpenJPA, EclipeseLink, jOOQ etc.

What is ORM?

ORM stands for object-relational-mapping, a technology that allows the transition from objects in object-oriented languages ​​to relational databases and vice versa.

Why use ORM?

Most applications need to store and manipulate databases. In previous JDBC technology we had to do things like connect the database, write the query, close the connection etc manually.

In addition to mapping tables, properties from relational databases to objects in object-oriented languages ​​and vice versa is also something very painful for programmers to do manually, yet. since if we want to switch to a new database for example from mysql to oracle, we will most likely have to repair all the queries etc.

ORM will do all of the above for us automatically, independent of the relational database, we will easily convert without having to change too much source code. We do not need to care what the underlying database is Mysql, Oracle, MysqlServer, PostgreSQL all. JPA architecture

JPA architecture

EntityManagerFactory

EntityManager factory class is responsible for creating and managing EntityManager instances.

EntityManager

An interface that provides APIs for application-database interaction.

Some of the basic functions of EntityManager are:

Persist: Create a new entity into the CSDl. Merge: Update an entity in the database. Remove: Delete an entity from the database.

EntityTransaction

A transaction is a set of operations in which they must either be successfully executed or all fail.

A database-transaction consists of a set of SQL statements that have been successfully executed or rolled-back ..

EntityTransaction has a 1-1 relationship with EntityManager. Any operation initiated through the EntityManager object is placed in a Transaction. EntityManager object that creates EntityTransaction.

Query

This is an interface, implemented by each JPA provider to obtain relational objects that meet query criteria. Source https://shareprogramming.net/dung-hibernate-da-lau-the-ban-co-biet-jpa-la-gi/

Share the news now

Source : Viblo