JPA primary key generation strategy introduction

JPA primary key generation strategy introduction

Before accessing the JPA framework, it is necessary to understand the JPA primary key generation strategy

1. Dependence

<dependency>
	<groupId>org.eclipse.persistence</groupId>
	<artifactId>javax.persistence</artifactId>
	<version>2.1.0</version>
</dependency>

2. GeneratedValue annotation

GeneratedValue is a very important annotation in the JPA primary key generation strategy. It provides the specification of the primary key value generation strategy, which can be applied together with the Id annotation to the primary key attribute or field of the entity or mapping superclass; it only supports simple primary keys, and derived primary keys do not support the use.

@Target({
    
    METHOD, FIELD})
@Retention(RUNTIME)
public @interface GeneratedValue {
    
    
    GenerationType strategy() default AUTO;
    String generator() default "";
}

As shown in the code above, the GeneratedValue annotation has two member variables, strategy and generator .

2.1 Primary key generation strategy [strategy]

The persistence provider must use a primary key generation strategy to generate the primary key of the annotated entity. This is an option, the default is GenerationType.AUTO ; the value of
strategy is the primary key generation strategy enumeration Type GenerationType , which contains 4 enumeration values: [ TABLE , SEQUENCE , IDENTITY , AUTO ].

2.2 Primary key generator [generator]

generator specifies the name of the primary key generator used, with SequenceGenerator or TableGenerator annotations. It provides ID generators for persistence providers . This is also an option and can be empty by default.

3. GenerationType

GenerationType defines the type of primary key generation strategy. Include as follows:

3.1 GenerationType.TABLE

TABLE indicates that the persistence provider must use the underlying database table to assign a primary key to the entity to ensure uniqueness. Its advantage is that it does not depend on the implementation of the specific database, and the code portability is high. However, due to the characteristics of certain databases (such as primary key self-growth, sequence, etc.), it cannot be used, and it is not recommended to use it first. It can be used as a compromise solution. .

The specific usage is as follows:

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "FLEA_LOGIN_LOG_GENERATOR")
    @TableGenerator(
        // 唯一的生成器名称,可以由一个或多个类引用以作为id值的生成器。
        name = "FLEA_LOGIN_LOG_GENERATOR",
        // 【可选】存储生成的ID值的表的名称,默认为持久化提供程序选择的名称
        table = "flea_id_generator",
        // 【可选】生成器表所属的数据库目录
        catalog = "",
        // 【可选】生成器表所属的数据库结构
        schema = "",
        // 【可选】表中主键列的名称,默认为持久化提供程序选择的名称
        pkColumnName = "id_generator_key",
        // 【可选】存储最后生成的主键值的列的名称,默认为持久化提供程序选择的名称
        valueColumnName = "id_generator_value",
        // 【可选】ID生成器表中的主键值模板,用于将该生成值集与其他可能存储在表中的值区分开
        // 默认为持久化提供程序选择的值,用以存储在生成器表的主键列中
        pkColumnValue = "pk_flea_login_log_(CREATE_DATE)",
        // 【可选】用于初始化存储最后生成的值的列的初始值,默认值为 0
        initialValue = 0,
        // 【可选】从ID生成器表中分配ID号时增加的数量, 默认值为 50
        allocationSize = 1,
        // 【可选】将在表上放置的其他唯一约束。仅当表生成有效时才使用它们。
        // 除了主键约束之外,还应用了这些约束。默认为无其他约束
        uniqueConstraints = {
    
    },
        // 【可选】表的索引。仅当表生成有效时才使用它们。
        // 请注意,不必为主键指定索引,因为主键索引将自动创建。
        indexes = {
    
    }
    )
    @Column(name = "login_log_id", unique = true, nullable = false)
    private Long loginLogId; // 登录日志编号

3.2 GenerationType.SEQUENCE

SEQUENCE indicates that the persistence provider must use the database sequence to assign a primary key to the entity. The policy applies only to the portion of the support sequence database systems, such as the Oracle .

The specific usage is as follows:

	@Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="PARA_DETAIL_SEQ")
    @SequenceGenerator(
    	// 唯一的生成器名称,可以由一个或多个类引用以作为id值的生成器。
    	name="PARA_DETAIL_SEQ", 
    	// 【可选】主键值对应的数据库序列对象的名称。默认为提供商选择的值。
    	sequenceName="PARA_ID_SEQ",
        // 【可选】生成器表所属的数据库目录
        catalog = "",
        // 【可选】生成器表所属的数据库结构
        schema = "",
        // 【可选】用于初始化存储最后生成的值的列的初始值,默认值为 0
        initialValue = 0,
        // 【可选】从ID生成器表中分配ID号时增加的数量, 默认值为 50
        allocationSize = 1
    )
    @Column(name = "para_id", unique = true, nullable = false)
    public Long getParaId() {
    
     return paraId; }

3.3 GenerationType.IDENTITY

IDENTITY indicates that the persistence provider must use the database identity column to assign a primary key to the entity. The policy applies only to support the primary key from the growth of the database systems such as MySQL.

The specific usage is as follows:

	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "para_id", unique = true, nullable = false)
    private Long paraId;        // 参数编号

3.4 GenerationType.AUTO

AUTO indicates that the persistence provider should choose the appropriate strategy for a particular database. The generation strategy may expect the existence of a database resource, or it may try to create a database resource. If the supplier does not support schema generation or cannot create schema resources at runtime, the supplier may provide documentation on how to create such resources.

	@Id
    @GeneratedValue
    @Column(name = "para_id", unique = true, nullable = false)
    private Long paraId;        // 参数编号

4. Comparison of various databases

TABLE SEQUENCE IDENTITY AUTO
MySQL ×
Oracle ×
PostgreSQL
SQL Server
DB2

Guess you like

Origin blog.csdn.net/u012855229/article/details/114642223