In this article, we'll take a look at different kinds of view object attributes you can create in Oracle ADF.
Different View Object AttributesView 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
- In the Application Navigator, double-click the view object for which you want to add a new attribute.
- 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 AttributeThe 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 AttributeIf 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:
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:
SQL-Calculated AttributeTo 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: