Friday 17 June 2016

Programmatically handling the Entity Objects in Oracle ADF

Entity Object is n object oriented representation of data base objects like table. It represents a table or view. It encapsulates the properties of a Row like the validation rules, security and presenting values. It is used to perform all the DML operations.
Files related to Entity Object
  1. Entity definition XML file- Defines the structure of  the entity object. It acts as a template for the creation of entity objects.
  2. Entity Object class - It represents an entity instance. It wraps the business logic for a row like having default values at the time of row creation, lock() method to lock the row and doDML() method to perform DML operations.
  3. Entity Collection Class- Caches the rows particular to a DB transaction.

Use the below code to create the entity rows. You can have it in the Application module implementation classes.
Creating a new entity row
public void createEntityObject(){
EntityDefImpl sampleEODef=sampleEOImpl.getDefinitionObject();//Here get the definition object using the EOImpl like EntityImpl
SampleEOImpl newEOInstance=sampleEODef.createInstance2(this.getDBTransaction(), null);
//The entity status is STATUS_NEW now and qualifies to participate in a transaction.
//Use the newEOInstance object to set the attribute values.
}
Finding an entity row and updating it
public void updateEntity(String primaryKey){
//Key creation by passing the primary key
Key key= SampleEOImpl.createPrimaryKey(primaryKey);
//get the definition object
//The row status here is STATUS_UNMODIFIED now
EntityEOImpl rowTobeUpdated =
(DepartmentEOImpl)SampleEOImpl.
getDefinitionObject().
findByPrimaryKey(getDBTransaction(), key);
rowTobeUpdated .setAttributes()
//The row status here is STATUS_MODIFIED and qualifies for row the transaction.
}
Removing an entity row
The below method takes a key attribute value in a row as input and performs find and delete operations.
public void removeEntity(Integer key){
            //Key creation by passing the key
            Key key= SampleEOImpl.createPrimaryKey(key);
            EntityEOImpl rowTobeDelted =
             (SampleEOImpl)SampleEOImpl.
             getDefinitionObject().
            findByPrimaryKey(getDBTransaction(), key);
           //Row status is STATUS_UNMODIFIED
           rowTobeDelted.remove();
          //Row status is STATUS_DELETED
 }

Committing a transaction
     public void commitTransaction(){
       getDBTransaction().commit();

      }

No comments:

Post a Comment