Jakarta Persistence API framework in Java Library Guide

The Jakarta Persistence API (JPA) is one of the most widely used persistence frameworks in Java. It is a alternative implementation of standard Java Persistence API and focuses on simplifying the durable operation of data.This article will explain how to use the Jakarta Persistence API framework in the Java class library and provide Java code examples. 1. Introduce dependencies First, you need to add JPA dependencies to the project construction tool (such as Maven or Gradle).The following is an example of adding dependencies in the Maven project: <dependency> <groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> <version>2.2.3</version> </dependency> 2. Configure persistent unit Next, you need to configure a Persistence Unit, which defines the connection and mapping relationship between the application and the database.Create a `Persistence.xml` file in the project's` src/main/resources/meta-inf` directory, and press the following example configuration: <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd" version="2.2"> <persistence-unit name="examplePU" transaction-type="RESOURCE_LOCAL"> <properties> <!-Database connection configuration-> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/example_db"/> <property name="javax.persistence.jdbc.user" value="your_user"/> <property name="javax.persistence.jdbc.password" value="your_password"/> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <!-Sports mapping-> <property name="javax.persistence.schema-generation.database.action" value="create"/> <property name="javax.persistence.schema-generation.create-source" value="metadata"/> <property name="javax.persistence.sql-load-script-source" value="META-INF/load-script.sql"/> </properties> </persistence-unit> </persistence> In the above examples, you need to modify the `javax.persistence.jdbc.url`,` javax.persistence.jdbc.user` and `javax.persistence.jdbc.password` attributes according to your database configuration. 3. Create a physical class The core concept of JPA is the entity class, which represents the table in the database.You can define the mapping relationship between the physical class and the table by adding an annotation.For example, the following is a simple example: import jakarta.persistence.*; @Entity @Table(name = "employees") public class Employee { @Id @GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY) private Long id; @Column(name = "name") private String name; // omit the definition of other attributes and methods ... } In the above examples, the annotation of `@Entity` indicates that this is a physical class,`@table` specifies the corresponding table name, `@id` indicating` id` field is the main key, `` `` `` `` `` `` generatedvalue ”annotations specifyThe strategy of generating primary keys, `@column` annotations define the columns mapped to the database table. 4. Write data access layer code Next, we can operate the data through the EntityManager API provided by JPA.For example, the following is a simple example: import jakarta.persistence.*; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; public class EmployeeDAO { private EntityManagerFactory emf; public EmployeeDAO() { emf = Persistence.createEntityManagerFactory("examplePU"); } public Employee find(Long id) { EntityManager em = emf.createEntityManager(); return em.find(Employee.class, id); } public void save(Employee employee) { EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); em.persist(employee); tx.commit(); } // omit other data access methods ... } In the above examples, the factory used to create a factory instance by creating factory instances by creating a factory used to create a factory used to create a factory used to create a factory used to create a factory used to create a factory used to create a factory used to create a factory used to create a factory used to create a factory that creates the `EntityManager`.Then, the data is added, deleted, deletion and change operation through an instance of the `EntityManager`. 5. Use persistent operation Finally, you can use a data access object (DAO) class in your application to perform durable operations.For example: public class Main { public static void main(String[] args) { EmployeeDAO employeeDAO = new EmployeeDAO(); // Create a new employee Employee newEmployee = new Employee(); newEmployee.setName("John Doe"); employeeDAO.save(newEmployee); // Find employees Long employeeId = 1L; Employee employee = employeeDAO.find(employeeId); System.out.println("Employee name: " + employee.getName()); } } In the above examples, we first created a new `Employee" object and saved it into the database, and then find employees and print their names through the specified `ID`. Through the above steps, you can successfully use the Jakarta Persistence API framework in the Java class library for data persistence operation.Hope this article will help you!