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

5.2 Creating a Simple, Read-Only View Object

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.

5.2.1 How to Create a Read-Only View Object

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.

Figure 5-2 Defining the Package and Component Name for a New View Object

Image of Step1 of the Create View Object wizard

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.

Figure 5-3 Defining the SQL Query for a Read-Only View Object

Image of Step 2 of the Create View Object wizard

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.

Figure 5-4 Wizard Creates Default Java-Friendly Attribute Names for Each Column in Select List

Image of step 4 of the Create View Object wizard

Click Finish at this point to create the view object.

5.2.2 What Happens When You Create a Read-Only 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 class UsersImpl.java and/or a custom view row class UsersRowImpl.java class.

5.2.3 What You May Need to Know About View Objects

Typically you create one view object for each SQL query your application will perform.

5.2.3.1 Editing an Existing View Object Definition

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.

5.2.3.2 Working with Queries That Include SQL Expressions

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

5.2.3.3 Controlling the Length, Precision, and Scale of View Object Attributes

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.

Figure 5-5 Editing the Settings for a View Object Attribute

Image of View Object Editor for a view object attribute