Siege Lion on the road (one) Hibernate (iv)---object identifier (OID) generation mechanism

Source: Internet
Author: User
Tags db2 uuid

Hibernate uses object identifiers (OIDs) to establish correspondence between objects in memory and records in database tables, and the OID of the object corresponds to the primary key of the database. To ensure the uniqueness and immutability of the OID, hibernate should be assigned a value for the OID. Hibernate assigns a value to the OID through the identifier generator.

One, specify the identifier generator :
Use the child element of the <id> element <generator class= "increment"/> to specify. Hibernate provides the built-in identifier generator:

A, Increment: Applies to the proxy primary key. An identifier is automatically generated incrementally by hibernate. B, Identity: Applies to the proxy primary key. Generated by the underlying database. The prerequisite is that the underlying database supports autogrow field types, such as: Db2\mysql\ms SQL server\sybase\hypersonicsql.c, sequence: for proxy primary keys. Generated by the sequence of the underlying database. The premise is that the underlying database supports sequences. such as: Db2\postgresql\oracle\sap Db;d, Hilo: Applies to the proxy primary key. Hibernate generates identifiers based on the high\low algorithm. E,native: Applies to the proxy primary key. Select identity, Sequence, or Hilo based on the support capabilities of the underlying database for automatically generated identifiers. F, Uuid.hex: Applies to the proxy primary key. Hibernate is generated using the 128-bit UUID algorithm. G, Assigned: Applies to the natural primary key. specified by the Java program. H, select: Applies to the proxy primary key or the natural primary key in the legacy database. The identifier is generated by a trigger in the database. (The specific mechanism is unclear) I, foreign: (not understand)

Second,increment identifier generator :
Hibernate first executes the following select Max (ID) from tableName when inserting records, obtains the current maximum ID value, and then increments by increment on that basis.
This mechanism may occur when multiple record ID values are inserted in concurrent operations, so this approach works only if a single hibernate application process accesses the database. Other situations that involve concurrent insertions also fail.
  Applicable Scenarios :

A, since the mechanism generates identifiers that do not depend on the underlying database, it is suitable for all database systems. B, for applications where only a single hibernate application process accesses the same database (multithreading should also fail), it is not recommended in a clustered environment. C, OID must be a long, int, or short type.

Third,identity identifier generator :
This mechanism requires the underlying database to define the primary key as the field type that grows automatically.
  Applicable Scenarios :

A, the underlying database supports fields for autogrow types. B, OID must be a long, int, or short type.

Iv.sequence identifier generator :
Applicable Scenarios :

A, the underlying database must support sequences. B, OID must be a long, int, or short type.

V.Hilo Identifier Generator :
Applicable Scenarios :

A, since the mechanism generates identifiers that do not depend on the underlying database, it is suitable for all database systems. B, OID must be a long, int, or short type. C, the identifier generated by the High\low algorithm can only be guaranteed to be unique within a single database.

Six, the map Composite Natural primary Key :
Example configuration file :

?
1 2 3) 4 5 <composite-id>             <!-- key-property标签表示哪一些属性对应复合主键 -->             <key-property name="id" column="id" type="string"></key-property>             <key-property name="name" column="name" type="string"></key-property> </composite-id>

Java code example :

?
1 2 3 4 Customer cu = new Customer(); cu.setName("Tom"); cu.setCompanyId(1L);session.get(Customer.class,cu);

Description :
For the Get () method of the previous session to take effect, the customer must implement the Serializable interface and override the Equals () and Hashcode () methods.

Vii. Summary :

?
1 2 3 4 MySQL:indentity\increment\hilo\native MS SQL Server:identity\increment\hilo\native Oracle:sequence\seqhilo\hilo\increment\native跨平台开发:native

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.