Simple and simple, learn JPA

JPA (Java Persistence API) is an API in Java EE for managing the mapping between Java objects and relational databases. It is an ORM (object-relational mapping) framework that allows developers to operate databases in an object-oriented manner without having to care about the details of SQL statements. In this article, we'll learn about JPA in a nutshell and demonstrate its usage with Java code examples.

Basic concepts of JPA

Before using JPA, we need to understand some basic concepts:

  • Entity Class: In JPA, an entity class refers to a Java class that corresponds to a database table. The attributes in the entity class have a one-to-one correspondence with the columns in the database table.
  • Entity Manager (Entity Manager): The entity manager is a core component in JPA, which is used to manage the life cycle of entity classes, including operations such as creation, update, and deletion.
  • Entity Transaction: Entity Transaction is a concept in JPA, which is used to ensure the consistency of entity classes in the database.

Usage of JPA

Below we demonstrate the usage of JPA through a simple example. Suppose we have a User entity class corresponding to the user table in the database. The User class is defined as follows:

@Entity
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private Integer age;

    // 省略 getter 和 setter 方法
}

In the above code, we use JPA annotations to define entity classes. The @Entity annotation indicates that this is an entity class, and the @Table annotation indicates that this class corresponds to the user table in the database. The @Id, @GeneratedValue, and @Column annotations respectively indicate that the property is the primary key, automatically generated, and corresponds to the columns in the database table.

Next, we need to create an entity manager. In JPA, we can use EntityManagerFactory to create EntityManager. EntityManagerFactory is thread-safe and can be used throughout the application.

EntityManagerFactory factory = Persistence.createEntityManagerFactory("my-persistence-unit");
EntityManager entityManager = factory.createEntityManager();

In the above code, we use the createEntityManagerFactory method of the Persistence class to create the EntityManagerFactory. This method needs to pass in one parameter, which is the name of the persistence-unit. In the persistence.xml file, we need to define the persistence-unit and specify the data source and other information.

Next, we can use EntityManager for CRUD operations. Here is some sample code:

// 创建一个新的用户
User user = new User();
user.setName("Tom");
user.setAge(20);
entityManager.getTransaction().begin();
entityManager.persist(user);
entityManager.getTransaction().commit();

// 查询所有用户
TypedQuery<User> query = entityManager.createQuery("SELECT u FROM User u", User.class);
List<User> users = query.getResultList();

// 更新用户信息
entityManager.getTransaction().begin();
User userToUpdate = entityManager.find(User.class, 1L);
userToUpdate.setName("Jerry");
userToUpdate.setAge(25);
entityManager.getTransaction().commit();

// 删除用户
entityManager.getTransaction().begin();
User userToDelete = entityManager.find(User.class, 1L);
entityManager.remove(userToDelete);
entityManager.getTransaction().commit();

In the above code, we use EntityManager's persist, createQuery, find, remove and other methods to perform CRUD operations. Before proceeding, we need to obtain an entity transaction by calling the getTransaction method.

Summarize

JPA is a powerful ORM framework that allows developers to manipulate databases in an object-oriented manner. In this article, we introduced the basic concepts of JPA and demonstrated its usage through Java code examples. I hope this article can help readers better understand the use of JPA.

Guess you like

Origin blog.csdn.net/m0_49151953/article/details/130252805
Recommended