Home > Java > javaTutorial > Dive into the core concepts of Java JPA: entities, mappings, and queries

Dive into the core concepts of Java JPA: entities, mappings, and queries

王林
Release: 2024-02-19 23:30:38
forward
1164 people have browsed it

深入探索 Java JPA 的核心概念:实体、映射和查询

entity

php editor Apple will take you to deeply explore the core concepts of Java JPA in this article: entities, mapping and queries. As the complexity of modern applications increases, an understanding of JPA becomes critical. This article will help you understand the key concepts in JPA so that you can better apply them to actual projects.

@Entity
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

private int age;

// 省略 getter 和 setter 方法
}
Copy after login

Mapping

Mapping is the mechanism used by JPA to map properties in an entity class to fields in the database . JPA provides many types of mappings, including:

  • One-to-one mapping: An attribute in an entity class corresponds to a column in the database table.
  • One-to-many mapping: One attribute in an entity class corresponds to multiple columns in the database table.
  • Many-to-one mapping: Multiple attributes in an entity class correspond to one column in the database table.
  • Many-to-many mapping: Multiple attributes in an entity class correspond to multiple columns in the database table.

Mapping can also specify the attribute type, length, whether it is allowed to be empty, and other information.

@Entity
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(length = 50, nullable = false)
private String name;

@Column(nullable = false)
private int age;

// 省略 getter 和 setter 方法
}
Copy after login

Inquire

JPA provides a variety of query mechanisms, including:

  • JPQL (Java Persistence Query Language): A query language similar to sql that can be used to query entity objects.
  • Criteria api: An object-oriented query API that can be used to query entity objects.
  • Native query: Use native SQL to query the database.
// JPQL 查询
List<Person> persons = entityManager.createQuery("SELECT p FROM Person p WHERE p.age > 18", Person.class)
.getResultList();

// Criteria API 查询
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Person> cq = cb.createQuery(Person.class);
Root<Person> root = cq.from(Person.class);
cq.select(root).where(cb.gt(root.get("age"), 18));
List<Person> persons = entityManager.createQuery(cq).getResultList();

// 本机查询
List<Person> persons = entityManager.createNativeQuery("SELECT * FROM Person WHERE age > 18", Person.class)
.getResultList();
Copy after login

Summarize

JPA is a powerful persistence framework that provides unified access to relational databases. By using JPA, we can easily persist Java objects into the database and query entity objects using JPQL, Criteria API or native queries.

The above is the detailed content of Dive into the core concepts of Java JPA: entities, mappings, and queries. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template