Use the Jakarta Persistence API for related queries and complex conditions (Performing Join and Complex Conditional Queries with Jakarta Persistence API)

When using the Jakarta Persistence API for associated queries and complex conditions inquiries, it can easily obtain the data required by defining the relationship between the physical class.This article will introduce how to use the Jakarta Persistence API for related inquiries and complex conditions, and provide relevant Java code examples. 1. Related query: In Jakarta Persistence API, you can use @Onetoone, @Onetomany, @Manytoone, and @Manytomany to define the relationship between physical classes.By defining these associations, the associated inquiries can be easily carried out. For example, assuming that there are two physical classes User and Address, one user can have multiple addresses, and one address belongs to only one user.They can define their relationship with the following code: @Entity public class User { @Id private Long id; @OneToMany(mappedBy = "user") private List<Address> addresses; // ... } @Entity public class Address { @Id private Long id; @ManyToOne @JoinColumn(name = "user_id") private User user; // ... } To perform related queries, you can use JPQL (Java Persistence Query Language) or Criteria API (Standard Query API). The example code using JPQL for associated query is as follows: String jpql = "SELECT u FROM User u JOIN u.addresses a WHERE a.city = :city"; TypedQuery<User> query = entityManager.createQuery(jpql, User.class); query.setparameter ("city", "Beijing"); List<User> users = query.getResultList(); The example code using the Criteria API for associated query is as follows: CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<User> query = builder.createQuery(User.class); Root<User> userRoot = query.from(User.class); Join<User, Address> addressJoin = userRoot.join("addresses"); query.select(userRoot).where(builder.equal(addressJoin.get("city"), "北京")); List<User> users = entityManager.createQuery(query).getResultList(); 2. Complex condition query: In addition to related query, the Jakarta Persistence API also provides rich condition query functions that can flexibly build complex query conditions. The example code using JPQL for complex condition query is as follows: String jpql = "SELECT u FROM User u WHERE u.age > :age AND u.city = :city"; TypedQuery<User> query = entityManager.createQuery(jpql, User.class); query.setParameter("age", 18); query.setparameter ("city", "Beijing"); List<User> users = query.getResultList(); The example code for using the Criteria API for complex conditions query is as follows: CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<User> query = builder.createQuery(User.class); Root<User> userRoot = query.from(User.class); query.select(userRoot).where( builder.greaterThan(userRoot.get("age"), 18), Builder.EQUAL (Userroot.get ("City"), "Beijing") ); List<User> users = entityManager.createQuery(query).getResultList(); By using the Jakarta Persistence API for related inquiries and complex conditions, you can easily obtain the required data.Whether using JPQL or Criteria API, it can meet various query needs and have good cross -database compatibility.