快速开始
创建一个空的 Maven 项目,添加如下依赖:
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>7.1.2.Final</version>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.4.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.42</version>
</dependency>
添加 JPA 的配置文件src/main/resources/META-INF/persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version="2.0">
<persistence-unit name="jpa-demo" transaction-type="RESOURCE_LOCAL">
<!-- 指定ORM框架实现了 PersistenceProvider 接口的实现类 -->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>cn.icexmoon.entity.Customer</class>
<properties>
<!-- 数据库连接设置 -->
<property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa" />
<property name="jakarta.persistence.jdbc.user" value="root" />
<property name="jakarta.persistence.jdbc.password" value="mysql" />
<!-- ORM 框架设置 -->
<property name="jakarta.persistence.schema-generation.database.action" value="update"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.highlight.sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
JPA 配置文件的格式和内容与 JPA 以及具体的 JPA 实现版本有关,不同的版本可能会有不同的差异,这里是 Hibernate 7.x 的配置内容。
这里的<class>cn.icexmoon.entity.Customer</class>是之后定义的实体类,如果没有在这里添加,就会报错表示实体类不属于这个persistence-unit。
根据需要,JPA 会在 SessionFactory 启动以及关闭时自动执行一些 DDL 语句对数据库进行初始化,具体的策略由配置项 jakarta.persistence.schema-generation.database.action决定,具体有以下选项:
-
drop-and-create,先删除数据库,然后创建表、序列和索引,最后填充初始数据。
-
create,创建表、序列和约束,填充初始数据。
-
create-drop,在启动时先删除再创建数据库,在关闭时删除数据库。
-
drop,关闭时删除数据库。
-
validate,仅检查数据库是否与 JPA 中的定义匹配,不进行任何更改。
-
update,仅在数据库与 JPA 中的定义不匹配时执行 DDL 语句更新数据库。
-
populate,仅填充初始数据,不执行任何 DDL 语句。
添加实体类src/main/java/cn/icexmoon/entity/Customer.java:
(name = "customer")
public class Customer {
(strategy = GenerationType.IDENTITY)
private Long id;
(name = "last_name", length = 10, nullable = false)
private String lastName;
private Integer age;
}
主要涉及以下注解:
-
@Entity,定义 JPA 实体
-
@Table,如果实体名称与表名不同,需要使用 @Table 标记对应的数据库表
-
@Id,标记用于表主键的字段
-
@GeneratedValue,主键生成策略,分为以下几种:
-
AUTO:JPA 自动选择合适策略,默认选项
-
IDENTITY:采用数据库自增方式生成主键,Oracle 不支持
-
SEQUENCE:通过序列产生主键,通过 @SequenceGenerator 注解指定序列名,MySQL 不支持
-
TABLE:通过表产生主键,框架借由表模拟序列产生主键,使用该策略可以使应用更易于数据库迁移
-
-
@Column,定义列的相关设置,比如列名、长度、是否为空、是否唯一约束等。
添加入口类src/main/java/cn/icexmoon/Main.java:
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jpa-demo");
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
Customer customer = new Customer();
customer.setLastName("icexmoon");
customer.setAge(18);
entityManager.persist(customer);
transaction.commit();
entityManager.close();
entityManagerFactory.close();
}
}
注解
@Transient
JPA 实体中默认所有具备 Getter 和 Setter 的属性都是表字段的映射,如果有属性不是表字段的映射,就需要使用@Transient注解标记:
(name = "customer")
public class Customer {
(strategy = GenerationType.IDENTITY)
private Long id;
(name = "last_name", length = 10, nullable = false)
private String lastName;
private Integer age;
private Integer score;
}
这里的score属性就不会作为表字段保存和读取。
@Temporal
如果实体类的字段是Date类型:
(name = "customer")
public class Customer {
// ...
private Date birth;
private Date createTime;
}
JPA 自动生成的数据库字段都会是datetime类型。在这里,birth字段使用date类型的数据库字段存储更为合适。
可以使用@Temporal注解显式为 Date 类型字段指定数据库字段的类型:
(name = "customer")
public class Customer {
// ...
(TemporalType.DATE)
private Date birth;
private Date createTime;
}
此时数据库中的birth字段会使用date类型而非datetime类型。
在高版本的 JPA 中
@Temporal虽然依然有效,但已经被标记为废弃,高版本的 JPA 中建议直接使用LocalDate和LocalDateTime定义时间类型的实体字段。
ID 属性
实体类都必须有用@Id标记的 ID 属性。
或者从父类(根实体)继承。
生成策略
需要为 ID 属性指定生成策略,Hibernate 支持以下 ID 属性生成策略:
| Strategy 策略 | Java type Java 类型 | Implementation 实现 |
|---|---|---|
GenerationType.UUID |
UUID 或 String |
一个 Java UUID |
GenerationType.IDENTITY |
Long 或 Integer |
一个标识符或自增列 |
GenerationType.SEQUENCE |
Long 或 Integer |
数据库序列 |
GenerationType.TABLE |
Long 或 Integer |
一个数据库表 |
GenerationType.AUTO |
Long 或 Integer |
根据数据库的标识符类型和功能选择 SEQUENCE 、 TABLE 或 UUID |
示例:
public class Person {
(strategy = GenerationType.UUID)
private UUID id;
// ...
}
此时数据库表主键将使用 Java 生成的 UUID,数据库存储的类型是binary(16)。
@Entity
@Data
@NoArgsConstructor
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// ...
}
此时数据库表主键将使用数据库表的自增序列,即id bigint auto_increment。
@Entity
@Data
@NoArgsConstructor
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
// ...
}
此时将使用额外的表(person_seq)用于存储和生成自增序列。
@Entity
@Data
@NoArgsConstructor
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
// ...
}
使用额外的表(hibernate_sequences) 生成主键。
@Entity
@Data
@NoArgsConstructor
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// ...
}
由 Hiberrnate 自动决定使用何种生成策略,对于 MySQL 8,使用的是 GenerationType.TABLE。
对于不同的数据库,同一个主键生成策略可能有不同的实现方式,上面讨论的都是基于 MySQL 8 的主键策略实现。
使用表生成主键
使用表生成主键的策略时,默认使用统一的表hibernate_sequences,可以自定义使用的表以及自增序列查找方式。
创建用于生成主键的表:
create table id_generator
(
id bigint unsigned auto_increment
primary key,
table_name varchar(255) not null,
value bigint null
);
修改实体类,使用表生成主键:
@Entity
@Table(name = "customer")
@Data
public class Customer {
@Id
@TableGenerator(
name = "customer_id_generator",
table = "id_generator",
pkColumnName = "table_name",
valueColumnName = "value",
pkColumnValue = "customer",
allocationSize = 1)
@GeneratedValue(strategy = GenerationType.TABLE, generator = "customer_id_generator")
private Long id;
// ...
}
@TableGenerator的table属性说明使用id_generator表生成主键,生成主键的数据通过pkColumnName、pkColumnValue、valueColumnName三个属性进行定位,即pkColumnName的值所在的列的值为pkColumnValue的值,游标存放在valueColumnName值对应的列。allocationSize属性决定了下一个生成的主键值的步进。
当@TableGenerator与@GeneratedValue一起使用时,@GeneratedValue的属性是可以省略的:
@Id
@TableGenerator(
name = "customer_id_generator",
table = "id_generator",
pkColumnName = "table_name",
valueColumnName = "value",
pkColumnValue = "customer",
allocationSize = 1)
@GeneratedValue
private Long id;
联合主键
并不是所有表都有唯一列作为主键,有时候有多个列(一般称作复合主键或联合主键),对应到 JPA 实体,需要使用多个属性作为联合主键。
比如有三张表:学生表、课程表、学生选课表。
对应的实体:
@Data
@Entity
@NoArgsConstructor
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(length = 10, nullable = false, unique = true)
private String name;
@Column(columnDefinition = "tinyint unsigned")
private Integer age;
public Student(String name) {
this.name = name;
}
}
@Entity
@Data
@NoArgsConstructor
public class ClassItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(length = 10, nullable = false)
private String name;
@Column(length = 100)
private String description;
public ClassItem(String name) {
this.name = name;
}
}
@Entity
@Data
@IdClass(StudentClass.StudentClassId.class)
@NoArgsConstructor
public class StudentClass {
public record StudentClassId(Long classId, Long studentId) {
}
@Id
private Long classId;
@Id
private Long studentId;
@Temporal(TemporalType.DATE)
private Date beginDate;
@Temporal(TemporalType.DATE)
private Date endDate;
public StudentClass(StudentClassId id, Date beginDate, Date endDate) {
this.classId = id.classId;
this.studentId = id.studentId;
this.beginDate = beginDate;
this.endDate = endDate;
}
}
学生选课表中有两个属性作为联合主键,它们都有@Id注解。为了能使用find等 API 通过主键进行检索,这里还需要额外定义一个包含了这两个属性的类StudentClassId,并使用@IdClass注解进行标记。
利用联合主键添加数据:
Student student = new Student("李四");
entityManager.persist(student);
ClassItem classItem = new ClassItem("java");
entityManager.persist(classItem);
StudentClass studentClass = new StudentClass(
new StudentClass.StudentClassId(classItem.getId(), student.getId()),
DateUtil.parseDate("2025-1-25"), DateUtil.parseDate("2025-10-25"));
entityManager.persist(studentClass);
查找数据:
StudentClass studentClass = entityManager.find(StudentClass.class, new StudentClass.StudentClassId(1L, 1L));
System.out.println(studentClass);
虽然像上面那样定义联合主键是可行的,但并不推荐,Hibernate 官方推荐的方式是:
@Entity
@Data
@NoArgsConstructor
public class StudentClass {
public record StudentClassId(Long classId, Long studentId) {
}
@EmbeddedId
private StudentClassId id;
@Temporal(TemporalType.DATE)
private Date beginDate;
@Temporal(TemporalType.DATE)
private Date endDate;
public StudentClass(StudentClassId id, Date beginDate, Date endDate) {
this.beginDate = beginDate;
this.endDate = endDate;
}
}
这里使用@EmbeddedId定义了一个联合主键,不需要使用@IdClass以及多个@Id注解。
自然键
实体除了可以用 ID 属性唯一定位,还可能通过其他属性组合唯一定位,这些属性被称作自然键(Nacture Key)。
@Entity
@Data
@NoArgsConstructor
public class Car {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String brand;
@NaturalId
private String engineCode;
public Car(String brand, String engineCode) {
this.brand = brand;
this.engineCode = engineCode;
}
}
上面的示例中,除了可以用 ID 唯一确定一辆车,还可以通过发动机序列号唯一确定一辆车。
为一个(或多个)属性添加@NaturalId注解后,Hibernate 会为这些属性对应的列创建单列(或联合)唯一索引。
本文的完整示例代码可以从获取。

文章评论