Best practice of Jakarta Persistence API in Java Library

Best practice of Jakarta Persistence API in Java Library Jakarta Persistence API (referred to as JPA) is an open source Java EE standard that is used to manage durable data solutions.It is an upgraded version of the Java Persistence API and part of the Java EE 8 specification, providing developers with a convenient way to operate the database. The following is the best practice and some example code using the Jakarta Persistence API framework: 1. Configure Persistence.xml file: Before using JPA, you need to add the Persistence.xml file to the project.This file is used to configure the data source connection and the physical class mapping information.The following is a simple persistence.xml example: <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.2" 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"> <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <class>com.example.MyEntity</class> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydatabase"/> <property name="javax.persistence.jdbc.user" value="username"/> <property name="javax.persistence.jdbc.password" value="password"/> </properties> </persistence-unit> </persistence> 2. Create a physical class: Using JPA, you can use annotations or XML mapping to describe the mapping relationship between the physical class and the database table.The following is a simple physical class example: import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @Entity public class MyEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; // getters and setters } 3. Perform basic CRUD operation: JPA provides a set of APIs to perform common CRUD (creation, reading, updating, deleting) operations.The following is a sample code using JPA to query: import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; public class Main { public static void main(String[] args) { EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("myPersistenceUnit"); EntityManager entityManager = entityManagerFactory.createEntityManager(); // Query the entity MyEntity entity = entityManager.find(MyEntity.class, 1L); // Update the entity entityManager.getTransaction().begin(); entity.setName("New Name"); entityManager.getTransaction().commit(); // Create entities MyEntity newEntity = new MyEntity(); newEntity.setName("New Entity"); entityManager.getTransaction().begin(); entityManager.persist(newEntity); entityManager.getTransaction().commit(); // Delete the entity entityManager.getTransaction().begin(); entityManager.remove(entity); entityManager.getTransaction().commit(); entityManager.close(); entityManagerFactory.close(); } } 4. Use JPQL for advanced query: JPA supports high -level inquiries using JPQL (Java Persistence Query Language).JPQL is similar to SQL, but uses physical classes and attributes rather than database tables and columns.The following is an example code using JPQL to query: import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; import jakarta.persistence.Query; import java.util.List; public class Main { public static void main(String[] args) { EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("myPersistenceUnit"); EntityManager entityManager = entityManagerFactory.createEntityManager(); Query query = entityManager.createQuery("SELECT e FROM MyEntity e WHERE e.name LIKE :name"); query.setParameter("name", "John%"); List<MyEntity> entities = query.getResultList(); for (MyEntity entity : entities) { System.out.println(entity.getName()); } entityManager.close(); entityManagerFactory.close(); } } The above are some of the best practices and example code using the Jakarta Persistence API framework.By mastering these techniques, you can use JPA in the Java library more efficiently to manage persistent data.