Thursday, March 1, 2012

ADF 11g: Get sequence in Groovy

A lot of people use sequences to generate an ID for their records. Since Oracle doesn't have an auto-increment, you need to manually get the next value and insert it into the ID field.


This can be done by triggers, but when you use Business Components, adding a one method and a single line of groovy code is enough. First, we need a method to get the next DB value:

  public Number seqNextVal(String seqName) {
 
    Number seqNextVal;
 
    if(seqName!=null && !seqName.equals("")) {
      SequenceImpl seq = new SequenceImpl(seqName, getDBTransaction());
      seqNextVal = seq.getSequenceNumber();
    }
    else {
      seqNextVal = new Number(0);
    }
 
    return seqNextVal;
  }


In the Id-attribute of your entity, you call this method by entering this expression in the value field:

adf.source.seqNextVal("SEQ_NAME")




Don't forget to check the Expression radio!

1 comment:

  1. Hi

    Good Tip!
    You can also set the Type as DBSequence to generate auto-increment.


    KR

    ReplyDelete