Tuesday, March 26, 2013

Hibernate and Oracle sequence

 For  Hibernate beginners using orcale db doing some examples then they will be confused over mapping file with auto increment . So for my experience I gave sample code as below to avoid 

1. Use Sequence increment in mapping File :

Create Tabele :

CREATE TABLE EMPLOYEE (
  ID          NUMBER        NOT NULL,
  FIRST_NAME  VARCHAR2 (20),
  LAST_NAME   VARCHAR2 (20),
  SALARY      NUMBER        DEFAULT null,
  CONSTRAINT EMPLOYEE_PK
  PRIMARY KEY ( ID ) ) ;

create sequence EMPLOYEE_ID_SEQ; 
 
In Mapping File :

Employee.hbm.xml


           


2. Mapping with Database trigger on sequence


Create table as like above

For trigger :

create sequence EMPLOYEE_ID_SEQ;

CREATE OR REPLACE TRIGGER EMPLOYEE_insert
before insert on EMPLOYEE
for each row
begin
    select EMPLOYEE_ID_SEQ.nextval into :new.id from dual;
end;
/

In Mapping File :

Employee.hbm.xml

No comments:

Post a Comment