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

7.6 Adding Calculated and Transient Attributes to an Entity-Based View Object

In addition to having attributes that map to underlying entity objects, your view objects can include calculated attributes that don't map to any entity object attribute value. The two kinds of calculated attributes are known as:

This section explains how to add both kinds, first illustrating how to add a SQL-calculated LastCommaFirst attribute and then a transient-calculated attribute named FirstDotLast to the StaffList view object. Finally, you'll see that a view object can include an entity-mapped attribute which itself is a transient attribute at the entity object level just to ensure that all of the supported combinations are clear.

7.6.1 How to Add a SQL-Calculated Attribute

To add a SQL-calculated attribute to an entity-based view object:

  1. Open the Attributes page in the View Object Editor and click New.

  2. Enter a name for the attribute, such as LastCommaFirst.

  3. Set the Java Attribute Type to an appropriate value, like String.

  4. Check the Mapped to Column of SQL checkbox.

  5. Provide a SQL expression in the Expression field like LAST_NAME||', '||FIRST_NAME

  6. Consider changing the SQL column alias to match the name of the attribute

  7. Verify the database Query Column Type and adjust the length (or precision/scale) as appropriate.

  8. Click OK to create the attribute.

Figure 7-18 Adding a New SQL-Calculated Attribute

Image of New View Object Attribute dialog

7.6.2 What Happens When You Add a SQL-Calculated Attribute

When you add a SQL-calculated attribute and finish the View Object Editor, JDeveloper updates the XML component definition for the view object to reflect the new attribute. Whereas an entity-mapped attribute like LastName looks like this in the XML, inheriting most of it properties from the underlying entity attribute to which it is mapped:

<ViewAttribute
   Name="LastName"
   IsNotNull="true"
   EntityAttrName="LastName"
   EntityUsage="User1"
   AliasName="LAST_NAME" >
</ViewAttribute>

in contrast, a SQL-calculated attribute's ViewAttribute tag looks like the following. As expected, it has no EntityUsage or EntityAttrName property, and includes datatype information along with the SQL expression:

<ViewAttribute
   Name="LastCommaFirst"
   IsUpdatable="false"
   IsPersistent="false"
   Precision="62"
   Type="java.lang.String"
   ColumnType="VARCHAR2"
   AliasName="FULL_NAME"
   Expression="LAST_NAME||&#39;, &#39;||FIRST_NAME"
   SQLType="VARCHAR" >
</ViewAttribute>

Note:

The &#39; is the XML character reference for the apostrophe, referencing it by its numerical ASCII code of 39 (decimal). Other characters in literal text that require similar construction in XML are the less-than, greater-than, and ampersand characters.

7.6.3 How to Add a Transient Attribute

To add a transient attribute to an entity-based view object:

  1. Open the Attributes page in the View Object Editor and click New.

  2. Enter a name for the attribute, like FirstDotLast.

  3. Set the Java Attribute Type to String.

  4. Leave the Mapped to Column of SQL checkbox unchecked.

  5. Click OK to create the attribute.

Figure 7-19 Adding a New Transient Attribute

Image of New View Object Attribute dialog

7.6.3.1 Adding an Entity-Mapped Transient Attribute to a View Object

To add a transient entity object attribute to an entity-based view object, first ensure that you have an entity usage for the entity on the Entity Objects page of the View Object Editor. Then go to Attributes page and the desired attribute from the Available list into the Selected list. Using these steps, you can add the FullName calculated attribute from the User entity object to the StaffList view object.

If you use the Business Components Browser to test the SRService data model after adding these three attributes to the StaffList view object lists, you can see their effect as shown in Figure 7-20:

Figure 7-20 StaffList View Object with Three Kinds of Calculated Attributes

Image of StaffList view object in tester

7.6.4 What Happens When You Add a Transient Attribute

When you add a transient attribute and finish the View Object Editor, JDeveloper updates the XML component definition for the view object to reflect the new attribute. A transient attribute's ViewAttribute tag in the XML is similar to the SQL-calculated one, but lacks an Expression property.

7.6.5 Adding Java Code in the View Row 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, 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 view object, to make it a calculated transient attribute you need to:

  • Enable a custom view row class on the Java page of the View 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 enabling the generation of the StaffListRowImpl.java view row class, the Java code to return its calculated value would reside in the getLastCommaFirst() method like this:

// In StaffListRowImpl.java
public String getFirstDotLast() {
  // Commented out this original line since we're not storing the value
  // return (String) getAttributeInternal(FIRSTDOTLAST);
  return getFirstName().substring(0,1)+". "+getLastName();
}

Note:

In Section 26.8, "Implementing Automatic Attribute Recalculation", you'll learn a coding technique to cause calculated attributes at the entity row level to be re-calculated when one of the attribute values on which they depend is modified. You could adopt a very similar strategy at the view row level to cause automatic recalculation of calculated view object attributes, too.

7.6.6 What You May Need to Know About Transient Attributes

The view object includes the SQL expression for your SQL-calculated attribute in the SELECT list of its query at runtime. The database is the one that evaluates the expression and it returns the result as the value of that column in the query. The value gets reevaluated each time you execute the query.