Set an attribute’s default value with a sequence number

Environment (JDeveloper, ADF BC)

One of the common use case is to show the sequence Number of an entity’s attribute as a default value when we create a new record. In other words allocate a sequence number at entity row creation time so that the user can see its value and so that this value does not change when the data is saved.
This can be done easily through a customized EntityImpl class which
can read a database sequence and assign the value to the one of entity’s attributes.
The steps for doing this are:

  • Firstly,  set the type of your attribute to Number not to DBSequence. The DBSequence type is used when your attribute is set by a DB trigger, which is not our case. In our case we have a DB sequence but we don’t have a trigger to set a table column by before insert trigger.
  • Adding the following method to your EntityImpl class in my example CustomerEntityImpl
    protected Number nextVal(String sequenceName) {
    SequenceImpl s = new SequenceImpl(sequenceName, getDBTransaction());
    return s.getSequenceNumber();
  • you will need to explicitly import oracle.jbo.server.SequenceImpl.
  • To call this method and make the sequence value as a default value for your attribute, you have two options, only apply one of them:
  1. You can call it from the create() method in the CustomerEntityImpl class  to set the value of your attribute in my example the CustomerId:
    protected void create(AttributeList attributeList) {
    //Customer_Seq is the sequence name in DB
  2. Go back to your entity object, click on the attribute dialog, choose your attribute (as mentioned before, in my case CustomerId), click edit selected attribute (pencil picture at the top), set ValueType to Expression and enter “adf.object.nextVal(“Customer_seq”);”


Filed under ADF

7 responses to “Set an attribute’s default value with a sequence number

  1. Lamya Omar

    That was helpful post , Thank you.

  2. KT

    Very Very Useful…thanks a lot for your sharing

  3. superamh

    can i add FacesMessage after upadate

  4. superamh

    why? how i can do it

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s