JPA annotation @generatedvalue and its strategy enumeration
1. Source Code
@Target ({elementtype.method, Elementtype.field})
@Retention (retentionpolicy.runtime) public
@interface Generatedvalue {
generationtype strategy () default Generationtype.auto;
String generator () Default "";
}
public enum GenerationType {
TABLE,
SEQUENCE,
IDENTITY,
AUTO;
Private GenerationType () {
}
}
2. Introduce
-table: Use a specific database table to save the primary key.
-SEQUENCE: Generates a primary key based on the sequence of the underlying database, provided the database supports a sequence.
-IDENTITY: Primary keys are automatically generated by the database (mainly automatic growth)
-Auto: Primary keys are controlled by the program.
If the database primary key uses the MySQL automatic generation policy, the following definition is required to properly save
Otherwise throw cannot find generator exception
@Id
@GeneratedValue (strategy = generationtype.identity)
private Long pkid;