hibernate primary key generation strategy based on annotation

Keywords: Java Hibernate Database MySQL

A custom primary key generation policy is implemented by @GenericGenerator.
Hibernate has been extended on the basis of JPA. It can be used to introduce hibernate's unique primary key generation strategy, which is joined by @GenericGenerator.

For example, JPA standard usage

@Id
@GeneratedValue(GenerationType.AUTO)

This can be achieved using hibernate's specific usage as follows

@GeneratedValue(generator = "paymentableGenerator") 
@GenericGenerator(name = "paymentableGenerator", strategy = "assigned")

@ The definition of GenericGenerator:

@Target({PACKAGE, TYPE, METHOD, FIELD})
@Retention(RUNTIME)
public @interface GenericGenerator{
String name(); 
String strategy(); 
Parameter[] parameters() default {}; 
}

The name attribute specifies the generator name.
The strategy attribute specifies the class name of the specific generator.
Parameters get the parameters used by the specific generator specified by strategy.

The relationship between these hibernate primary key generation strategies and their respective specific generators is specified in org.hibernate.id.IdentifierGeneratorFactory.

static { 
GENERATORS.put("uuid", UUIDHexGenerator.class); 
GENERATORS.put("hilo", TableHiLoGenerator.class); 
GENERATORS.put("assigned", Assigned.class); 
GENERATORS.put("identity", IdentityGenerator.class); 
GENERATORS.put("select", SelectGenerator.class); 
GENERATORS.put("sequence", SequenceGenerator.class); 
GENERATORS.put("seqhilo", SequenceHiLoGenerator.class); 
GENERATORS.put("increment", IncrementGenerator.class); 
GENERATORS.put("foreign", ForeignGenerator.class); 
GENERATORS.put("guid", GUIDGenerator.class); 
GENERATORS.put("uuid.hex", UUIDHexGenerator.class); //uuid.hex is deprecated 
GENERATORS.put("sequence-identity", SequenceIdentityGenerator.class); 
}

The above twelve strategies, plus native, hibernate supports thirteen generation strategies by default.

1,native
For orcale, Sequence is adopted, and for MySQL and SQL Server, identity (situational primary key generation mechanism) is adopted. native means that the generation of primary key is handed over to the database itself, regardless of hibernate. (Very common)
Java code

@GeneratedValue(generator = "paymentableGenerator") 
@GenericGenerator(name = "paymentableGenerator", strategy = "native")

2,uuid
The 128-bit uuid algorithm is used to generate the primary key. uuid is encoded into a 32-bit 16-digit string. It takes up a lot of space (string type).
Java code

@GeneratedValue(generator = "paymentableGenerator") 
@GenericGenerator(name = "paymentableGenerator", strategy = "uuid")

3,hilo
To create an additional table in the database, the default table is hibernate_unque_key, the default field is integer type, and the name is next_hi.
Java code

@GeneratedValue(generator = "paymentableGenerator") 
@GenericGenerator(name = "paymentableGenerator", strategy = "hilo")

4,assigned
When inserting data, the primary key is handled by the program (very often), which is the default generation strategy when the element is not specified. Equivalent to AUTO in JPA.
Java code

@GeneratedValue(generator = "paymentableGenerator") 
@GenericGenerator(name = "paymentableGenerator", strategy = "assigned")

5,identity
Using self-incremental fields of SQL Server and MySQL, this method can not be put into Oracle. Oracle does not support self-incremental fields and sets sequence (which is commonly used in MySQL and SQL Server). Identical to IDENTITY in JPA
Java code

@GeneratedValue(generator = "paymentableGenerator") 
@GenericGenerator(name = "paymentableGenerator", strategy = "identity")

6,select
Use triggers to generate primary keys (mainly for early database primary key generation mechanisms, less used)
Java code

@GeneratedValue(generator = "paymentableGenerator") 
@GenericGenerator(name="select", strategy="select", 
parameters = { @Parameter(name = "key", value = "idstoerung") })

7,sequence
Call the sequence of the prudent database to generate the primary key. Set the sequence name, otherwise hibernate can't find it.
Java code

@GeneratedValue(generator = "paymentableGenerator") 
@GenericGenerator(name = "paymentableGenerator", strategy = "sequence", 
parameters = { @Parameter(name = "sequence", value = "seq_payablemoney") })

8,seqhilo
It is implemented by hilo algorithm, but the history of primary key is stored in Sequence, which is suitable for database supporting Sequence, such as Orcale.
Java code

@GeneratedValue(generator = "paymentableGenerator") 
@GenericGenerator(name = "paymentableGenerator", strategy = "seqhilo", 
parameters = { @Parameter(name = "max_lo", value = "5") })

9,increment
Hibernate adds a self-increasing primary key to the primary key when inserting data, but a hibernate instance maintains a counter, so this method cannot be used when multiple instances run.
Java code

@GeneratedValue(generator = "paymentableGenerator") 
@GenericGenerator(name = "paymentableGenerator", strategy = "increment")

10,foreign
Java code

@GeneratedValue(generator = "idGenerator") 
@GenericGenerator(name = "idGenerator", strategy = "foreign", 
parameters = { @Parameter(name = "property", value = "employee") })

Note: Use @PrimaryKey JoinColumn to report errors directly (?)
Java code

@OneToOne(cascade = CascadeType.ALL) 
@PrimaryKeyJoinColumn 

for example
Java code

@Entity
public class Employee { 
@Id Integer id; 

@OneToOne @PrimaryKeyJoinColumn?
EmployeeInfo info; 

}

Should be
Java code

@Entity
public class Employee { 
@Id 
@GeneratedValue(generator = "idGenerator") 
@GenericGenerator(name = "idGenerator", strategy = "foreign", 
parameters = { @Parameter(name = "property", value = "info") }) 
Integer id; 

@OneToOne
EmployeeInfo info; 

}

11,guid
Using the guid algorithm mechanism at the bottom of the database, corresponding to the uuid() function of MySQL, the newid() function of SQL Server, the rawtohex(sys_guid()) function of ORCALE, etc.
Java code

@GeneratedValue(generator = "paymentableGenerator") 
@GenericGenerator(name = "paymentableGenerator", strategy = "guid")

12,uuid.hex
Look at uudi. Suggest replacing it with uuid
Java code

@GeneratedValue(generator = "paymentableGenerator") 
@GenericGenerator(name = "paymentableGenerator", strategy = "uuid.hex")

13,sequence-identity
Java code

@GeneratedValue(generator = "paymentableGenerator") 
@GenericGenerator(name = "paymentableGenerator", strategy = "sequence-identity", 
parameters = { @Parameter(name = "sequence", value = "seq_payablemoney") })

4. Customize Primary Key Generation Strategy through @GenericGenerator
If in practice, the primary key policy specifies the assigned primary key for the program and takes it from the sequence without specifying it.
Obviously, all the strategies discussed above are not satisfied, so we have to expand and integrate assigned and sequence strategies.

Java code

public class AssignedSequenceGenerator extends SequenceGenerator implements 
PersistentIdentifierGenerator, Configurab

Posted by gerrydewar on Mon, 25 Mar 2019 08:48:30 -0700