Thursday, August 11, 2011

ADF generate primary key using DBSequence and display to user

In this post, I am going to demonstrate a way of generating and displaying a Oracle DB sequence and show that to user in a Oracle ADF page.

Step 1: Create a DB sequence in the database. For example let us consider order_sequence as the DB sequence

Step 2: In the ADF application, under the model project create an entity object for the table which has the primary key (for example, order_id) for which the DB sequence created above will be used for data insertion

Step 3: Open the Entity Object, go to Java tab and click on the 'Edit' button to auto-generate the entity object java class
              a. Select 'Generate Entity Object Class' check box
              b. Under the 'Include' grouping select 'Create Method' in additions to the default selected 'Accessors' option

Step 4: Now, click on the hyperlink which shows the auto-generated entity object java class and add the following java code under the create() method that is generated

super.create(attributeList);
SequenceImpl s = new SequenceImpl("ORDER_SEQUENCE", getDBTransaction());
super.create(attributeList);
Object obj = s.getSequenceNumber();
this.setOrderId((Number)obj);

Now, drag and drop the view object from the ADF data controls as a form with input text. Everytime, the form is invoked, a new sequence number will be generated from database and will be displayed in the UI.

I have illustrated another more sophisticated way of DB sequence generation here which has couple of advantages over the aforesaid method.

1 comment: