Tuesday, November 29, 2011

Sequence creation appraoch in oracle ADF Business Component

Hi ,

In post i would like to explain different approach for creating the auto generate sequence number approach in business component.

This is very common use case for any application of oracle ADF to generate sequence number automatically on primary attribute or column.

There are following two way to generate the sequence number

1-Generate the sequence number through application.You can write code in following ways
    i-In Entity Imp class
    ii-Using groovy expression

However this approach has one disadvantage, if data not being saved into table the newly generated sequence will loss.

2-Using Database trigger.

1-Generate the sequence number through application.You can write code in following ways:
i-In Entity Imp Class: Create the Entity class for associated Entity Object and then override the create method and put following code inside that method.

    /**
     * Add attribute defaulting logic in this method.
     * @param attributeList list of attribute names/values to initialize the row
     */
    protected void create(AttributeList attributeList) {
        SequenceImpl sequenceImpl=new SequenceImpl("EMPLOYEES_SEQ",getDBTransaction());
        setEmployeeId(sequenceImpl.getSequenceNumber());
        super.create(attributeList);
    }

In  SequenceImpl constructor you need to pass sequence number and database transaction object.

ii- You can put direct groovy expression into primary key attribute

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


Do not forget to select expression as value type.

2-Using Database trigger: In the second approach require to implement following approach

1-Create the data base trigger into table
2-Define the attribute type as DBSequence

1-First it require to create the trigger into table.While creating trigger it will asked following information
    1-Tigger name
    2-Column Name
    3-Sequence Name 






And it will create trigger.

2-Define the attribute type as DBSequence: Change the property of the primary attribute as a DBSequence.




So when we run the application it will provide or assign some dummy value to primary attribute and when we do actual commit it will get the value from the sequence and assign into primary key




So these are two way to generate sequence number into ADF BC layer.However second approach is much better than first because it does not loss sequence number.


Thanks,
Prateek









No comments:

Post a Comment