Home » General

Using database sequences as primary key with ADF Business Components

Written By: Pascal Alma on December 28, 2006 One Comment

Last week I was going over some code I used at a former project. In this project we used ADF Business Components (10.1.2) as business layer (it also works for 10.1.3). All our database tables had an ID column (number) as primary key and each table had his own sequence to be used to create the ID.


We decided to implement the use of the sequence in our Entity object. We created a base class 'BaseEntityImpl' with the following code:

JAVA:
  1. /**
  2.   * Overriden method
  3.   * This version sets the sequence of the primary key if the name of
  4.   * the sequence is provided with the entity object's attribute.
  5.   *
  6.   * @param nameValuePair
  7.   */
  8. protected void create(AttributeList nameValuePair)
  9. {
  10.   super.create(nameValuePair);
  11.  
  12.   AttributeDef ad = getSinglePrimaryKey();
  13.   if (ad != null)
  14.   {
  15.     // Now check if an sequence name is provided. If so, take this
  16.     // sequence value
  17.     String seqName = (String)ad.getProperty(SEQUENCE_PROPERTY);
  18.     setAttributeFromSequence(ad.getIndex(), seqName);
  19.   }
  20. }
  21.  
  22. /**
  23.   * Gets the attribute defintion that is defined as primary key for
  24.   * the entity.
  25.   * WARNING: Will return the first attribute it encounters when a multi-field
  26.   * primary key is defined!
  27.   * @return AttributeDefinition that is marked as primary key
  28.   */
  29. private AttributeDef getSinglePrimaryKey()
  30. {
  31.   final String method = "getSinglePrimaryKey()";
  32.   _log.debug(method);
  33.  
  34.   AttributeDef result = null;
  35.   AttributeDef[] adArray = getStructureDef().getAttributeDefs();
  36.   for (int i = 0; i <adArray.length; i++)
  37.   {
  38.     if (adArray[i].isPrimaryKey())
  39.     {
  40.       result = adArray[i];
  41.       break;
  42.     }
  43.   }
  44.   return result;
  45. }
  46.  
  47. /**
  48. * Use incoming sequence name to generate value for attribute
  49. *
  50. * @param sequence
  51. * @param index
  52. */
  53. private void setAttributeFromSequence(int index, String sequence)
  54. {
  55.   final String method = "setAttributeFromSequence(int index, String sequence)";
  56.   _log.debug(method);
  57.  
  58.   if (!StringUtils.isEmpty(sequence))
  59.   {
  60.     SequenceImpl seq = new SequenceImpl(sequence, getDBTransaction());
  61.     setAttributeInternal(index, new DBSequence(seq.getSequenceNumber()));
  62.   }
  63. }

When we create a business component with the corresponding wizard we used the following settings:

blog_2_fig_3.jpg

And for the primary key column we set the following properties:

wizard 1
wizard 2

So now every time a new row is created in the Entity Object the sequence is used to generate the new ID for that row.

Tags:

Digg this!Add to del.icio.us!Stumble this!Add to Techorati!Share on Facebook!Seed Newsvine!Reddit!Add to Yahoo!

One Response to “Using database sequences as primary key with ADF Business Components”

  1. Pascal’s Blog » Oracle ADF Basics: Duplicating an existing row said:

    [...] step. 2. The employeeId is now always set to -1, you will need to something more fancy here (see this post for example). After we have added this method and it compiles, we have to do one more thing and [...]

Copyright © 2009 Pascal’s Blog, All rights reserved.| Powered by WordPress