Many modern data-driven applications use automatic sequence generators to create primary keys for new database records. OFBiz provides a handy tool to acquire the next unique sequence identifier for any entity using the GenericDelegator
object's sequence generator methods.
For example, to add a record with a unique primary key to the Recipe
table, we could use the OFBiz sequence generator as shown in the following code snippet to find the next sequenceId
for the Recipe
entity, and then add a GenericValue
with that sequenceId
set as the primary key:
String sequenceId = delegator.getNextSeqId("Recipe"); GenericValue genericValue = delegator.makeValue("Recipe", UtilMisc.toMap("recipeId",sequenceId)); // Or we could do it like this: GenericValue genericValue = delegator.makeValue("Recipe", UtilMisc.toMap("recipeId",delegator.getNextSeqId("Recipe"))); // Finish adding field values to genericValue and then store // it in the database
OFBiz keeps track of sequence identifiers, and each time one is requested, it calculates a unique sequence value based on the name of the sequence passed. An easy way to keep sequences unique for entities is to pass the name of the entity as the sequence name. In that way, all calls to the same entity will use the same sequence.
Note: for performance reasons, all primary keys for OFBiz data model entities default to twenty text characters or less. Using the GenericDelegator
object's getNextSeqId
method with a target entity ensures that you get a properly formatted Java data types (String or otherwise) for use as a primary key for that entity.
You may use the OFBiz sequence generator to return guaranteed unique values, including numeric values, for any purpose. For example, to create a Java Long
unique sequence value, use a GenericDelegator
method call similar to the following:
Long someSequenceNumber = getNextSeqIdLong('SequenceName'),
Where SequenceName
is any sequence name. If the named sequence does not exist, it will be created automatically.
3.144.15.154