Skip Headers
Oracle® Application Development Framework Developer's Guide For Forms/4GL Developers
10g (10.1.3.1.0)

Part Number B25947-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

6.10 Adding Transient and Calculated Attributes to an Entity Object

In addition to having attributes that map to columns in an underlying table, your entity objects can include transient attributes that are value holders or that display values calculated in Java. This section explores a simple example of adding a FullName transient attribute to the Users entity object that calculates its value by concatenating the values of the FirstName and LastName attributes.

6.10.1 How to Add a Transient Attribute

To add a transient attribute to an entity object:

  1. Open the Attributes page in the Entity Object Editor and click the New... button. As shown in Figure 6-22:

  2. Enter a name for the attribute like FullName,

  3. Set the Java Attribute Type like String, and

  4. Deselect the Persistent checkbox

  5. If the value will be calculated, set Updateable to Never

Then click OK to create the attribute.

Figure 6-22 Adding a New Transient Attribute

Adding new transient attribute in New Entity Attribute

6.10.2 What Happens When You Add Transient Attribute

When you add a transient attribute and finish the Entity Object Editor, JDeveloper updates the XML component definition for the entity object to reflect the new attribute. Whereas a persistent entity association looks like this in the XML:

<Attribute
   Name="FirstName"
   IsNotNull="true"
   Precision="30"
   ColumnName="FIRST_NAME"
   Type="java.lang.String"
   ColumnType="VARCHAR2"
   SQLType="VARCHAR"
   TableName="USERS" >
</Attribute>

a transient attribute's Attribute tag looks like this, with no TableName and a ColumnName of $none$:

<Attribute
   Name="FullName"
   IsUpdateable="false"
   IsQueriable="false"
   IsPersistent="false"
   ColumnName="$none$"
   Type="java.lang.String"
   ColumnType="$none$"
   SQLType="VARCHAR" >
</Attribute>

6.10.3 Adding Java Code in the Entity Class to Perform Calculation

A transient attribute is a placeholder for a data value. If you change the Updatable property of the transient attribute to While New or Always, then the end user can enter a value for the attribute. If you want the transient attribute to display a calculated value, then you'll typically leave the Updatable property set to Never and write custom Java code that calculates the value.

After adding a transient attribute to the entity object, to make it a calculated attribute you need to:

  • Enable a custom entity object class on the Java page of the Entity Object Editor, choosing to generate accessor methods

  • Write Java code inside the accessor method for the transient attribute to return the calculated value

For example, after generating the UserImpl.java view row class, the Java code to return its calculated value would reside in the getFullName() method like this:

// Getter method for FullName calculated attribute in UserImpl.java 
public String getFullName() {
  // Commented out original line since we'll always calculate the value
  // return (String)getAttributeInternal(FULLNAME);
      return getFirstName()+" "+getLastName();
}

To ensure that the FullName calculated attribute is reevaluated whenever the LastName or FirstName attributes might be changed by the end user, you can add one line to their respective setter methods to mark FullName as "dirty" whenever either's value is set.

// Setting method for FirstName attribute in UserImpl.java
public void setFirstName(String value) {
  setAttributeInternal(FIRSTNAME, value);
    // Notify any clients that the FullName attribute has changed
    populateAttribute(FULLNAME,null,true, /* send notification */
                                    false, /* markAsChanged     */
                                    false);/* saveCopy          */   
}

and

public void setLastName(String value) {
    setAttributeInternal(LASTNAME, value);
    // Notify any clients that the FullName attribute has changed
    populateAttribute(FULLNAME,null,true, /* send notification */
                                    false, /* markAsChanged     */
                                    false);/* saveCopy          */  
  }