Here are a few of the more common strategies:
①identity [Natural Increment]
Supports Db2,mysql,sql server,sybase and Hypersonicsql databases for generating unique identities for long or short or int types. It relies on different databases at the bottom,
Not related to Hibernate and programmers.
Examples of annotations:
@Id
@GenericGenerator (name = "Idgenerator", strategy = "identity")
@GeneratedValue (generator = "Idgenerator")
②sequence [sequence]
Databases such as ORACLE,DB2,POSTGRESQL,SAPDB are supported for generating unique identities for long or short or int types. It requires support from the underlying database,
The sequence sequence is maintained by the database.
Examples of annotations:
@Id
@GenericGenerator (name = "Idgenerator", strategy = "sequence",
Parameters = {@Parameter (name = "Sequence", value= "Seq_name")})
@GeneratedValue (generator = "Idgenerator")
Note: This policy requires that the sequence name be set, otherwise hibernate will not be found, which will cause an exception to be thrown:
Org.hibernate.exception.SQLGrammarException:could not get Next sequence value
③native
Support for the underlying database is required, and for Oracle, the sequence policy is used for Mysql,sql Server's identity generation strategy.
Examples of annotations:
@Id
@GenericGenerator (name = "Idgenerator", strategy = "native")
@GeneratedValue (generator = "Idgenerator")
④increment [Natural Increment]
Unlike the identity policy, the policy does not rely on the underlying database, but relies on hibernate itself to generate a unique identity for a long or short or int type.
The primary key counter is maintained by one instance of hibernate, each with a self-increment of 1, but cannot be used under the cluster.
Otherwise, a primary key violation is caused, and the policy applies to all relational database uses.
Examples of annotations:
@Id
@GenericGenerator (name = "Idgenerator", strategy = "increment")
@GeneratedValue (generator = "Idgenerator")
⑤uuid [string of 32-bit 16 binary numbers]
Using the 128-bit UUID algorithm to generate the primary key can guarantee the uniqueness of the primary key under the network environment, and it can guarantee the uniqueness of the primary key under different database and different server.
The UUID is eventually encoded into a 32-bit, 16-digit string,
Occupy a large amount of storage space. Used to generate a unique identity for the String type, applicable to all relational databases.
Examples of annotations:
@Id
@GenericGenerator (name = "Idgenerator", strategy = "uuid")
@GeneratedValue (generator = "Idgenerator")
⑤assigned [manually assigning primary key ID values]
This policy requires the programmer to maintain and manage the primary key on its own, and when the data needs to be stored, the programmer must specify a primary key ID value for the data assignment.
If the data is not assigned the primary key ID value or the assigned value is duplicated, the data will not be persisted and will cause an exception to be thrown.
Examples of annotations:
@Id
@GenericGenerator (name = "Idgenerator", strategy = "assigned")
@GeneratedValue (generator = "Idgenerator")