Java persistence API is very useful. Data is the most important part of our application and it is the heart of our application. JPA is almost completely configured. It is a ORM (Object Relational Mapping) tool. ORM is the middle layer which manages communications to database with the application.
Java is object oriented application, so there are objects in our application. Whenever we make any changes in object, that needs to be stored in database using SQL (Structured Query Language). But ORM tool automatically maps the objects to the relational database. So we don't have to write SQL queries ourselves.
It will create objects from database rows and give it to java application.
While saving java application gives java objects to ORM and orm takes values from the objects and save to the database.
For example our table user in the database has 4 fields
user (id, name, city, profile)
Now to tell ORM how it needs to convert table to object, we create a class
Class User {
int id
String name
String city
String profile
}
Now the ORM converts this class to table. and maps each variable in class to table column name.
JPA
J - Java
P - Persistence
A - API
JPA is used to persist (make permanent) any data that has been handled through the application. JPA is only a specification given by oracle and we can use any implementation of JPA. Hibernate is one of the most popular ORM tool, which is implementation of JPA. other implementations include Eclipse link, Open JPA etc.
JPA provides two important interfaces
Provides Entity Manager object
Provides many methods to create, read, update, delete records
Spring boot makes easy to perform operation with JPA. For that, we need to add a new dependency
spring-boot-starter-data-jpa
When you add this dependency, all the settings to run spring boot jpa gets autoconfigured. In spring framework you need to specify configuration manually. Now we need User object, and create UserRepository
If we want to handle object User, First extend UserRepository to CRUDRepository. This CRUDRepository interface in Spring boot provides all functionality to manage data. We inject this UserRepository anywhere we require, then all the implementation will come with this. Insted of CRUDRepository we can also extend JPARepository, which is child of CRUDRepository. JPARepository provides extra functionalities like pagination, flush database etc.
If we want to do any operation,
- Create entity of that operation, e.g. User, Product, employee, client etc.
- Create repository out of that entity e.g. UserRepository, ClientRepository, ProductRepository etc.
- Now extend that repository to CRUDRepository or JPARepository
- Now we get basic functionality of CRUD operation. No need to do any other coding.
- If we need some extra features, which is not available in CRUDRepository or JPARepository, we can customize later.
Do it yourself
Create a project from
start.spring.io
Select Java Maven Project with appropriate names.
Add dependencies
MySQL Driver
Spring Data JPA
If you want to make web based app, use Spring web too
No comments:
Post a Comment