Introduction to Oracle sequence in hibernate

Source: Internet
Author: User
Tags import database

I. There are two types of auto-increment self-segments for tables. One is that different tables use their respective sequence. The method is as follows:
1. Create a sequence in Oracle sequence first

Create sequence seq_idminvalue 1 start with 1 increment by 1 cache 20;

2. Configure in your HBM. xml

Seq_id

In this way, Hibernate will automatically generate the following statement when inserting data again:

Hibernate: Select seq_id.nextval from dualhibernate: insert into yxjk. t_yxjk_whrytxl (xm0000, zw0000, lxdh00, sjhm00, dzyj00, ip0000, id0000) values (?, ?, ?, ?, ?, ?, ?)

Automatically generate the next Sequence Value and insert the object into the table. When using it, you must note that hibernate must require Shor, long, or integer for the primary key of sequence.

Another method is to use the public sequence.

In this case, you can choose not to specify the sequence used by the table. The corresponding HBM. XML content is:

Create a common sequence object named hibernate_sequence.

Create sequence hibernate_sequenceminvalue 0 maxvalue 99999999 start with 10000 increment by 1;

3. Notes

If the database is transplanted from other databases such as sqlserver, the starting value of the sequence should be greater than the largest ID value in the current table. Otherwise, an error occurs, because sequence does not maintain whether it is the same as the existing value before the import database.

1. sequence: used for Oracle databases

Sequence name

2. Native: used across databases, produced by underlying dialects.
Default. sequence is hibernate_sequence

Note: When native is used, Hibernate searches for the hibernate_sequence sequence in Oracle by default. If the sequence does not exist in Oracle, an error is returned when you connect to the Oracle database.

 

 

The <generator> sub-element that must be declared in *. HBM. XML is a Java class name, which is used to generate a unique identifier for the persistence class instance.

 
 
  1. < generator class="sequence"/>

This is a very simple interface; some applications can choose to provide their own specific implementations. Of course, Hibernate provides many built-in implementations. The following are the quick names of some built-in generators:

Increment (incremental)
Used to generate a unique identifier for long, short, or Int type. Data can be used only when no other process inserts data into the same table. Do not use it in a cluster.

Identity
Supports built-in identity fields for DB2, MySQL, ms SQL Server, Sybase, and hypersonicsql. The returned identifier is of the long, short, or Int type.

Sequence)
Use sequence in DB2, PostgreSQL, Oracle, sap db, and mckoi, and generator in InterBase ). The returned identifier is of the long, short, or Int type.

HiLo (high/low)
Use a high/low level algorithm to efficiently generate long, short, or Int type identifiers. Specify a table and field (hibernate_unique_key and next_hi by default) as the source of high value. The identifier generated by the high/low algorithm is unique only in a specific database. Do not use this generator in the connection obtained by using JTA or the connection provided by the user.

Seqhilo (using the high and low bits of the sequence)
Use a high/low level algorithm to efficiently generate long, short, or Int type identifiers and give a database sequence name.

UUID. HEX
Use a 128-bit UUID algorithm to generate string-type identifiers. It is unique in a network (using an IP address ). UUID is encoded as a 32-bit hexadecimal number string.

UUID. String
Use the same UUID algorithm. UUID is encoded as a string consisting of 16 characters long and any ASCII characters. Cannot be used in a PostgreSQL database

Native (local)
Select one of identity, sequence, or HiLo Based on the capabilities of the underlying database.

Assigned (program settings)
Assign an identifier to the object before saving.

Foreign (External Reference)
Use the identifier of another associated object. And <one-to-one>.

  1. < ClassName = "onlyfun. Caterpillar. User" table = "user">
  2. < IDName = "ID" type = "string" unsaved-value = "null">
  3. < ColumnName = "user_id"/>
  4. < GeneratorClass = "UUID. Hex"/>
  5. </ID>
  6. Identity Method
    <Generator class = "Identity"/>
    Identity means that the primary key generation mode of the database is based on the primary key generation mechanism of the database, such as the automatic primary key generation mechanism of SQL Server or MySQL.
    INCREMENT METHOD
    <Generator class = "increment"/>
    When using the increment method, Hibernate sets the primary key in ascending mode. The specific method is to first obtain the maximum value of the primary key of the current record, and then add this value to 1 as the primary key.
    Assigned Mode
    <Generator class = "assigned"/>
    The primary key is generated by the application logic. Before the object is saved, the oId must be explicitly set. The primary key generation requires no hibernate intervention.
    Sequence Mode
    <Generator class = "sequence">
    <Param name = "sequence"> some_sequence </param>
    </Generator>
    This method is applicable to databases that generate primary keys in sequence, such as oracle. In the <generator> child element <Param name = "sequence">, specify the sequence name used to generate the primary key.
    Native Mode
    <Generator class = "native"/>
    Native means that the primary key generation mechanism is decided by hibernate. hibernate uses different database-specific primary key generation methods according to the dialect definition in the configuration file.
    UUID Mode
    <Column name = "ID" SQL-type = "char (32)" not-null = "true"/>
    <Generator class = "UUID. Hex"/>
    Generate a 32-bit hexadecimal string type identifier using the 128-bit UUID Algorithm
Related Article

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.