How hibernate grows with IDs in Oracle (cont.)

Source: Internet
Author: User

Citation Link: http://blog.csdn.net/w183705952/article/details/7367272

The second: The growth strategy for setting the ID is native, but you need to create a globally used sequence named Hibernate_sequence (the name is the default sequence name of Hibernate, do not create an error), Then, when generating the ID for each table, use a trigger to get hibernate_sequence. Currval as the ID of the new record, the specific Oracle database script and hibernate configuration file are as follows:

[1]oracle creation script for data tables:
Java code

CREATE TABLE Staff (

ID number (0) DEFAULT ' 0 ' not NULL,

NAME VARCHAR2 (255) is not NULL,

Age Number (3,0) is not NULL,

BIRTHDAY DATE not NULL,

SALARY Number (ten,2) is not NULL,

Levelness FLOAT not NULL,

Createtime TIMESTAMP not NULL,

ENABLE CHAR (2) DEFAULT ' Y ' not NULL,

STATUS VARCHAR2 (+) not NULL,

department_id number (0)

);

ALTER TABLE staff ADD CONSTRAINT primary_1 PRIMARY KEY (ID) ENABLE;

ALTER TABLE staff ADD CONSTRAINT staff_ibfk_0 FOREIGN KEY (department_id) REFERENCES DEPARTMENT (ID) ENABLE;

ALTER TABLE staff ADD CONSTRAINT uk_staff_1 UNIQUE (NAME);

CREATE INDEX idx_staff_status on Staff (STATUS);

CREATE SEQUENCE hibernate_sequence MINVALUE 90000 MAXVALUE 999999999999999999999999 INCREMENT by 1 nocycle;

CREATE OR REPLACE TRIGGER STAFF_ID_TRG before INSERT on staff

For each ROW

BEGIN

IF INSERTING AND:NEW.ID is NULL then

SELECT hibernate_sequence. Currval INTO:NEW.ID from DUAL;

END IF;

END;

The staff table was created, but instead of creating the corresponding primary key sequence for the staff, a sequence named Hibernate_sequence was created, and a trigger STAFF_ID_TRG was created, and when the insert operation was performed, the Hibernate performs a hibernate_sequence first. Nextval, so you only need to get hibernate_sequence in the trigger. Currval as the ID of the new record.

[Configuration of the 2]hibernate mapping file:
Java code

<?xml version="1.0"?>

<! DOCTYPE hibernate-mapping Public

"-//hibernate/hibernate Mapping DTD 3.0//en"

"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

="Com.liyanframework.demo.domain" >

<class name="Staff" table=" Staff" >

<id name="id" column="id" >

<generator Class="native"/>

</id>

<property name="name" column="name" type="string"/>

<property name= "Age"column="age" type="integer"/>

<property name="Birthday" column="Birthday" type="date"/>

<property name="salary" column="salary" type="Big_decimal"/>

<property name="level" column="levelness" type="float"/>

<property name="createtime" column="Createtime" type="timestamp"/>

<property name="Enable" column="Enable" type="character"/>

<property name="status" column="status" type="string"/>

<many-to-one name="department" column="department_id" class="department"/> /c8>

</class>

In the hibernate mapping file, the generation policy selection for the ID native,hibernate generates the ID of the new record based on the trigger of your database.

PS: To compare two approaches, the second is hibernate in the code that implements the trigger functionality in Oracle. For different situations, choose not to understand the practice. If the new system, the new Oracle database, it is recommended to use the first approach, simple, easy to migrate to other support auto-growth of the database; If the old system needs to convert other databases to Oracle, then the second, using native, can not change the configuration file, Compatible with automatically growing databases such as Oracle and MySQL.

How hibernate grows with IDs in Oracle (cont.)

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.