1. The assigned primary key is generated by an external program and must be specified before saving. Hibernate does not maintain primary key generation. Neither Hibernate nor underlying databases
1. The assigned primary key is generated by an external program and must be specified before saving. Hibernate does not maintain primary key generation. Neither Hibernate nor underlying databases
1,Assigned
The primary key is generated by an external program.Save ()You must specify one.HibernateIt is not responsible for maintaining primary key generation. AndHibernateIt has nothing to do with the underlying database and can be cross-database. Before storing objects, you must useSetterThe method assigns a value to the primary key. It is up to you to determine how this value is generated. This method should be avoided as much as possible.
"Ud"Is the name of a custom policy. It is a human name and is used later."Ud.
Features: You can control the generation of primary keys across databases and avoid them as much as possible.
2 ,Increment
ByHibernateExtracts the maximum value of the primary key from the database (eachSessionRetrieve only1), Based on this value, each increment is1The primary key is generated in the memory and does not depend on the underlying database. Therefore, it can be used across databases.
HibernateCallOrg. hibernate. id. IncrementGeneratorClassGenerate ()Method, useSelect max (idColumnName) from tableNameStatement to obtain the maximum value of the primary key. This method is declaredSynchronized, So in an independentJavaThere is no problem inside the virtual machine, however, in multipleJVMConcurrent Database AccessSelect maxThe same value may be retrieved.InsertIt will happenDumplicate entry. Therefore, only oneHibernateIf the application process accesses the database, otherwise a primary key conflict may occur. Therefore, it is not suitable for multi-process concurrent database update. It is suitable for a single process to access the database and cannot be used in a cluster environment.
Official documentation: This document can be used only when no other process inserts data into the same table. It is not used in a cluster.
Features: Cross-database, not suitable for multi-process concurrent database updates, suitable for a single process to access the database, cannot be used in a cluster environment.
3 ,Hilo
Hilo(High/low bit modeHigh low) YesHibernateOne of the most common generation methods requires an additional table to be saved.Hi. SaveHiThe value table has at least one record (only related to the first record). Otherwise, an error occurs. Cross-database.
Hibernate_hilo
Next_hi
100
Hibernate_hilo SaveHiValue table name
Next_hi SaveHiColumn name of the value
100 Specify the maximum value of A Low Position
You can also omitTableAndColumnConfiguration. The default table isHibernate_unique_key, ColumnNext_hi
100
HiloThe process of generating a primary key by usingHibernate_unique_keyTable,Next_hiFor example ):
1.ObtainHiValue: Read and recordHibernate_unique_keyTableNext_hiField value. This field value is added to the database.1Save.
2.ObtainLoValue: From0ToMax_loCyclic value. The difference value is1, The value isMax_loValue, re-obtainHiValue, and thenLoValue continues from0ToMax_loLoop.
3.According to FormulaHi * (max_lo + 1) + loCalculates the generated primary key value.
Note: WhenHiThe value is0The first value is not0 * (max_lo + 1) + 0 = 0,LoSkip0Slave1Start, directly1,2,3......
ThatMax_loHow much configuration is appropriate?
This depends on the specific situation. If the system generally does not restart and you need to use this table to create a large number of primary keys, right?Max_loThe configuration is relatively large, which can reduce the number of reading data tables and improve efficiency. If the server restarts frequently, you canMax_loIf the configuration is smaller, the interval between primary keys is too large each time they are restarted, resulting in inconsistent primary key values.
Features: Cross-database,HiloThe algorithm-generated flag must be unique in only one database.
4 ,Seqhilo
AndHiloSimilarlyHi/loThe primary key generation mechanism implemented by the algorithm isHiloThe data table in is replaced with a sequenceSequence, You must first create a databaseSequence, Applicable to supportSequenceSuchOracle.
Hibernate_seq
100
Features:HiloSimilarly, it can only be used in databases that support sequences.
5 ,Sequence
UseSequenceThe primary key generation mechanism requires Database SupportSequence. For exampleOralce,DB,SAP DB,PostgerSQL,McKoiInSequence.MySQLThis is not supportedSequence(You can useIdentity).