1. UUID: Automatically generate 32-bit and above random string, generated by including but not limited to network card address, time value and so on.
Note: Can be cross-database, high efficiency, can guarantee uniqueness, recommended to use "Although Occupy space large"
2. Increment: Automatically gets the maximum value of the primary key in the database "integer", and automatically adds a value to the object.
Note: You can cross a database, but not for use under a cluster, and the multi-threaded concurrent update database will fetch the same primary key value.
3. Identity: The database primary key is set to auto-Grow "integer".
Note: For Mysql,db2,sqlserver, not for Oracle.
4. Sequence: The database primary key is set to auto-Grow "integer".
Note: Applies to Oracle.
5. One of the most common forms of generation in hilo:hibernate requires an extra table to hold the HI value. The table that holds the HI value has at least one record (only related to the first record), or an error occurs.
<IDname= "id"column= "id"><Generatorclass= "Hilo"><paramname= "Table">Hibernate_hilo</param> <paramname= "column">Next_hi</param><paramname= "Max_lo">100</param></Generator></ID><!--Specifies the name of the table that holds the HI value -<paramname= "Table">Hibernate_hilo</param><!--Specify column names to hold hi values -<paramname= "column">Next_hi</param> <!--Specify the maximum value of the low -<paramname= "Max_lo">100</param> <!--You can also omit the table and column configurations, whose default table is Hibernate_unique_key, which is listed as Next_hi -<IDname= "id"column= "id"><Generatorclass= "Hilo"><paramname= "Max_lo">100</param></Generator></ID>
The Hilo generator process for generating a primary key (in the Hibernate_unique_key table, next_hi column example):
- Get Hi Value: Reads and records the value of the Next_hi field in the database's Hibernate_unique_key table, and the value of this field in the database is saved by 1.
- Get the LO value: from 0 to Max_lo Loop, the difference is 1, the value is Max_lo, and the LO value continues to loop from 0 to Max_lo.
- Generates a primary key value based on the formula Hi * (Max_lo + 1) + lo calculation.
Note: When the Hi value is 0, then the first value is not 0* (max_lo+1) +0=0, but Lo skips 0 starting from 1, directly 1, 2, 3 ...
How much is the Max_lo configuration appropriate?
This depends on the situation, if the system generally does not restart, and the need to use this table to establish a large number of primary keys, you can max_lo configuration larger, so you can reduce the number of reading data tables, improve efficiency; Conversely, if the server is often restarted, you can max_lo a smaller configuration, You can avoid a large interval between each reboot of the primary key, which causes the primary key value of the primary key to be incoherent.
Note: You can cross-database, the flags generated by the Hilo algorithm can only remain unique in one database.
6. Native:native by hibernate according to the database used by the use of identity, Hilo, sequence one of the primary key generation mode, flexibility is strong. If the identity is supported, the identity is used and sequence is used if sequence is supported.
Note: According to the automatic selection of the database, if you use multiple databases in the project, you can use this method, you need to set the table's self-increment field or set up a sequence, create a table, etc. 7. Foreign: Used in a one-to-one association relationship.
<IDname= "id"column= "id"><Generatorclass= "foreign"><paramname= "Property">User</param></Generator></ID><one-to-onename= "User"class= "Com.msym.domain.User"constrained= "true" />
Note: Less use, only one-to-one association relationship "a single relationship is usually a table."