Oracle ADF Business Components
The first version of Oracle ADF Business Components say the light of the world with JDeveloper 3. It was created to address the deficiencies of the EJB 1.1 specification. Although named Business Components this is a framework to create Business Components (for Java). ADF Business Components are usually referred ADF BC or BC4J. I will try be consistent but bear with me if I mix both.
Entities and Views and Application Modules
Like any other ORM framework ADF BC has its own names for the same concepts. The primary concepts are Entities, Views and Application Modules. Entities are usually considered as the representation of a table row in the database, while Views are manipulating the Entities (creating, updating, retrieving, and deleting). While Views work on Entities you need an Application Module to work with the Views and with the data in the database. The Application Module contains all the Views that are need for this module and the necessary information of the underlying database.
Implementing the ADF BC Model Layer
As in the two previous model layers we are reusing the data model created in the database. This is also a strength of ADF BC, working with existing database model instead of creating it from scratch. (As both approaches have their pros and cons, a seperate entry to show these is quite likely.) Like before we're following the same order of steps for JDeveloper 10.1.3 and 11g:
- Create a Model Layer Project
- Import the Database Objects
Steps for JDeveloper 10.1.3
Step 1: Create a Model Layer Project
- To create a new Model Layer Project select OnlineSurvey in the Application Navigator, open the Context Menu and chose the New Project... menu entry.
- In the New Gallery select an Empty Project and click on OK.
- In the Create Project window set the Project Name to ModelBC and click on the OK button.
- Select the newly created ModelBC project in the Application Navigator and open the Project Properties window with a double-click.
- In the Project Properties window, check the values of Project Content and make sure that the Default Package is set to demo.survey.model.bc. Click OK to close this window.
- Click on the Connections Navigator. Expand the Database folder, select the survey entry and open the Context Menu. From it select the Properties... entry to open the Edit Database Connection window. Click on the Authentication tab and check whether the Deploy Password check box is checked. If not, make sure that it is checked! ADF BC relies on it.
Step 2: Import Business Components from Tables
Select the ModelBC project in the Application Navigator. Open the New Gallery from the Context Menu -> New .... In the New Gallery expand the Business Tier and select ADF Business Components. On the right-hand side select Business Components From Tables and click on the OK button.
- In the Initialize Business Components Project window select the survey connection and accept the default values for the SQL Flavor and Type Map (both should be Oracle). Click OK and the OK button on the Login window.
- In the Create Business Components From Tables wizard step 1, click on the Query button to get all available tables. Once the table names appear, select all of them. Move all entries from Available to Selected, by clicking on the >> button. Like in the other model projects we change the names from plural to singular names. Best is to do it right now by selecting each line in the list and change the appearing value in the Entity Name text field, eg Answers to Answer.
- Click Finish to end this wizard.
The wizard will now create the Entities and Association files in a single package. This is quite good at the first sight but really tedious if return to the project in three weeks. The naming for the Associations is hardly readable or memorable. Therefore we need to do two things:
- Move the Associations and the Entities in separate package.
- Rename the Associations with better readable names.
Move the files in separate packages is quite easy. Select all Entities, the ones without the Assoc suffix (Click on the file while holding the Ctrl-key). From the Context Menu select Refactor->Move... and type in the name demo.survey.model.bc.entities. After clicking on OK all files will be moved in the new package. You might be asked for creating the new package directory. This is perfectly OK. Repeat this for the Associations and move them into the package demo.survey.model.bc.links.
Warning! In JDeveloper 11g TP4 this might not finish. If this happens to you, the easiest bet is to kill JDeveloper 11g TP 4 and continue where it stopped.
Renaming the Associations to better readable names is as simple as moving the files. Only difference is that must do it file by file. In the demo.survey.model.bc.links package we select an Association, open the Context Menu, select Refactor, select Rename..., enter the new name and click on OK. We repeat this for all entries in the package and rename them according to the table below.
| Old Name | New Name |
|---|---|
| AnswersResponsesFKAssoc | AnswersPerResponse |
| ItemsQuestionsFKAssoc | ItemsPerQuestion |
| QuestionsSurveyFKAssoc | QuestionsPerSurvey |
| RepsonseSurveysFKAssoc | ResponsesPerSurvey |
| SurveysAuthorsFKAssoc | SurveysPerAuthor |
Steps for JDeveloper 11g
Note: During the course of this series a new version of the JDeveloper 11g has been released on OTN. I will follow every official release and you should do the same. If you've created the projects with an earlier version of JDeveloper 11g and reopen them in a newer release, it is possible that the project files have to be migrated. if JDeveloper asks you to do this, simply accept it.
Step 1: Create a Model Layer Project
- To create a new Model Layer Project select OnlineSurvey in the Application Navigator, click on the Applications Menu on the right-hand side select the New Project... menu entry.
- In the New Gallery select an Empty Project and click on OK.
- In the Create Project window set the Project Name to ModelBC and click on the OK button.
- Select the newly created ModelBC project in the Application Navigator and open the Project Properties window with a double-click.
- In the Project Properties window, check the values of Project Content and make sure that the Default Package is set to demo.survey.model.bc. Click OK to close this window.
- Expand the Application Resources accordion, expand the Connections folderm andexpand the Database folder, select the survey entry and open the Context Menu. From it select the Properties... entry to open the Edit Database Connection window. Check whether the Deploy Password check box is checked. If not, make sure that it is checked! ADF BC relies on it.
Step 2: Import Business Components from Tables
Select the ModelBC project in the Application Navigator. Open the New Gallery from the Context Menu -> New .... In the New Gallery expand the Business Tier and select ADF Business Components. On the right-hand side select Business Components From Tables and click on the OK button.
- In the Initialize Business Components Project window select the survey connection and accept the default values for the SQL Flavor and Type Map (both should be Oracle). Click OK and the OK button on the Login window.
- In the Create Business Components From Tables wizard step 1, click on the Query button to get all available tables. Once the table names appear, select all of them. Move all entries from Available to Selected, by clicking on the >> button. Like in the other model projects we change the names from plural to singular names. Best is to do it right now by selecting each line in the list and change the appearing value in the Entity Name text field, eg Answers to Answer.
- Click Finish to end this wizard.
The wizard will now create the Entities and Association files in a single package. This is quite good at the first sight but really tedious if return to the project in three weeks. The naming for the Associations is hardly readable or memorable. Therefore we need to do two things:
- Move the Associations and the Entities in separate package.
- Rename the Associations with better readable names.
Move the files in separate packages is quite easy. Select all Entities, the ones without the Assoc suffix (Click on the file while holding the Ctrl-key). From the Context Menu select Refactor->Move... and type in the name demo.survey.model.bc.entities. After clicking on OK all files will be moved in the new package. You might be asked for creating the new package directory. This is perfectly OK. Repeat this for the Associations and move them into the package demo.survey.model.bc.links.
Renaming the Associations to better readable names is as simple as moving the files. Only difference is that must do it file by file. In the demo.survey.model.bc.links package we select an Association, open the Context Menu, select Refactor, select Rename..., enter the new name and click on OK. We repeat this for all entries in the package and rename them according to the table below.
| Old Name | New Name |
|---|---|
| AnswersResponsesFkAssoc | AnswersPerResponse |
| ItemsQuestionsFkAssoc | ItemsPerQuestion |
| QuestionsSurveyFkAssoc | QuestionsPerSurvey |
| RepsonseSurveysFkAssoc | ResponsesPerSurvey |
| SurveysAuthorsFkAssoc | SurveysPerAuthor |
The final step in JDeveloper 11g is click on the Save All button in the button bar to save all the modified files.