How to use the Jakarta Persistence API framework in the Java library for data persistence

How to use the Jakarta Persistence API framework in the Java library for data persistence preface: In modern software development, data persistence is a very important part.We need a reliable way to store data into the database and be able to retrieve and update these data when needed.The Jakarta Persistence API (JPA) framework is a popular Java class library that provides a standardized way to achieve data persistence.In this article, we will discuss how to use the JPA framework in the Java class library for data persistence and how to use some common JPA features. 1. Introduce JPA dependencies <dependency> <groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>eclipselink</artifactId> <version>2.7.7</version> </dependency> 2. Configure database connection Next, we need to configure JPA to connect to the database.Create a file called "Persistence.xml" in the project and place it in SRC/main/Resources/Meta-INF directory.In this file, we can configure information such as database types, connecting URLs, user names and passwords.For example, for mysql, you can use the following 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="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/my_database"/> <property name="javax.persistence.jdbc.user" value="username"/> <property name="javax.persistence.jdbc.password" value="password"/> <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/> </properties> </persistence-unit> </persistence> Note that in the above example, the class name "Com.example.myEntity" represents your physical class.You need to replace it with your own physical class. 3. Create a physical class Next, we need to create a physical class corresponding to the database table.The physical class is used to represent the table in the database, as well as the column and association in the table.The following is a simple example: import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @Entity public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String firstName; private String lastName; private String email; // Getters and Setters } In the above example, we use JPA annotations to define the physical class.@ENTITY Note indicates that this class is a physical class, and@ID notes indicate that the ID field is the primary key. 4. Write data access objects (Data Access Objects) In order to perform persistence on the physical class, we need to write data access objects (DAO).The DAO layer is responsible for sending inquiries, inserting, updating, and deleting the database.The following is a simple example: import jakarta.persistence.EntityManager; import jakarta.persistence.Persistence; import jakarta.persistence.Query; public class CustomerDAO { private EntityManager entityManager; public CustomerDAO() { entityManager = Persistence.createEntityManagerFactory("myPersistenceUnit").createEntityManager(); } public Customer getCustomerById(Long id) { return entityManager.find(Customer.class, id); } public void saveCustomer(Customer customer) { entityManager.getTransaction().begin(); entityManager.persist(customer); entityManager.getTransaction().commit(); } public void updateCustomer(Customer customer) { entityManager.getTransaction().begin(); entityManager.merge(customer); entityManager.getTransaction().commit(); } public void deleteCustomer(Customer customer) { entityManager.getTransaction().begin(); entityManager.remove(customer); entityManager.getTransaction().commit(); } public List<Customer> getAllCustomers() { Query query = entityManager.createQuery("SELECT c FROM Customer c"); return query.getResultList(); } } In the above example, we use JPA's EntityManager object to perform interactive operations with the database.Among them, the getCustomerByid method is used to obtain customers based on ID, the SaveCustomer method is used to preserve customer objects, the UPDATECUSTOMER method is used to update customer objects, the DeleteCustomer method is used to delete customer objects. The getallCustomers method is used to obtain a list of all customer objects. 5. Use JPA for data persistence operation Now, we have configured JPA and wrote the physical and DAO class, and we can use JPA for data persistence operation.The following is a simple example: public class Main { public static void main(String[] args) { CustomerDAO customerDAO = new CustomerDAO(); // Create a new customer object Customer customer = new Customer(); customer.setFirstName("John"); customer.setLastName("Doe"); customer.setEmail("john.doe@example.com"); // Save the customer object to the database customerDAO.saveCustomer(customer); // Obtain customer objects through ID Customer savedCustomer = customerDAO.getCustomerById(customer.getId()); System.out.println (SavedCustomer.getFirstName ()); // Output: John // Update customer objects savedCustomer.setLastName("Smith"); customerDAO.updateCustomer(savedCustomer); // Delete customer objects customerDAO.deleteCustomer(savedCustomer); // Get the list of all customer objects List<Customer> customers = customerDAO.getAllCustomers(); System.out.println (Customers.size ()); // Output: 0 } } In the above example, we perform different database operations by calling the CustomerDao method.First, we create a new customer object and save it into the database.We then obtain customer objects through ID and update their surnames.Finally, we delete customer objects and obtain a list of all customer objects. in conclusion: In this article, we introduced how to use the Jakarta Persistence API framework in the Java class library for data persistence.We have discussed how to introduce JPA dependencies, configure database connections, create physical classes, write data access objects, and provide some example code to demonstrate how to use JPA for data persistent operations.JPA is a powerful framework that can simplify the process of data persistence and greatly improve development efficiency.I hope this article will help you and inspire you to use JPA in the Java class library for data persistent development.