Hibernate provides a variety of policies that produce automatic growth type primary key, here as an example of UUID.HEX: 1, set a table in the database, the primary key name is ID, type is varchar2 (string type) 2, in ** .hbm.xml (Hibernate mapping file) configured
Test test = new test (); string uname = httpservletRequest.getParameter ("uname"); test.setuname (uname); // only to set the uname, ID is generated by Hibernate; tx.commit ();
4. The method of using other strategies is basically consistent, such as Hilo, Seqhilo, etc.
Generator provides unique identifiers for each Pojo instance. In general, we use "native". Class represents an instance implemented by the generator interface NET.SF.HIBERNATE.ID.IDENTIFIERGENERATOR, including:
Assigned "
The primary key is generated by the external program and specifies one before save ().
"Hilo"
The primary key generation mechanism implemented by the Hi / LO algorithm requires additional database tables or fields to provide high-bit values.
"Seqhilo"
Similar to HILO, primary key generation mechanism implemented by Hi / LO algorithm, requires Sequence in the database, which is suitable for supporting Sequence's database, such as Oracle.
"Increment"
The primary key is incremented in the order of value. The implementation mechanism of this manner is to maintain a variable in the current application instance to save the current maximum, and then add this value to 1 as the primary key each time you need to generate a primary key. The problem that may be generated in this way is that it cannot be used under the cluster.
Identity "
The primary key generation mechanism provided by the database. Such as the primary key generating mechanism in DB2, SQL Server, MySQL.
"Sequence"
The SEQUENCE mechanism supplied with the database generates a primary key. Such as Sequence in Oralce.
"Native"
One of the use of Identity, Hilo, Sequence is used as a primary key generation by Hibernate.
"Uuid.hex"
The 16-based value is generated based on the 128-bit UUID algorithm based on the 128-bit UUID algorithm (indicated by a string of length 32) as the primary key. UUID.STRING
Similar to uuid.hex, only the generated primary key is not encoded (length 16), and cannot be applied in the PostgreSQL database.
"ForeIgn"
Use another identifier of another associated object as the primary key.