User. HBM. xml
<? XML version = "1.0" encoding = "UTF-8" ?>
< Hibernate-Mapping Xmlns = "Urn: nhibernate-mapping-2.2" >
< Class Name = "Preordainsolution. preordainmodel. User, preordainsolution. preordainmodel" Table = "Users" Lazy = "False" >
< ID Name = "Accountid" Column = "Accountid" Type = "Int32" >
< Generator Class = "Native" > </ Generator >
</ ID >
< Property Name = "Account" Column = "Account" Type = "String" Length = "100" > </ Property >
< Property Name = "Username" Column = "Username" Type = "String" Length = "100" > </ Property >
< Property Name = "Password" Column = "Password" Type = "String" Length = "200" > </ Property >
< Property Name = "Enable" Column = "Enable" Type = "Int32" > </ Property >
< Property Name = "Phone" Column = "Phone" Type = "String" Length = "200" > </ Property >
< Property Name = "Im" Column = "Im" Type = "String" Length = "100" > </ Property >
< Property Name = "Email" Column = "Email" Type = "String" Length = "200" > </ Property >
< Property Name = "Level" Column = "Level" Type = "Int32" > </ Property >
< Property Name = "Lastdate" Column = "Lastdate" Type = "Datetime" > </ Property >
< Property Name = "Remark" Column = "Remark" Type = "String" Length = "2000" > </ Property >
</ Class >
</ Hibernate-Mapping >
In<Generator class = "native"> </generator>
Primary Key Generator
Option description:
1) assigned
Primary Key from externalProgramResponsible for generation, without the involvement of nhib.pdf.
2) HiLo
Use hi/LoAlgorithmImplements the primary key generation mechanism and requires additional database tables to be saved to the master
Key Generation history status.
3) seqhilo
Similar to hilo, the primary key generation mechanism implemented through the HI/LO algorithm is only the primary key history.
The status is stored in sequence, which is applicable to databases that support sequence, such as oracle.
4) Increment
The primary key increments in numerical order. The implementation mechanism of this method is maintained in the current application instance.
A variable to save the current maximum value, and each time the primary key needs to be generated
Add 1 as the primary key.
This method may cause a problem: if multiple instances currently access the same data
Because each instance maintains its primary key status, different instances may generate the same
As a result, duplicate primary keys are abnormal. Therefore, if the same database has multiple instances
For example, this method must be avoided.
5) Identity
Use the primary key generation mechanism provided by the database. Such as DB2, SQL Server, MySQL
Primary Key Generation Mechanism in.
6) sequence
Use the sequence mechanism provided by the database to generate the primary key. For example
Sequence.
7) Native
According to the underlying database, nhib.pdf determines whether to use identity, Hilo, and sequence.
One of them is used as the primary key generation method.
8) UUID. HEX
The algorithm generated by hibernate Based on the 128-bit unique value generates a hexadecimal value (after Encoding
Is expressed as a string of 32 characters) as the primary key.
9) UUID. String
Similar to UUID. HEX, the generated primary key is not encoded (Length: 16 ). In some
Problems may occur in the database (such as PostgreSQL ).
10) Foreign
Use the field of the External table as the primary key.
Generally, using UUID. HEX to generate a primary key provides the best performance and database platform suitability.
Response.
In addition, common databases such as Oracle, DB2, SQL Server, and MySQL All provide
It provides an easy-to-use primary key generation mechanism (auto-increase field or sequence ). We can
The primary key generation mechanism provided by the database adopts the primary key generation method of generator-class = native.
However, it is worth noting that the primary key generation mechanism provided by some databases is not necessarily the best in terms of efficiency,
A large number of concurrent insert data may cause mutual locks between tables.
The primary key generation mechanism provided by the database usually stores the current primary key in an internal table
(For example, for an auto-increment primary key, this internal table maintains the current maximum value and incremental amount ),
Then, the maximum value is read each time the data is inserted, and the incremental value is added as the primary key of the new record.
Then update the new maximum value back to the internal table. In this way, an insert operation may cause data
Multiple table read/write operations in the database, along with data lock and unlock operations, which results in performance
.
Therefore, for systems with high requirements for concurrent insert, UUID. Hex is recommended for primary key generation.
Mechanism.