Proficient in hibernate--hibernate built-in identifiers

Source: Internet
Author: User
Tags db2

1. Increment identifier generator
The surrogate primary key is incremented by hibernate, for example:

<hibernate-mapping>    <class name="Mypack." Incrementtester " table=" Increment_tester " >        <ID name="id" type= "long" column=" ID " >            <meta attribute="Scope-set">Private</meta>            <generator class="increment" />        </ID>        < property name="name" type="string">            <column name="name" length=" /> "        </Property >    </class></hibernate-mapping>

Hibernate generates an identifier incrementally when a Incrementtester object is persisted, in fact, hibernate reads the maximum primary key value from the Increment_tester table in the initialization phase, and then Increment_ When a record is inserted in the tester table, it is incremented on the basis of Max (ID), and the increment is 1. Consider the scenario where two hibernate application processes access the same database.
(1) Assume that hibernate in the first process reads the maximum primary key in the Increment_tester table in the initialization phase is 6
(2) Hibernate in the second process reads the maximum primary key of the Increment_tester table in the initialization phase is still 6
(3) Hibernate in the next two processes each inserts a record with a primary key of 7 to the Increment_tester table, which violates the database integrity constraint and causes the insert operation of one process to fail.
It is seen that the increment identifier generator works only when a single hibernate application process accesses the database. More specifically, even if multiple sessionfactory instances connected to the same database are created in the same process, the insert operation is invalidated. In the Java EE Software architecture, hibernate is typically run as a Jndi resource on the application server.
The increment identifier generator has the following scope of application:
1, because the mechanism of increment generated identifier is not dependent on the underlying database system, so he applies to all database systems
2, suitable for only a single hibernate application process to access the same database, in a clustered environment is not recommended to use
3, the OID must be a long,int or short type, if the OID is defined as a byte type, the following exception is thrown at run time:
[Java] net.sf.hibernate.id.IdentifierGenerationException:this ID generator generates long,integer,short,identity
2. Identity identifier Generator
The identity identifier generator is responsible for generating identifiers by the underlying database, and he requires the underlying database to define the primary key as the autogrow field type. For example: In MySQL, the primary key should be defined as the Auto_increment type, and in Ms SQL service, the primary key should be defined as the identity type. The configuration is as follows:

<hibernate-mapping>    <class name="Mypack." Incrementtester " table=" Increment_tester " >        <ID name="id" type= "long" column="id" >            <meta attribute="Scope-set">Private</meta>            <generator class="Identity" />        </ID>        < property name="name" type="string">            <column name="name" length=" /> "        </Property >    </class></hibernate-mapping>

Hibernate when persisting a Identitytester object, the underlying database is responsible for generating the primary key
The identity identifier generator has the following scope of use.
1, because the identity generation identifier mechanism relies on the underlying database system, it is required that the underlying database system must support the Autogrow field type. Supported databases are DB2, Mysql, Ms SQL Server, Sybase, Hsqldb, and Informix
2, OID must be a long,int or short type, if the OID is defined as byte at run time will throw the following exception
[Java] net.sf.hibernate.id.IdentifierGenerationException:this ID generator generates long,integer,short,identity
3. Sequence Identifier generator
The sequence identifier generator uses the sequence provided by the underlying data to generate identifiers, such as:

<hibernate-mapping>    <class name="Mypack." Incrementtester " table=" Increment_tester " >        <ID name="id" type="Long" column="id" >             <meta attribute="Scope-set">Private</meta>            <generator class="sequence">                <param name="sequence">Tester_id_seq</param>            </Generator>        </ID>        < property name="name" type="string">            <column name="name" length=" /> "        </Property >    </class></hibernate-mapping>

Hibernate when persisting a Sequencetester object, obtain a unique sequence number from the tester_id_seq sequence of the underlying database, and then use it as the primary key value.
The sequence identifier generator has the following scope of use:
1, since the sequence generation identifier mechanism relies on the sequence of the underlying database system, it is required that the underlying database system must support the sequence and that the supported databases are Oracle, DB2, SAP db, and PostgreSQL.
2, the OID must be a long, int, or short type, and if the OID is defined as a byte type, the following exception is reported at run time:
[Java] net.sf.hibernate.id.IdentifierGenerationException:this ID generator generates long,integer,short,identity
4. Hilo Identifier Generator
The Hilo identifier generator generates identifiers from hibernate according to a certain High/low algorithm, and he obtains the high values from the fields of a particular table in the database, such as the following configuration declaration using the Hilo Identifier generator, where high values are stored in the Hi_value table Next_ In the Value field:

<hibernate-mapping>    <class name="Mypack." Incrementtester " table=" Increment_tester " >        <ID name="id" type= "long" column="id" >            <meta attribute="Scope-set">Private</meta>            <generator class="Hilo">                <param name="table">Hi_value</param>                <param name="column">Next_value</param>                <param name="Max_lo">100</param>            </Generator>        </ID>        < property name="name" type="string">            <column name="name" length=" /> "        </Property >    </class></hibernate-mapping>

Hibernate is responsible for generating the primary key when a Hilotester object is persisted by hibernate. When the Hilo identifier generator generates identifiers, it needs to read and modify the Next_value in the Hi_value table. This operation needs to be handled in a separate transaction, such as in the following code, when the Session.save (object) method is executed, the Hilo identifier generator does not apply the current database connection and transaction for the session object, but instead creates a new transaction in a new database connection. Then access the Hi_value table

tx = session.beginTransaction();session.save(object);tx.commit();

The Hilo identifier Generator has the following scope of application:
1. Because Hilo generates identifiers that are not dependent on the underlying database system, it is suitable for all database systems
2, the OID must be a long, int, or short type, and if the OID is defined as a byte type, the following exception will be thrown at run time:
[Java] net.sf.hibernate.id.IdentifierGenerationException:this ID generator generates long,integer,short,identity
3. The identifier generated by the High/low algorithm can only be guaranteed in one database
4. Hilo is not available when the user provides a database connection for hibernate itself, or hibernate passes JTA, so that the application server's data source gets a database connection because it does not guarantee that Hilo will access Hi_value in the new database connection transaction. In this case, if the database system supports sequences, you can use the Sehilo generator. , such as Oracle
5. Native Identifier generator
The native identifier generator chooses to use identity, sequence, or Hilo to identify the Fuzhou generator, depending on the underlying database's ability to automatically generate identifiers. Native can automatically determine the mechanism for generating identifiers provided by the underlying database. For example, if MySQL and MS SQL Server, select the identity identifier generator and, if Oracle, select the sequence Identity Fuzhou generator. For example:

<hibernate-mapping>    <class name="Mypack." Incrementtester " table=" Increment_tester " >        <ID name="id" type= "long" column="id" >            <meta attribute="Scope-set">Private</meta>            <generator class="native" />        </ID>        < property name="name" type="string">            <column name="name" length=" /> "        </Property >    </class></hibernate-mapping>

When the underlying database is MySQL, the identity identifier generator is used, so when hibernate persists a Nativetester object, the underlying database is responsible for generating the primary key value. The native identifier generator has the following scope of application:
1, because the native can automatically select the appropriate identifier generator according to the type of the underlying database system, it is very suitable for the development of the Kua database platform, that is, the same hibernate application needs to connect a variety of database systems.
2, the OID must be a long, int, or short type, and if the OID is defined as a byte type, the following exception will be thrown at run time:
[Java] net.sf.hibernate.id.IdentifierGenerationException:this ID generator generates long,integer,short,identity

Proficient in hibernate--hibernate built-in identifiers

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.