Oracle® Application Development Framework Developer's Guide For Forms/4GL Developers 10g (10.1.3.1.0) Part Number B25947-01 |
|
|
View PDF |
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:
SQL-calculated attributes, when their value is retrieved as an expression in the SQL query's SELECT list
Transient attributes, when their value is not retrieved as part of the query
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.
To add a SQL-calculated attribute to an entity-based view object:
Open the Attributes page in the View Object Editor and click New.
Enter a name for the attribute, such as LastCommaFirst
.
Set the Java Attribute Type to an appropriate value, like String
.
Check the Mapped to Column of SQL checkbox.
Provide a SQL expression in the Expression field like LAST_NAME||', '||FIRST_NAME
Consider changing the SQL column alias to match the name of the attribute
Verify the database Query Column Type and adjust the length (or precision/scale) as appropriate.
Click OK to create the 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||', '||FIRST_NAME" SQLType="VARCHAR" > </ViewAttribute>
Note: The' 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. |
To add a transient attribute to an entity-based view object:
Open the Attributes page in the View Object Editor and click New.
Enter a name for the attribute, like FirstDotLast
.
Set the Java Attribute Type to String
.
Leave the Mapped to Column of SQL checkbox unchecked.
Click OK to create the attribute.
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:
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.
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. |
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.