Saturday 27 July 2013

Set DB Sequence number as default value to entity object

In this post i will explain you about setting default value to entity object.
It is very common requirement, where you need to set DB sequence number to entity object. I am using Employees table and Employees_seq (sequence) in this example.

There are two ways of setting default value
1. Groovy Expression    2. Entity Object Java class

1. Groovy Expression
Create the Employees entity object and go to attributes tab in that select employee_id and then go to the default value and select the expression paste the below expression in the text box.

(new oracle.jbo.server.SequenceImpl("EMPLOYEES_SEQ",adf.object.getDBTransaction()))
.getSequenceNumber()


2. Entity Object java class

Create the entity object java class and include the create method in that. Then use the below code in create  method.

        SequenceImpl seq=new SequenceImpl("EMPLOYEES_SEQ",getDBTransaction());
        setEmployeeId(seq.getSequenceNumber());

Final step is to create Employee view and Application module, run your AM, You vill see the result when ever u add new record.





3 comments:

  1. HI Nitish.. Thank You for the useful info. Can you please let me know , how to create tables from existing Entity Objects (.xml files) in ADF (Using JDeveloper)

    ReplyDelete
    Replies
    1. Hi Shiva,
      Here is the post to create DB tables from entity object

      http://nitesh-java.blogspot.in/2013/08/creating-db-table-form-entity-object.html

      Thanks
      Nitesh

      Delete
  2. Hi,

    I copy pasted the below expression
    (new oracle.jbo.server.SequenceImpl("EMPLOYEES_SEQ",adf.object.getDBTransaction()))
    .getSequenceNumber()

    but still am getting an error like

    "(oracle.jbo.domain.DataCreationException) JBO-25009: Cannot create an object of type:oracle.jbo.domain.Number from type:java.lang.String with value:(new oracle.jbo.server.SequenceImpl("EMPLOYEES_SEQ",adf.object.getDBTransaction())).getSequenceNumber()"

    ReplyDelete