Hibernate - Object Identifier Generation Method (Primary Key Growth Strategy)

Hibernate Object Identifier (OID)

The persistent object in Hibernate corresponds to a data table in the database, so different persistent objects are distinguished. In Hibernate, it is done through OID (Object ID, object identifier). From the perspective of the table, OID corresponds to the table the primary key. From the perspective of the class, the OID corresponds to the primary key attribute of the class.

Object identifier generation method

There are many main object identifier generation strategies in Hibernate. Here we mainly introduce 9 identifier generation methods. These include 7 identifier generators and a composite primary key generation method.

increment identifier generator

Identifiers are automatically generated by Hibernate in increments of 1.

Advantages: Does not depend on the underlying database system, applicable to all database systems.
Disadvantages: It is suitable for single-process environment, and it is likely to generate the same primary key value in multi-threaded environment, and OID must be of numeric type, such as long, int, short type.

The configuration method is:

<id name="id" column="id">
    <generator class="increment"/>
</id>

identity identifier generator

The identifier is generated by the underlying database.

The prerequisites are: the database supports auto-increment field types, such as (SQLServer, MySQL), and the OID must be a numeric type, such as long, int, and short.

Configuration method:

<id name="id" column="id" type="java.lang.Integer">
    <generator class="identity"/>
</id>

sequence identifier generator

Sequence dependent on the underlying database system.

The prerequisites are: the database needs to support the sequence mechanism (such as Oracle, etc.), and the OID must be a numeric type, such as long, int, and short.

Configuration method:

<id name="id" type="java.lang.Long" column="ID">
    <generator class="sequence">
        <param name="sequence">my_seq</param><!--配置序列的名字-->
    </generator>
</id>

native identifier generator

The native generator can automatically select the appropriate identifier generator according to the type of the underlying database system, so it is very suitable for cross-database platform development. It will be automatically adopted by Hibernate according to the definition in the database adapter. One of identity, hilo, sequence It is used as the primary key generation method, but the OID must be a numeric type, such as long, int, and short types.

Configuration method:

<id name="id" type="java.lang.Integer" column="ID">
    <generator class="native"/>
</id>

hilo identifier generator

The hilo identifier generator is generated by Hibernate according to a high/low algorithm. It obtains the high value from the field of a specific table in the database, so an additional database table is required to save the historical state of the primary key generation. The hilo generation method does not depend on It is applicable to the underlying database, so it is suitable for every database, but the OID must be a numeric type, such as long, int, short, etc.

Configuration method:

<id name="id" type="java.lang.Long" column="ID">
    <generator class="hilo">
        <param name="table">my_hi_value</param>
        <param name="column">next_value</param>
    </generator>
</id>

uuid identifier generator

Hibernate generates an algorithm based on a 128-bit unique value, and generates a hexadecimal value as the primary key according to four parameters such as the current device IP, time, JVM startup time, and internal self-increment. Generally speaking, the primary key generated by uuid provides the best Data insertion performance and database platform adaptability. OIDs are generally of type String.

Configuration method:

<id name="id" type="java.lang.String" column="ID">
    <generator class="uuid"/>
</id>

assigned identifier generator

Using the assign generation strategy means that the application logic is responsible for generating the primary key identifier, and the OID type is not limited.

Configuration method:

<id name="id" type="java.lang.String" column="ID">
    <generator class="assigned"/>
</id>

foreign identifier generator

Used in a one-to-one relationship, automatically generated based on the primary key of another table. For details, see [ Hibernate - one-to-one relationship mapping ].

Mapping conforms to primary key

Try not to use the primary key, the data is unstable and the efficiency is relatively low. It is omitted here for the time being.

Principles for choosing generators

  1. For Oracle database, the primary key is numeric, it is recommended to use sequence, and if the primary key is a string, it is recommended to use uuid or assigned

  2. For MySQL database, if the primary key is numeric, it is recommended to use increment or assigned, and if the primary key is a string, it is recommended to use uuid or assigned

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325655538&siteId=291194637