2010年11月14日星期日

Sequencing in TopLink Mapping and Sequencing in ADF BC

This post will explain using sequencing as primary key in TopLink Mapping and ADF BC’s mapping specifically.

Section A:

Sequencing in TopLink. There are two options to implement the sequencing in toplink.  The applied technology and attributes of the selected technology can be set at the mapping level by double-clicking the map in the structure.

sequence-config

  • using TABLE sequencing : the sequence value will be queried from a table, the table’s name can be the default ‘SEQUENCE’ when using the ‘Default Sequence Table’ option, or the user defined table when using the ‘Custom Sequence table ’ options. In this option, all the objects in the system share the same sequence table.
  • using oracle native sequencing. The sequence value will be queried from a sequence in the database. In this option, every object in the system can have its own coordinated sequence.  The Preallocation Size of the sequence should be the same as the ‘increase by’ parameter of the sequence in the database. Since there will be different sequences for objects and the increase-by parameter of the sequence is usually set to 1, it’s a good practice to set the Preallocation size to 1 instead of the default value-50.

I will illustrate the option 2 from here below.

Step 1. Create the Sequence in the database.

Step 2. Open the object’s mapping descriptor and select the Sequencing tab.

Step 3. Set the sequence’s name in the database. The table and its column that uses the sequence to generate its values.

sequence-config-2

Save the change, and now when insert a new object, the object will get the Id from the sequence.

Section B:

When using ADF BC, it’s very necessary to set the Id column (primary key)’s value of a table to be generated by a sequence in the database.

Step 1. Create the sequence;

Step 2. Create an trigger on the table to set the value of the ID attribute of the object by query next value from the sequence; [This is the very difference between sequencing in TopLink and Entity Object]

Step 3. In the Entity Object’s configuration file, set the attribute’s Type to DBSequence.

When choosing the type to DBSequence,  a ‘Sequence’ tag displays. Set the sequence name created in the Step 1 and set a Start number which should be greater than 0.  (It’s really wired that the ‘Sequence’ tab doesn’t show up sometimes)

Also, the Updatable property of the attribute should be set as ‘Never’. As below:

 sequencing-eo

Save all the changes.

When Inserting the record/object, an sequence value is set to the Id column.

NOTE:

The previously generated VO’s code will not change coordinated when the EO’s xml changes. So please change the ID to DBSequence before generating the VO code.