Introduction to using Oraclesequence 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:

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:

I. There are two types of auto-incrementing self-segments for tables. One is that different tables use their Sequence. The method is as follows:

1. Create a sequence in Oracle sequence first

Create sequence seq_id
Minvalue 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 dual
Hibernate: 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_sequence
Minvalue 0
Max value 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.

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.