JPA primary key strategy

      Before talking about the primary key strategy, let's briefly introduce JPA.

      JPA is a java specification, not a framework, but an ORM specification. JPA can support multiple ORM frameworks, such as: hibernate, OpenJPA, TopLink, EclipseJPA, etc. JPA only provides a unified specification interface, and the final implementation is realized by various database vendors. The purpose of this design is to solve the cost of switching ORM frameworks, database connections, etc. in the project.

 

       When we develop with JPA, we often add the @GeneratedValue annotation to the ID of the entity class to generate the primary key of the table.

E.g:

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

private String id;

 

Among them: strategy is used for the life primary key generation strategy, and JPA provides four primary key generation strategies.

 

1.GenerationType.IDENTITY

        The id is self-increasing. When the database adds a new row of data, it automatically assigns a value to the id. Similar to when MySql creates a new table, the column is designed as auto_increment. Common databases basically support this strategy.

 

2.GenerationType.AUTO

         The primary key generation strategy is automatically selected from the other three strategies, and the primary key generation strategy is handed over to JPA for processing. JPA will select the appropriate strategy according to different databases.

 

3.GenerationType.SEQUENCE

     The primary key is generated using a sequence, for example:

@Id

@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "testSeq")

@SequenceGenerator(initialValue = 1,name = "testSeq", sequenceName = "TEST_SEQUENCE")

private String id;

 

The above statement is equivalent to creating a sequence, create sequence TEST_SEQUENCE;

If the name of the sequence generator is not specified, the default sequence generator provided by the manufacturer is used. For example, the default sequence name provided by Hibernate is hibernate_sequence.

 

4.GenerationType.TABLE

        This strategy utilizes a table in the database to generate the primary key, which does not depend on the specific implementation of the database, and can switch between different databases.

If you do not specify a table generator, the JPA vendor will use the default table. For example, Hibernate will use the table hibernate_sequence by default on the Oracle database.

example:

@Id

@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "testSeq")

@TableGenerator(name ="PK_SEQ",table="SEQUENCE_TABLE",pkColumnName="SEQUENCE_NAME", valueColumnName  = "SEQUENCE_COUNT") 

private String id;

The name attribute indicates the name of the table generator, table indicates which table is used to store the generated primary key, pkColumnName is used to specify which column in the generator table to store the primary key, and valueColumnName specifies that the column in the generator table is used to store the last generated one The value of the primary key.

       

 

    

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326825900&siteId=291194637