JPA ID generation strategy (turn---)

Respect originality: http://tendyming.iteye.com/blog/2024985

 

JPA ID generation strategy

@Table Table is used to define the name, catalog, schema and other attributes of the entity main table. 
Property description: 

  • name: table name
  • catalog: corresponds to the catalog in the relational database
  • schema: corresponds to the schema in the relational database
  • UniqueConstraints: Defines an array of UniqueConstraints, specifying the columns that need to create unique constraints. UniqueConstraints are defined in the Table or SecondaryTable metadata, and are used to specify the columns that need to be unique constraints when creating a table. The following is to specify 2 fields to be uniquely constrained.
@ID 和  @GeneratedValue

To map hibernate entities through annotations, the annotation-based hibernate primary key is identified as @Id, and 
its generation rules are set by @GeneratedValue. Here @id and @GeneratedValue are both standard usages of  JPA.
JPA provides four standard usages, which are defined by The source code of @GeneratedValue makes it obvious.

 

Where GenerationType:

 

The four standard usages provided by JPA are TABLE, SEQUENCE, IDENTITY, AUTO.

  • TABLE: Use a specific database table to hold the primary key.
  • SEQUENCE: The primary key is generated based on the sequence of the underlying database, provided that the database supports sequences.
  • IDENTITY: The primary key is automatically generated by the database (mainly automatic growth type)
  • AUTO: The primary key is controlled by the program (also the default, when specifying the primary key, if the primary key generation strategy is not specified, the default is AUTO)
 
The four databases are supported as follows:

 

Name database

Supported id strategies

mysql

GenerationType.TABLE
GenerationType.AUTO
GenerationType.IDENTITY
不支持GenerationType.SEQUENCE

oracle

strategy=GenerationType.AUTO
GenerationType.SEQUENCE
GenerationType.TABLE
不支持GenerationType.IDENTITY

postgreSQL

GenerationType.TABLE
GenerationType.AUTO
GenerationType.IDENTITY
GenerationType.SEQUENCE
都支持

kingbase

GenerationType. TABLE
GenerationType. SEQUENCE
GenerationType. IDENTITY
GenerationType. AUTO
are supported

@GeneratedValue: The generation strategy of the primary key, specified by the strategy attribute.

  The primary key generation strategy is specified by GenerationType. GenerationType is an enumeration that defines the type of primary key generation strategy.

  1. AUTO automatically selects a primary key generation strategy that is most suitable for the underlying database. For example, MySQL will automatically correspond to auto increment. This is the default option, that is, if you only write @GeneratedValue, it is equivalent to @GeneratedValue(strategy=GenerationType.AUTO).

  2. The IDENTITY table self-increasing field, Oracle does not support this method.

  3. SEQUENCE generates a primary key through a sequence, which is not supported by MySQL.

  4. TABLE generates the primary key through the table, and the framework generates the primary key through the table simulation sequence. Using this strategy can make the application easier to transplant the database. The table names generated by different JPA implementers are different. For example, OpenJPA generates an openjpa_sequence_table table, Hibernate generates a hibernate_sequences table, and TopLink generates a sequence table. These tables have two fields, a sequence name and a corresponding value, such as SEQ_NAME and SEQ_COUNT.

  In our application, the method of @GeneratedValue(strategy=GenerationType.AUTO) is generally used to automatically select the primary key generation strategy to adapt to different database migrations.

  If you use Hibernate's implementation of JPA, you can use Hibernate's extension to the primary key generation strategy, implemented through Hibernate's @GenericGenerator.

  @GenericGenerator(name = "system-uuid", strategy = "uuid") declares a strategy generic generator with name "system-uuid" and strategy strategy "uuid".

  @GeneratedValue(generator = "system-uuid") Use the generator attribute to specify the strategy generator to use.

  This is a way I use in my project to generate 32-bit strings that are unique values. The most general, applicable to all databases.

======================================================================================================================

 

@Table(name = "scbp_user") //corresponding to the table name in the database
public class ScbpUser {
/**
* User Id
*/
@Id
@Column(name = "Id")
@GeneratedValue(strategy = GenerationType.IDENTITY) / /mysql database primary key policy
private Integer id;

/**
* username
*/
@Column(name = "UserName")
private String username;

/**
* login alias
*/
@Column(name = "Account")
private String account ;

/**
* Phone number
*/
@Column(name = "PhoneNumber")
private String phonenumber;

/**
* Email
*/
@Column(name = "Email")
private String email;

/**
* ID card, military ID number
*/
@Column(name = "IdNumber")
private String idnumber;

/**
* Whether it is a regular employee
*/
@Column (name = "IsInformal")
private Boolean isinformal;

/**
* password
*/
@Column(name = "PasswordHash")
private String passwordhash;

/**
* creation time
*/
@Column(name = "CreationTime")
private Date creationtime;

/**
* CreatorId
*/
@Column(name = "CreatorUserId")
private Integer creatoruserid;

/**
* Last Modification Time
*/
@Column(name = "LastModificationTime")
private Date lastmodificationtime;

/**
* Updater Id
*/
@Column(name = "LastModifierUserId")
private Integer lastmodifieruserid;

/**
* Delete marker
*/
@Column(name = "IsDeleted")
private Boolean isdeleted;

/**
*Deletion time
*/
@Column(name = "DeletionTime")
private Date deletiontime;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325259047&siteId=291194637