Oracle® Application Development Framework Developer's Guide For Forms/4GL Developers 10g (10.1.3.1.0) Part Number B25947-01 |
|
|
View PDF |
View objects can be used for reading data as well as updating data. This chapter focuses on working with read-only data using view objects. In Chapter 7, "Building an Updatable Data Model With Entity-Based View Objects", you'll learn how to create view objects that can handle updating data.
To create a view object, use the Create View Object wizard. The wizard is available from the New Gallery in the Business Tier > ADF Business Components category. If it's the first component you're creating in the project, the Initialize Business Components Project dialog appears to allow you to select a database connection. These examples assume that you are working with a connection named SRDemo
for the SRDEMO
schema.
As shown in Figure 5-2, provide a package name, a view object name, and indicate that you want this view object to manage data with read-only access. The figure illustrates creating a view object named Users
in the devguide.examples
package.
In step 2 of the wizard (the SQL Statement page), paste in any valid SQL statement into the Query Statement box or click Query Builder to use the interactive query builder. Figure 5-3 shows a query to retrieve a few columns of user information from the USERS
table ordered by EMAIL
.
Note: If you see an Entity Objects page instead of the SQL Statement page shown here, go back to step 1 and ensure that you've selected Read-only Access. |
Since the query does not reference any bind variables, you can skip step 3 Bind Variables page for now. In Section 5.9, "Using Named Bind Variables", you'll add a bind variable and see how to work with it in the query.
In addition to the SQL query information, a view object captures information about the names and datatypes of each expression in its query's SELECT list. As you'll see in Section 5.6, "Working Programmatically with View Object Query Results", you can use these view object attribute names to access the data from any row in the view object's result set by name. You could directly use the SQL column names as attribute names in Java, but typically the SQL names are all uppercase and often comprised of multiple, underscore-separated words. The view object wizard converts these SQL-friendly names to Java-friendly ones.
In step 4 on the Attribute Mappings page, as shown in Figure 5-4 you can see how the SELECT list column names correspond to the more Java-friendly view object attribute names that the wizard has created by default. Each part of an underscore-separated column name like SOME_COLUMN_NAME
is turned into a capitalized word in the attribute name like SomeColumnName
. While the view object attribute names correspond to the underlying query columns in the SELECT list, the attribute names at the view object level need not match necessarily. You can later rename the view object attributes to any names that might be more appropriate without changing the underlying query.
Note: You'll see throughout the ADF Business Components wizards and editors, that the default convention is to use "CamelCapped" attribute names, beginning with a capital letter and using upper-case letters in the middle of the name to improve readability when the name comprises multiple words. |
Click Finish at this point to create the view object.
When you create a view object, JDeveloper first describes the query to infer the following from the columns in the SELECT list:
The Java-friendly view attribute names (e.g. USER_ID
-> UserId
)
The SQL and Java data types of each attribute
JDeveloper then creates the XML component definition file that represents the view objects's declarative settings and saves it in the directory that corresponds to the name of its package. In the example above, the view object was named Users
in the devguide.examples
package, so that the XML file created will be ./devguide/examples/Users.xml
under the project's source path. This XML file contains the SQL query you entered, along with the names, datatypes, and other properties of each attribute. If you're curious to see its contents, you can see the XML file for the view object by selecting the view object in the Application Navigator and looking in the corresponding Sources folder in the Structure window. Double-clicking the Users.xml
node will open the XML in an editor so that you can inspect it.
Note: If your IDE-level Business Components Java generation preferences so indicate, the wizard may also create an optional custom view object classUsersImpl.java and/or a custom view row class UsersRowImpl.java class. |
Typically you create one view object for each SQL query your application will perform.
After you've created a view object, you can edit any of its settings by using the View Object Editor. Choose the Edit menu option on the context menu in the Application Navigator, or double-click the view object, to launch the dialog. By opening the different panels of the editor, you can adjust the SQL query, change the attribute names, add named bind variables, add UI controls hints, control Java generation options, and other settings that are described in later chapters.
If your SQL query includes a calculated expression like this:
select USER_ID, EMAIL, SUBSTR(FIRST_NAME,1,1)||'. '||LAST_NAME from USERS order by EMAIL
use a SQL alias to assist the Create View Object wizard in naming the column with a Java-friendly name:
select USER_ID, EMAIL, SUBSTR(FIRST_NAME,1,1)||'. '||LAST_NAME AS USER_SHORT_NAME from USERS order by EMAIL
As shown in Figure 5-5, by selecting a particular attribute name in the View Object Editor, you can see and change the values of the declarative settings that control its runtime behavior. One important property is the Type in the Query Column section. This property records the SQL type of the column, including the length information for VARCHAR2
columns and the precision/scale information for NUMBER columns. The JDeveloper editors try to infer the type of the column automatically, but for some SQL expressions the inferred value might be VARCHAR2(255)
. You can update the Type value for such attributes to reflect the correct length if you know it. For example, VARCHAR2(30)
which shows as the Type for the FirstName
attribute in Figure 5-5 means that it has a maximum length of 30 characters. For a NUMBER
column, you would indicate a Type of NUMBER(7,2)
for an attribute that you want to have a precision of 7 digits and a scale of 2 digits after the decimal.