Jakarta Persistence API in the Java Class Library

Jakarta Persistence API (referred to as JPA) is a set of specifications used to mappore between objects and relational databases in the Java library.It provides a simple and easy -to -use way, allowing developers to access and manage database data through object -oriented methods.In this article, we will introduce the fast entry guide of JPA and provide some Java code examples. ## Preparation Before starting development, we need to ensure that the following environment has been installed: -JAVA Development Environment (JDK) -On database, such as MySQL, Oracle, etc. -Hey Java Integrated Development Environment (IDE), such as Eclipse or Intellij IDEA In addition, we need to add JPA dependencies.In the Maven project, it can be achieved by adding the following dependencies to the POM.XML file: <dependency> <groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> <version>2.2.3</version> </dependency> ## Create a physical class In JPA, the physical class is used to mappore database tables.We need to create a Java class and use some annotations to specify the mapping rules of the entity.The following is an example: import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private int age; // omit the constructive method, getter and setter } In the above example, we used the `@Entity` annotation to mark the class as a physical class.`@ID` Note specifies the unique identifier of the entity, where we use automatically generated values.We also define some attributes (such as names and age), which can add more attributes according to actual needs. ## Configuration persistence unit JPA needs a persistent unit to manage the mapping relationship between the physical class and the database.We need to define durable units in the configuration file of the project.In this case, we will use a file called `Persistence.xml` to define it.Create the `Persistence.xml` file under the` src/main/resources/meta -inf/`folder, and add the following: <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jakarta.ee/xml/ns/persistence http://jakarta.ee/xml/ns/persistence/persistence_2_2.xsd" version="2.2"> <persistence-unit name="example"> <class>com.example.User</class> <!-Add database connection information-> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydb"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.password" value="password"/> <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/> </properties> </persistence-unit> </persistence> In the above example, we define a durable unit called `Example` and specify the position of the physical class.At the same time, we also need to add database connection information, including URL, user name, password and driver. ## Use JPA to perform database operations We are ready to start using JPA for database operations.Here are some basic examples: ### Create a physical manager factory import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; public class Main { public static void main(String[] args) { // Create a physical manager factory EntityManagerFactory emf = Persistence.createEntityManagerFactory("example"); // Create a physical manager EntityManager em = emf.createEntityManager(); // Perform the database operation // Turn off the physical manager and factory em.close(); emf.close(); } } In the above example, we use the `Persistence.CreateEntityManagerFactory (" Example ") method to create a physical manager factory called` Example`.Then, we used the factory to create a physical manager `EntityManager`, we will use it to perform specific database operations.Finally, we closed the physical manager and factory after completing the operation. ### adding data em.getTransaction().begin(); User user = new User(); user.setName("John"); user.setAge(25); em.persist(user); em.getTransaction().commit(); In the above example, we use the `Persist ()" method to add a new `User` object to the database.Before calling the `Commit ()` method, we need to call the `Begin () method in the context of the transaction to start a new transaction. ### Query data Query query = em.createQuery("SELECT u FROM User u WHERE u.age > :age"); query.setParameter("age", 20); List<User> users = query.getResultList(); for (User user : users) { System.out.println(user.getName()); } In the above example, we use JPQL (Java Persistence Query Language) to query the language to retrieve users who are older than 20 years old in the database. ### update data em.getTransaction().begin(); User user = em.find(User.class, 1L); user.setAge(30); em.getTransaction().commit(); In the above example, we use the `Find ()` method to retrieve the specific `User` object from the database according to the given unique identifier, and update the age of the user by setting the` setage () `method. ### delete data em.getTransaction().begin(); User user = em.find(User.class, 1L); em.remove(user); em.getTransaction().commit(); In the above example, we use the `Remove ()" method to delete a specific `User` object from the database according to the given unique identifier. ## Summarize In this article, we introduced how to quickly get started in the Java class library Jakarta Persistence API (JPA).We have learned how to create a physical class, configure durable units, and use JPA for common database operations.I hope this article is helpful to you when using JPA for development!