【Spring Boot】JPA — Getting started with JPA

Introduction to JPA

1. What is JPA

JPA is the Java persistence specification officially proposed by Sun. It provides Java developers with an object/association mapping tool to manage relational data in Java applications. It describes the mapping relationship between "object-relational tables" through annotations or XML. , and persist entity objects into the database, greatly simplifying existing persistence development work and integrating ORM technology.

JPA is not a new ORM framework. Its emergence is mainly to simplify existing persistence development work and integrate ORM technology, ending the current situation in which ORM frameworks such as Hibernate, TopLink, and JDO operate independently. It is a set of specifications rather than a product. Products such as Hibernate and TopLink implement JPA specifications. We can call them JPA implementation products. The relationship is as shown in the figure.

Insert image description here

2. What can JAP do?

JPA is developed on the basis of fully absorbing existing ORM frameworks such as Hibernate, TopLink, JDO, etc., and has the advantages of ease of use and strong scalability. In general, JPA includes the following three technologies:

1) ORM mapping metadata: JPA supports two forms of metadata, XML and JDK 5.0 annotations. Metadata describes the mapping relationship between objects and tables, and the framework persists entity objects to database tables accordingly.

2) Java persistence API: used to operate entity objects and perform add, delete, modify, and query (CRUD) operations. The framework completes everything for us in the background, freeing developers from cumbersome JDBC and SQL codes.

3) Query language (JPQL): This is an important aspect of persistence operations. Data is queried through object-oriented rather than database-oriented query language to avoid tight coupling of the SQL statements of the program.

JPA frees us to operate the database, so that developers no longer need to care about the table structure of the database. When changes need to be made, they only need to modify the attributes of the corresponding entity classes. In the microservice architecture, services are split into more and more details. The microservices only need to care about their own business, and we do not need to pay too much attention to the database. Therefore, it is more recommended to use JPA technology in microservice architecture.

Spring Data support for JPA

Spring Data JPA is a set of JPA application framework encapsulated by Spring based on the ORM framework and JPA specifications.

For quite a long time, it has been troublesome to implement data access in applications, and a large amount of SQL code must be written to perform data query, update and other operations. Using Spring Data JPA developers only need to write the repository interface and custom finder methods , and other SQL statements are automatically provided by Spring, freeing developers from cumbersome JDBC and SQL codes.

Although mainstream ORM frameworks have implemented the JPA specification, switching between different ORM frameworks requires writing their own code. Using Spring Data Jpa can facilitate developers to switch between different ORM frameworks without changing any code . . This facilitates developers to use JPA technology in Spring Boot projects. The specific relationship is as shown in the figure.

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_45627039/article/details/132715602