Thursday, January 19, 2012

ADF 11G : Generate Primary Key from Sequence Number

There is Multiple Way to Handle this:
First::
-----
Step-1: Create Sequence
CREATE SEQUENCE EMPSEQ MINVALUE 50 MAXVALUE 1000 INCREMENT BY 1 START WITH 50 NOCACHE NOORDER NOCYCLE ;

Step-2: Create Trigger
CREATE OR REPLACE TRIGGER emp_generate_pkBEFORE INSERTON empFOR EACH ROWBEGINSELECT empseq.nextvalINTO :new.empnumFROM dual;END;

Step-3: Make PK data_type as DBSequenec, Update-Never

*************************************************************
SECOND ::

Step-1: Create Sequence
CREATE SEQUENCE DEPT_ID_SEQ MINVALUE 50 MAXVALUE 100 INCREMENT BY 1 START WITH 70 CACHE 20 NOORDEROCYCLE ;

Step-2: Put the below code inside the EOImpl.java

XXEOImpl.javaOverride- initDefaults()--afetr- super()

@Override
protected void initDefaults() {

super.initDefaults();
SequenceImpl seq=new SequenceImpl("DEPT_ID_SEQ",getDBTransaction()); populateAttributeAsChanged(DEPTID, seq.getSequenceNumber());

System.out.println(seq.getSequenceNumber());

}

*************************************************************
THIRD ::

CREATE Sequence:

CREATE SEQUENCE STUDENT_SEQ MINVALUE 50 MAXVALUE 1000 INCREMENT BY 1 START WITH 50 NOCACHE NOORDER NOCYCLE;


Put the below code inside XXEOImpl.java

    protected void create(AttributeList attributeList) {
        super.create(attributeList);
        setRoll(nextVal("STUDENT_SEQ").toString());    }

    protected oracle.jbo.domain.Number nextVal(String sequenceName) {
    SequenceImpl s = new SequenceImpl(sequenceName, getDBTransaction());
    return s.getSequenceNumber();
    }

**************************************************************
FOURTH::

Directly set the below mentioned expression for the attribute of EO



(new oracle.jbo.server.SequenceImpl("Student_Seq",adf.object.getDBTransaction())).getSequenceNumber()

ss







***********************************************************************
FIFTH:

Set it directly at DB layer