Creating and Finding a View Object Instance in Code

At run time, you can instantiate a View Object from a design-time definition, a complete SQL statement, or SQL query clauses. You can also find and reuse View Object instances. The key methods are:

You call these methods on an Application Module. For an example of a helper routine that returns an Application Module, see Loading an Application Module.

Whether created dynamically with SQL or from a design-time definition, a View Object persists until it, or the Application Module from which it was created, is explicitly deleted (using remove methods). You can use a given View Object definition more than once within an Application Module; the Business Components for Java framework uses View instance names to distinguish between them. For example, given a View Object definition named hr.EmpView, you could define aliases MyEmpView and MyDetailEmpView to represent an unrestricted view and a detail view, respectively.

The View Object methods described in this section are also available from the DBTransaction interface to create anonymous View Objects on the database transaction. Anonymous View Objects do not require an Application Module. An anonymous View Object is a View Object to which you do not give a name and that is usually used locally. You could use an anonymous View Object when you want to perform a query (for example, a look-up) through a View Object, then remove it.

Using createViewObject

The following code snippet instantiates a View Object defined at design time. This technique is useful for instantiating View Objects within a generic Application Module, but you can use it with any Application Module. The code calls createViewObject, passing in the name of the View Object metadata (that is, the name you provided for the View Object at design time) that defines the View Object and a View instance name to identify this instance. You can use this View instance name to find the View Object later, if needed.

// Specify the Java file that defines the View Object.
// Format: <package>.<filename>
String voDefFile = "d2e.DeptView";
// Identify the View Object. Must be a valid Java identifier.
String voName = "demoDeptVO";
// Create the View Object within the context defined by the 
// Application Module.
ViewObject vo = appMod.createViewObject(voName, voDefFile);

Using findViewObject

The method findViewObject locates and returns a named View Object within an Application Module. This lets the Application Module reuse the View Object. The following code snippet calls findViewObject and passes in the name of a View Object.

// appMod is an Application Module defined at design time.
ViewObject vo = appMod.findViewObject("MyEmpView");

Using createViewObjectFromQueryStmt

The following code snippet creates a View Object from a SQL statement. A View Object created by calling createViewObjectFromQueryStmt is read-only. You cannot set its attribute values because there is no underlying Entity Object. To create a read-write View Object, call createViewObject, findViewObject, or createViewObjectFromQueryClauses to create a View Object based on an Entity Object.

// Define basic query string.
String sqlStr = "SELECT Emp.ename, Emp.mgr FROM EMP Emp ";
ViewObject vo = appMod.createViewObjectFromQueryStmt("QueryDemo", sqlStr);

Using createViewObjectFromQueryClauses

Use createViewObjectFromQueryClauses to build a View Object from SQL clauses. The View Object can be based on an Entity Object. For example, the following statement creates a View Object from attributes of the EMP Entity Object. Attributes related to EMP (such as E.ENAME) are updateable.

ViewObject v = appMod.createViewObjectFromQueryClauses("xyz",
"demo.hr.EMP", /* The one updateable Entity Object Name */
"E.ENAME, E.EMPNO", /* select clause */
"EMP E", /* from clause */
"E.DEPTNO = 10", /* where clause */
null); /* order by clause */