Thursday, December 31, 2009

Examining View Object Attributes in Oracle ADF 11g

In Oracle ADF 11g, you use entity objects to represent tables or UML entities and to implement business rules. And you use view objects to retrieve data from a data source and to expose and shape the data for clients. Using an Entity Fascade design pattern, view objects provides a restricted view of data and behavior of one or more entity objects.
In this article, we'll take a look at different kinds of view object attributes you can create in Oracle ADF.

Different View Object Attributes

View objects provide the means to retrieve data from a data source. In the majority of cases, the data source will be a database and the mechanism to retrieve data is the SQL query. At runtime each view object manages its own query result set. If you connect two or more view objects in master-detail relationships, that coordination is handled automatically. While defining a view object, you can link any of its query columns to underlying entity objects.
The easiest way to create view objects is to launch the Create Business Components from Tables Wizard, which allows you to create multiple entity objects, multiple view objects and other Business Components from database tables.

However, sometimes you need to add new columns to your tables or UML entities or you need to create calculated columns, then you must follow the steps described in this article to create new view object attributes incrementally. But, our focus here is to examine different kinds of view object attributes you can set up in Oracle ADF.

The classification of a view object attribute is mainly based on whether you would map it to a column/SQL or not.

If you map it to a column or SQL, you need to define a query column which either link it to an underlying entity attribute or provide it a SQL expression. View object attributes that are mapped to columns or SQL expressions will be included in the SQL SELECT list.
If your view object attribute is not mapped to a column or SQL, it's a transient attribute. A transient attribute can have a default value which could be either a literal or a Groovy expression.

How to Create a New View Object Attribute

  1. In the Application Navigator, double-click the view object for which you want to add a new attribute.
  2. In the overview editor, click the Attributes navigation tab and click the Create new attribute button (i.e., "+"). Note that Create new attribute button provides a dropdown list with two options:
    • New Attribute...
    • Add New Attribute from Entity...


Entity-Based Attribute

The simplest way to create a new view object attribute is to link it to an entity attribute (i.e., selecting "Add New Attribute from Entity..."). After creating it, its definition will look like this:

As you can see it, "Mapped Column or SQL" is checked and an alias (i.e., "ENAME") is provided in "Query Column." Also notice that it includes an Entity Attribute navigation tab. In this case, view object attribute properties are inherited from underlying entity object.

Transient Attribute

If you choose "New Attribute... " option, it will bring up New View Object Attribute wizard. To define a transient attribute, you can provide its default value using either a literal or a Groovy expression. In the following example, we set ValueUsingVORowFunction attribute's default value to be a Groovy expression:
  • adf.object.myFunctionAtVORowLevel(Sal)

Because a transient attribute is not mapped to a column or SQL, you need to uncheck "Mapped to Column or SQL" (notice that Query Column is grayed out after unchecking). adf.obj can be used to reference the object on which the expression is being applied. Depending on the context in which the Groovy script is applied, adf.obj could point to different objects. The context here is an instance of the view row implementation class (i.e., EmpViewRowImpl.java). Through this object, you can reference custom methods of the custom view row implementation class.

If you want to reference custom methods of the custom view implementation class (i.e., EmpViewImpl.java) from the Groovy script, you specify this instead:

  • adf.object.viewObject.myFunctionAtVOLevel(Sal)
To learn more on Groovy expression, see references in the More Readings section. For the sample implementation of transient attributes, you can download it from Steve Muench's blog here.

SQL-Calculated Attribute

To define SQL based calculated attribute, you also bring up New View Object Attribute wizard by choosing "New Attribute..." In the following example, it shows a SQL-calculated attribute which computes total number of tests in a detail table that is linked to the mater table that this attribute is defined in:
SQL-calculated attribute is mapped to SQL. Therefore, you need to check "Mapped to Column or SQL." Also, you need to provide a SQL expression and its type:
  • select count(*) from xtm_sel_tests xst where xst.test_run_id = TestRunEO.test_run_id

Note that TotalTests is defined in TestRunVO (i.e., a View Object) and TestVO (detail object) is linked to TestRunVO (master object) via a view link. Here detail table is named xtm_sel_tests and referenced in the expression. A Where clause which filters the result set with entries matching the test_run_id value of master table row is specified.
The query column you specified corresponds to an inner query which is embedded in the outer query. The final query generated by Oracle ADF looks like this:
SELECT TestRunEO.TEST_RUN_ID,
        ProductEO.PRODUCT_NAME,
        ProductEO.PRODUCT_ID,
        TestRunEO.START_TIME,
        TestRunEO.END_TIME,
        TestRunEO.TEST_URL,
   (select count(*) from xtm_sel_tests xst  where
    xst.test_run_id = TestRunEO.test_run_id) AS TOTAL_TESTS
FROM XTM_SEL_TEST_RUNS TestRunEO, XTM_SEL_PRODUCTS ProductEO, XTM_SEL_TESTS TestEO
WHERE (TestRunEO.PRODUCT_ID = ProductEO.PRODUCT_ID) AND (TestRunEO.TEST_RUN_ID = TestEO.TEST_RUN_ID)
Note that, in this example, there is also a join between XTM_SEL_RPODUCTS and XTM_SEL_TEST_RUNS tables. If you specify something wrong (for example, changing TestRunEO.test to TestRunVO.test), you can find out the error using Oracle Business Component Browser:

More Readings

  1. Groovy
  2. Overview of Groovy Support
  3. Groovy Scripting Tips for ADF Business Components
  4. Introduction to Groovy Support in JDeveloper and Oracle ADF 11g
  5. Adding Calculated and Transient Attributes to a View Object
  6. Oracle/PLSQL: Subqueries

6 comments:

AlyciaSweat said...
This comment has been removed by a blog administrator.
OdisCHashimoto said...

一起加油吧..................................................

不管不管 said...

^^ 謝謝你的分享,祝你生活永遠多彩多姿!.........................

Luciano said...

hello
I have a query in the field of Business Components
how to assign a default value, the result of a sql query, an attribute of an entity (EO) or if VO.

entity: Comprobantesgasto.
attributes: cgid, noejercicio, noentrada,
The default attribute value noejercicio is the result of a query, eg select max (Ej.ejercicio) from Ex Exercise WHERE Ej.Activo = 'S',
where the Ejercicio table contains information relating to Ejercicio = years may have more than one year active at any given time.

thank you very much. Greetings

Blogger said...

If you want your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (even if they're dating somebody else now) you got to watch this video
right away...

(VIDEO) Have your ex CRAWLING back to you...?

Blogger said...

QUANTUM BINARY SIGNALS

Professional trading signals sent to your mobile phone daily.

Start following our trades NOW and make up to 270% a day.