Oracle MSSQL MySQL hibernate primary key generator

Source: Internet
Author: User

ORACLE:

 

 <id name="id" column="ID">       
<generator class="sequence">
<param name="sequence">DEPARTMENT_ID_SEQ</param>
</generator>
</id>

 

 

MySQL:

MSSQL:

 

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

 

 

Compatible with Oracle and MSSQL hibernate:

 

 <id name="acid" type="java.lang.Long">
<column name="ACID" precision="20" scale="0" />
<generator class="native">
<param name="sequence">SEQ_AC_CABINET</param>
</generator>
</id>

 

 

 

In fact, the principle is very simple. First, Hibernate will find it based on the parameters in Param. If not, the sequence number will be produced by default.

 

 

The increment primary key increments in numerical order. The implementation mechanism of this method is to maintain a variable in the current application instance to save the current maximum value, and then add 1 as the primary key each time the primary key needs to be generated. This method may cause a problem: if multiple instances access the same database, different instances may generate the same primary key because each instance maintains its primary key status, this causes duplicate primary key exceptions. Therefore, if the same database has multiple instances to access, this method must be avoided. Identity uses the primary key generation mechanism provided by the database. Such as the primary key generation mechanism in DB2, SQL Server, and MySQL. Sequence uses the sequence mechanism provided by the database to generate a primary key. For example, sequence in oralce. Native is determined by hibernate based on the underlying database and uses one of identity, Hilo, and sequence as the master node.

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.