There are various ways of generating sequence for attributes in a EO. Just have a look at below approaches.
  1. Groovy Expression
  2. DB Trigger
  3. Overriding create() in EOImpl

Groovy Expression

This is most easier and effective way of doing using the so called Groovy Expression. Below are the steps for the same.
  • Create EO based on the table and make sure the attribute type(whose value to be from sequence) is set to Number.
  • In the default value select Expression and enter below code(with your sequnce name). ADF takes care of creating the new value from sequence and assign the same to the attribute.
(new oracle.jbo.server.SequenceImpl("YourSequenceName", adf.object.getDBTransaction())).getSequenceNumber()

DB Trigger

The conventional way is to use trigger to generate the sequence number right before inserting a row into the DB. Below are the steps to implement the same
  • Create BEFORE INSERT TRIGGER on table as shown below.
  •   CREATE OR REPLACE TRIGGER sequence_trigger_test_trg
      BEFORE INSERT ON sequence_trigger_tbl
      FOR EACH ROW
      BEGIN
          SELECT sequence_s.nextval INTO :new.primary_key_id FROM dual;
      END;
    
  • Create EO based on the table.
  • Make sure the attribute Type is set to DBSequence and check Refresh On Insert. This property enables the ADF to refresh the attribute after the row is committed.

Override create() in EOImpl

There is also one programmatic way of doing it by overriding create() method in the EOImpl. Below are the steps to do the same.
  • Create EO based on the table and make sure the attribute type(whose value to be from sequence) is set to Number
  • Create a custom property with name SequenceName and set it's value to the sequence name from where you want to set this attribute. Using custom property we can avoid hardcoding the sequence name in the create() method. So create() method can be used in a generic way.
  • Generate EOImpl and override create(AttributeList) method as shown below. This method is a generic method and generate sequence for any attribute provided we set the custom property XXSequenceName for the attribute(s). So if you are using sequences frequently for most of the attributes, then create your custom EOImpl and override create() method as shown below and while generating your EO, simply base your EOImpl on the custom EO rather than EntityImpl alternatively write the below code in you EOImpl.

  •    import oracle.jbo.AttributeDef;
       import oracle.jbo.AttributeList;
       import oracle.jbo.server.EntityImpl;
       import oracle.jbo.server.SequenceImpl;    
       protected void create(AttributeList attributeList) {
            super.create(attributeList);
            for (AttributeDef def : getEntityDef().getAttributeDefs()) {
                String sequenceName = (String)def.getProperty("XXSequenceName");
                if (sequenceName != null) {
                    SequenceImpl s = new SequenceImpl(sequenceName,getDBTransaction());
                setAttribute(def.getIndex(),s.getSequenceNumber());
                }
            }
        }
    
If you have more than one table whose primary key is based on the sequence it's better to use third approach. We just need to set the custom property to the sequence and our underlying create method do the rest for us.
0

Add a comment

Loading