Understand the basic knowledge of the Jakarta Persistence API framework in the Java class library

Understand the basic knowledge of the Jakarta Persistence API framework in the Java class library In Java development, persistence is an important concept.Persistently refers to saving data to persistent storage media (such as databases) for long -term preservation and use.Jakarta Persistence API (JPA) is an open source framework in the Java class library that is used to implement the mapping between Java objects and databases. It provides a simplified and standardized method for database persistence operations. JPA is the ORM (object relationship mapping) specification based on the Java standard, which aims to simplify the process of developers using databases in applications.ORM is a technology that maps the surface structure in the relational database to the Java object, so that developers can use object -oriented methods for database operations without having to care about the details of the underlying database. Here are a simple example of using JPA: First of all, we need to add the JPA framework related library to the project's dependence.It can be achieved by adding the following dependencies in the configuration file of the project construction tool (such as Maven): <dependencies> <dependency> <groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.32.Final</version> </dependency> </dependencies> Then, we can define a simple Java class as a physical class and use JPA for annotations.For example, we create a physical class called "User": import jakarta.persistence.*; @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "username") private String username; @Column(name = "password") private String password; // Getter and Setter methods } In the above code, we use the annotations provided by JPA to define the mapping relationship between the attributes of the physical class and the database table.`@Entity` Note indicates that this is a physical class,` `` ``@@column` annotations are used to specify the mapping of the attributes and the database table. Next, we can use the API provided by the JPA framework for database operations.Before using JPA, we need to create a Persistence Unit first.The persistent unit is a set of sets of entity class, defining the configuration information of database connections and JPA.We can define the configuration of the persistent unit in a configuration file called "Persistence.xml": <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.2"> <persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL"> <class>com.example.User</class> <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"/> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.show_sql" value="false"/> </properties> </persistence-unit> </persistence> In the above configuration file, we designated the name of the `Persistence-Unit` as" MyPU ", and configured the database connection information and some other JPA and Hibernate-related configurations. Now, we can use the JPA API for database operations.The following is a simple sample code: import jakarta.persistence.*; public class Main { public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPU"); EntityManager em = emf.createEntityManager(); User user = new User(); user.setUsername("john"); user.setPassword("password"); EntityTransaction tx = em.getTransaction(); tx.begin(); em.persist(user); tx.commit(); em.close(); emf.close(); } } In the above code, we first created a `EntityManagerFactory` method through the method of` Persistence.CreateEntityManagerFactory`.We then use this instance to create an instance of `EndityManager` for database operations.Next, we create a `User` object and persist in the database. Finally, we use the `EntityManagertransAction` to open a transaction and submit it.In transactions, we call the `em.persist` method to save the` user` object into the database. Summarize: Through the above example, we understand the basic knowledge of the JPA framework.JPA provides us with a simplified and standardized way to perform database persistence operations, so that developers can more easily use the Java objects to interact with databases.By using JPA, we can better follow the principles of object -oriented programming, focusing on business logic without having to care about the details of the underlying database.