View Object attribute source order

JDev: 12.1.3.0.0
Source: GitHub

This article talks about the source order of ADF View Object's attributes and how it affects findByKey operation.

As a handy list, we need to remember that there are 3 items which need to be in sync in order for findByKey to work (or for that matter anything to work):

1. Sequence of columns in the ViewObject's SQL.
2. Sequence of attributes in the ViewObject's Attributes tab.
3. Sequence of attributes in ViewRowImpl's AttributesEnum.

Let's see how we can manipulate these orders and destroy our code! In order to demonstrate our use cases, I have a table VIEW_OBJECT_SOURCE_DEMO, which has 2 columns - KEY_ATTR1 and KEY_ATTR2 - which are marked as key attributes. In my AppModuleImpl class, I have a findByKey operation which takes in these 2 string values and returns a single row.

Let's go through the above 3 point list and see where we stand.


My AppModule's method is as follows:


Now if I run this method from BC tester, it works fine. (Well it should, right?)


Now let's add some chaos by changing the order of the attributes in the findByKey method.


And now we have an error. So in order for the new sequence of attributes to work in findByKey, we need to change the sequence of attributes in the view.

And this is where we need to be careful with the Set Source Order option in a view object. The "Set Source Order" button changes the sequence of attributes in the view object's metadata, but the sequence of attributes in the query remains unchanged. Let's use that and see.


If we run the BC tester now, we still get the same error. Why? Because the attribute sequence in the metadata has changed, but it is still not in sync with the sequence of columns in the SELECT clause.

So I also need to update the sequence of columns in the SQL query. Now if I change the query, this is how it looks:


Now the AM method works with the new sequence of key attributes.


This is the reason why JDeveloper asks if you'd want to update the attribute mappings every time you make any modification to the SQL query. Always do that, unless you know for sure that you have not tinkered with the SELECT clause.

Letting JDeveloper do the refractoring task is always the safest option. But if you do need to manually modify the View Object's XML metadata and add a new attribute (I have been forced to do it sometimes, with the development database being down for maintenance), be sure to follow the same sequence of attributes in the 3 point handy list mentioned at the beginning of this article. An example is below:


Cheers!

Comments