Creating Updateable, Multi-Entity View Object Definitions Dynamically
Of course, if you know the query and the relationships between entities at design time, it's preferable and simpler to use the View Object wizard to create your view object definition at design time. But, if you have an application that is dynamic to the point that you could not know the view definition information until runtime, it's possible to create a new view definition programmatically. This downloadable example, DynamicMultiEntityViewDef.zip, illustrates how to do it.
The API's to create a new View Definition live in the oracle.jbo.server package, so they are not intended to by used directly by clients. Instead, you should write code in your application module impl class (as illustrated in the sample) to construct a new ViewDefImpl instance, and proceed to call appropriate methods on the ViewDefImpl object to setup the various aspects of its definition.
As the createDynamicMultiEntityViewDefinition() in TestModuleImpl.java shows, you pass a fully-qualified view definition name like my.dynamic.EmpDeptView into the ViewDefImpl contructor. Then you proceed to invoke methods on the object to add the entity usages (indicating which entities particpate in the view object and with what "alias" names). Finally, you call methods to add each view object attribute definition. The example illustrates adding entity mapped attributes, SQL-calculated attributes, as well as transient attributes.
To make things easier to follow, I've created some private helper methods to clarify what's going on and have included comments to explain each one.
Finally, the TestClient.java client driver program illustrates working with the dynamically created view object, and illustrates that it is updateable.
Hope the example helps understand why it's so much simpler to define things at design time.