Main

ADF Archives

March 7, 2008

ADF in Action: Deploying the Database Model

You may have noticed it, this is the missing step from the last post...

Deploying the Database

Once the database schema modeling is finished it needs to be deployed to the database. There are several options and a good database administrator (DBA) could improve the database behaviour considerably. Since this is not a DBA blog we leave this as an exercise to the reader.

Scripts to Create the Database User

In an Oracle Database a database user is the same as the schema user by default. It is also a good habit to create a tablespace for that user. First, we create a script for creating the tablespace:
  1. Click on the Offline Database Sources
  2. Open the Context Menu
  3. Select New ...
  4. Expand Database Tier
  5. Select Database Files
  6. On the right select SQL File
  7. Click the OK button
  8. In the opening Create SQL File window enter File Name: create_survey_tablespace.sql and make sure that the Directory Name has OnlineSurvey/Database/database (in JDeveloper 10.1.3) or OnlineSurvey/Database/database/SURVEY (in JDeveloper 11g) at the end.
  9. Click on the OK button to create an empty file.
  10. You will notice that the file appears right below the create_survey_schema.sql file in the Application Navigator.
  11. In the tab named create_survey_tablespace.sql enter the following line. You might need to change your file path!
    CREATE TABLESPACE survey datafile 'C:\oracle\xe\oradata\XEsurvey.dbf'
    SIZE 500k reuse autoextend ON;

  12. Repeat steps -10 to create a file called create_survey_user.sql. Put the following code in this file:
    DROP USER survey;
    CREATE USER survey IDENTIFIED BY survey DEFAULT TABLESPACE survey TEMPORARY
    TABLESPACE temp;
    GRANT connect,resource TO survey;

Running the SQL Files ...

All we need to do is to run the SQL files to complete the database deployment.

... In JDeveloper 10.1.3

Before we can do this, we need to check whether JDeveloper is configured to use SQL*Plus to execute the SQL scripts:
  1. Open Tools->Preferences
  2. Select Database Connections
  3. If the text field for SQL*Plus Executable is completed you're done, otherwise we have to point it to the right executable.
  4. Click on the Browse button
  5. In the new Open file dialog navigate to the $ORACLE_HOME directory for XE. In the $ORACLE_HOME/bin directory you'll find an executable named sqlplus.exe or sqlplus.
  6. Select sqlplus.exe and click on Open
  7. Once this is done, select the create_survey_tablespace.sql and from the Context Menu follow the Run in SQL to select the sysXE entry. This will open a terminal window for SQL*Plus. For security reasons the password of the sys database user has to be entered. When this is done the script will be run and creates the tablespace.
  8. Now do the same with the create_survey_user.sql.
To use this new user for later connections we create another Database Connection called surveyXE in the Connection Navigator (see ADF in Action: Setting up the Gear, Verifying the Database Connection for more details on this). With this connection we run the create_survey_schema.sql to create the full schema.

... In JDeveloper 11g

In 11g things are slightly different. We have Global Connections in the Resource Palette and Application-specific Connections in the Application Resources accordion.

To reuse the global database connection, we expand the sysXE database connection from the Resource Palette and drag it to the Application Resources Connections entry. (This is really cool!) It creates a Connections->Database->sysXE structure for the application also.

The rest is similar to JDeveloper 10.1.3. Just try it.

You should now have the Survey schema in your database. If not post a comment...


March 11, 2008

ADF in Action: Content of the Database Project, Iteration 1

Online Survey Application, Database Project, Iteration 1

It is time to verify you are on the right track. Your database project should like mine. Here are the screenshots for both JDeveloper versions.

Database Project In JDeveloper 10.1.3

DB_01_10:

Database Project In JDeveloper 11g

DB_01_11:

March 31, 2008

ADF in Action: The Model Layer

Application Layers

Modern computer applications are composed of several layers to make the different application tasks better manageable. The most prominent and widely used ... is the Model-View-Controller (MVC) Pattern. This pattern was introduced by the Smalltalk language. The MVC is one of the best understood and widely used implementation strategies. The same applies to most ADF applications and we will use it in this series as well.

The Model Layer

After having completed the database model, it is time to have some thoughts about the model layer. The model layer acts as the data holding layer. It arranges the data objects with their attributes and relationships. Methods for adding, deleting and modifying these objects are supplied. Very often this layer also supplies methods for retrieving and persisting the data.

Model Layer Choices

Oracle ADF supports many ways to implement the model layer. We will have a look at these three possible choices:
  • EJB 3.0 / Java Persistence API (JPA)
  • TopLink
  • ADF Business Components
Each of these choices deserves a separate coverage as they tend to be different in many ways.

General Considerations

Before we go into the details we have to consider some things:
  • Plural vs Singular Naming - Database designers use conventions different from Java designers. Most visible is the naming convention. While database designers use plural names, Java designers use singular names. The Model Layer should take care of that.
  • Using Existing Sequence Generators - To make the database model as useful as possible Oracle database designers often use Sequences for generating primary keys. This is particularly useful if more than one application accesses the database. The model layer should be able to use the existing Sequences.

ADF in Action: Building the Model Layer with EJB 3

Enterprise Java Beans 3.0

Although named after the ill-fated Enterprise Java Beans standards up to 2.1, the latest version specifies many state-of-the-art technologies and leverages lessons learned from technologies like dependency injection, Hibernate's and TopLink's Entity and Session Management. EJB 3 implements Configuration by Exception, where default values apply as long as these are deduceable from provide information. This results in an easier to develop habit which very often uses a single file per bean including every definition needed.

EJB 3 Entities

To mark the change to earlier versions, EJB 3 removed the Bean from the term Entity Bean and uses Entity. Entities are not bound to the container anymore and can move between the different layers (Model to View to Model and so on) without a specific impact. As a result some design patterns of the EJB 2.x world are not fully applicable (eg. Data Transfer Objects) while others are a possible candidates for an update (eg. Data Access Object). EJB 3 Entities implement the Java Persistence API.

Java Persistence API

The authors of the Java Persistence API (JPA) have learned a lot from ORM frameworks like TopLink or Hibernate and other techniques. The JPA is much more complete then the previous EJB definitions in terms of attribute mapping, inheritance and the query language (JPA Query Language (JQL)).

Implementing the EJB 3 Model Layer

A very easy way to implement the EJB 3 Model Layer is by using the existing Database Model as developed in the previous posts (see Setting up the Gear, Creating the Database, and Deploying the Database Model). Additionally you should make sure that your database is up and running and a Database Connection called survey configured. The steps in JDeveloper 10.1.3 and 11g are quite similar.
  1. Create a Model Layer Project
  2. Import the EJB 3 from Database Tables
  3. Check the generated files
  4. Create a Session Facade
  5. Create a Test Client

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 ModelEJB30 and click on the OK button. Select the newly created ModelEJB30 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.ejb30. Click OK to close this window.

Step 2: Import the EJB 3 from Datbase Tables

Select the ModelEJB30 project in the Application Navigator. Open the New Gallery from the Context Menu -> New .... In the New Gallery expand the Business Tier and select EJB. On the right-hand side select Entities From Tables (JPA/EJB 3.0) and click on the OK button.
  • In the Create Entities From Tables wizard step 1 select the survey connection. Click on Next to get the database connection.
  • In step 2 click on the Query button to get all available tables. Since we only have tables the check box for Tables is sufficient. Once the table names appear, select all of them.
  • Move all entries from Available to Selected, by clicking on the >> button. Note that all names have changed to typical Java type names but in plural (eg. Answers (SURVEY.ANSWERS)). The values in parentheses tell you about the table relationship. Select every entry and see how the text field under the Selected list changes its value. Here we can change the names from plural to singular names. Change the names accordingly. You should have these names: Answer, Author, Item, Question, Response, Survey
  • Click Next to go to wizard step 3. In step 3 of the wizard accept the default values and click Next to go to wizard step 4.
  • This step can be used for changing the names as well, but is less user-friendly. Click Next to go to the Summary and click on Finish to close the wizard. Now all six entities will be created.
You can find them in the Application Navigator under ModelEJB30->Application Sources->demo.survey.model.ejb30.

Step 3: Check the generated files

To check the generated files open them by selecting Answer.java, hold the Shift key, select Survey.java, open the Context Menu and select Go To Bean Class. Every selected file will be opened in a Code Editor. Select the tab named Answer.java to bring it to the front. Change the code to get the lines below:
@Entity
@NamedQuery(name = "Answer.findAll", query = "select o from Answer o")
@SequenceGenerator(name = "SEQUENCE_ANSWERS", sequenceName = "SEQ_ANSWERS", allocationSize = 1)
@Table(name = "ANSWERS")
public class Answer implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQUENCE_ANSWERS")
    @Column(nullable = false)
    private Long id;

While adding this code, JDeveloper will suggest possible values and actions to be taken. Only import the types javax.persistence.SequenceGenerator, javax.persistence.GeneratedValue and javax.persistence.GenerationType. You should explore how the Ctrl-Blank short cut helps you to provide the correct annotation parameters.
Do the same for the other files Author.java, Item.java, Question.java, Response.java, Survey.java but use SEQ_AUTHORS with SEQUENCE_AUTHORS, SEQ_ITEMS with SEQUENCE_ITEMS, SEQ_QUESTIONS with SEQUENCE_QUESTIONS, SEQ_RESPONSES with SEQUENCE_RESPONSES, and SEQ_SURVEYS with SEQUENCE_SURVEYS.

Step 4: Create a Session Facade

Click on ModelEJB30 in the Application Navigator. Any other entry of the ModelEJB30 project will do, also. Select New... from the Context Menu and navigate to Business Tier -> EJB in the New Gallery. On the right-hand side select Session Bean (EJB 1.1/2.x/3.0) and click on the OK button to open the 4 step Create Session Bean wizard.
  • In wizard step 1 change the EJB Name to SurveySession, accept the pre-selected defaults and click on Next
  • In wizard step 2 make sure that all methods are checked (expand the tree to see which methods are available). Click Next to go to the next wizard step.
  • Step 3 shows the default values for the bean class and step 4 - Click Next to go from step 3 to step 4 - shows the implemented interfaces. Remote and Local interfaces are good enough for our scenario.
  • Clicking Next in step 4 directs us to the wizard Summary window which we close by clicking on the Finish to go to the next wizard step.
A SurveySessionBean will be added to the project. Double-click on it and you'll see the generated Session Facade in the Code Editor.

Step 5: Create a Test Client

To finish the JDeveloper 10.1.3 tasks the creation of a test client needs to be done. Click on the SurveySessionBean in the Application Navigator and open the Context Menu. Find the New Sample Java Client entry and click on it. Accept all default values and click on the OK button to generate the test client. Open it and change the main method as follows:
    public static void main(String[] args) {
        try {
            final Context context = getInitialContext();
            SurveySession surveySession =
                (SurveySession)context.lookup("SurveySession");
            Author author = new Author();
            author.setName("Olaf Heimburger");
            surveySession.persistEntity(author);
for (Author author :
(List<Author>)surveySession.queryAuthorFindAll()) {
System.out.println("id = " + author.getId());
System.out.println("name = " + author.getName());
}
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
To test the Session Facade and the Test Client do the following:
  1. Select the SurveySessionBean, open the Context Menu and select Run. Wait until you see Oracle Containers for J2EE 10g (10.1.3.3.0)  initialized.
  2. Select the SurveySessionClient, open the Context Menu and select Run. You should get some output.

Steps for JDeveloper 11g

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 ModelEJB30 and click on the OK button. Select the newly created ModelEJB30 project in the Application Navigator and open the Project Properties window with a double-click. In the Project Source Paths window, check the values of Project Source Paths and make sure that the Default Package is set to demo.survey.model.ejb30. Click OK to close this window.

Step 2: Import the EJB 3 from Datbase Tables

Select the ModelEJB30 project in the Application Navigator. Open the New Gallery from the Context Menu -> New .... In the New Gallery expand the Business Tier and select EJB. On the right-hand side select Entities From Tables and click on the OK button.
  • In the Create Entities From Tables wizard step 1 select EJB 3.0 - JPA Entities. Click on Next to select the Persistence Unit. Since we haven't created one yet, click on the New ... button. In the New Persistence Unit window set the Name to survey and the JTA Datasource Name to jdbc/surveyDS. Click OK to close the window. Click Next to go to the next step.
  • In step 3 make sure that the Online Database Connection radio button is checked. Click Next to go to the next step.
  • Step 4 lets you define the necessary details for the Database connection and the Offline Database. The Connection should be survey. If this is not available click on the green plus button to create a connection with survey as the Connection Name. For the Offline Database click on the New button and in the Create Offline Database window set the Name and Default Schema to SURVEY. From the Database to emulateOracle10g Express Edition Release 2. Click OK to close this window. Note that this is not really convenient as we did this in the Database project already. So far I didn't found a way to reuse the definitions from the Database project and hope for a dependency-like implementation in the future.
  • Click Next to go to step 5. 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.
  • Click Next to go to wizard step 6. In this step accept the default values and click Next to go to wizard step 7. In step 7 we have to change the names from plural to singular names. From the drop-down list select one entry after another and remove the last letter s from the Entity Name.
    Do this carefully and slowly. In 11g TP 3 chances are that you do it to quick or go to far and you are not able to change anything afterwards. Only solution is start again with the whole wizard. I speak from experience.
  • Click on Next to go to the Summary step. Click on Finish to create all six entities.
You can find them in the Application Navigator under ModelEJB30->Application Sources->demo.survey.model.ejb30.
drop-down select

Step 3: Check the generated files

To check the generated files open them by selecting Answer.java, hold the Shift key, select Survey.java, open the Context Menu and select Go To Bean Class. Every selected file will be opened in the Code Editor. Select the tab named Answer.java to bring it to the front. Change the code to get the lines below:
@Entity
@NamedQuery(name = "Answer.findAll", query = "select o from Answer o")
@SequenceGenerator(name = "SEQUENCE_ANSWERS", sequenceName = "SEQ_ANSWERS", allocationSize = 1)
@Table(name = "ANSWERS")
public class Answer implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQUENCE_ANSWERS")
    @Column(nullable = false)
    private Long id;

While adding this code, JDeveloper will suggest possible values and actions to be taken. Only import the types javax.persistence.GenerationType. A big improvement is that the EJB 3 Annotations are already known by JDeveloper and do not require any additional action. You should explore how the Ctrl-Blank short cut helps you to provide the correct annotation parameters.
Do the same for the other files Author.java, Item.java, Question.java, Response.java, Survey.java but use SEQ_AUTHORS with SEQUENCE_AUTHORS, SEQ_ITEMS with SEQUENCE_ITEMS, SEQ_QUESTIONS with SEQUENCE_QUESTIONS, SEQ_RESPONSES with SEQUENCE_RESPONSES, and SEQ_SURVEYS with SEQUENCE_SURVEYS.

Step 4: Create a Session Facade

Click on ModelEJB30 in the Application Navigator. Any other entry of the ModelEJB30 project will do, also. Select New... from the Context Menu and navigate to Business Tier -> EJB in the New Gallery. On the right-hand side select Session Bean and click on the OK button to open a 6 step Create Session Bean wizard.
  • Since the EJB version is already known the wizard starts a step 2, by using the Back button you can go to step 1 but this is not needed.
  • In step 2 change the EJB Name to SurveySession, accept the pre-selected defaults and click on Next to go to the next wizard step.
  • In step 3 make sure that all methods are checked (expand the tree to see which methods are available). Click Next to go to the next wizard step.
  • Step 4 shows the default values for the bean class and Step 5 - Click Next to go from step 4 to step 5 - shows the implemented interfaces. Remote and Local interfaces are good enough for our scenario. Clicking Next in Step 5 directs us to the wizard Summary window which we close by clicking on the Finish button.
A SurveySessionBean will be added to the project. Double-click on it and you'll see the generated Session Facade in the Code Editor.

Step 5: Create a Test Client

To finish the JDeveloper 11g tasks, the creation of a test client needs to be done. Click on the SurveySessionBean in the Application Navigator and open the Context Menu. Find the New Sample Java Client entry and click on it. Accept all default values and click on the OK button to generate the test client. Open it and change the main method as follows:
    public static void main(String[] args) {
        try {
            final Context context = getInitialContext();
            SurveySession surveySession =
                (SurveySession)context.lookup("SurveySession");
            Author author = new Author();
            author.setName("Olaf Heimburger");
            surveySession.persistEntity(author);
for (Author author :
(List<Author>)surveySession.queryAuthorFindAll()) {
System.out.println("id = " + author.getId());
System.out.println("name = " + author.getName());
}
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
To test the Session Facade and the Test Client do the following:
  1. Select the SurveySessionBean, open the Context Menu and select Run. Wait until you see Embedded OC4J Server startup time: 83594 ms.
  2. Select the SurveySessionClient, open the Context Menu and select Run.

What Have We Done So Far?

First of all we have created a number of Entities for the Tables that we created in the Database project. Each Entity has it's own Sequence Generator assigned, using the @GeneratedValue annotation. Since we used the default SEQUENCE statement for the database, ie without an explicit INCREMENT option, we had to adjust the default allocationSize for JPA layer from 50 to 1 in the @SequenceGenerator annotation. Finally we created a Session Facade for persisting, retrieving and updating our Entities conveniently. The very last step was to test the Facade with the Author class.

References

  • EJB 3 in Action, Debu Panda, Reza Rahman, Derek Lane, Manning, ISBN 978-1-933988-34-4
  • Java Persistence with Hibernate, Christian Bauer, Gavin King, Manning, ISBN 1-932394-88-5

April 15, 2008

ADF in Action: Building the Model Layer with Oracle TopLink

Oracle TopLink

When looking around for O/R mapping frameworks, Oracle TopLink will appear as one of the most mature frameworks. Oracle TopLink builds high-performance applications that store persistent object-oriented data in a relational database. It successfully transforms object-oriented data into either relational data or Extensible Markup Language (XML) elements.

Entities

In TopLink Entities are defined as Plain Old Java Objects (POJOs) with additional files containing the mapping information. These files hold the meta data for the connection and mapping information.

Implementing the TopLink Model Layer

Similar to implementing the EJB Model Layer we're using the  existing Database Model as developed in the previous posts (see Setting up the Gear, Creating the Database, and Deploying the Database Model). Additionally you should make sure that your database is up and running and a Database Connection called survey configured. The steps in JDeveloper 10.1.3 and 11g are quite similar.
  1. Create a Model Layer Project
  2. Import TopLink Entities from Database Tables
  3. Check the generated files
  4. Create a Session Facade
  5. Create a Test Client

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 ModelTopLink and click on the OK button.
  • Select the newly created ModelTopLink 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.toplink. Click OK to close this window.

Step 2: Import TopLink Entities from Database Tables

Select the ModelTopLink project in the Application Navigator. Open the New Gallery from the Context Menu -> New .... In the New Gallery expand the Business Tier and select TopLink. On the right-hand side select Java Objects From Tables and click on the OK button.
  • In the Create Java Objects From Tables wizard step 1, select the survey connection and Oracle 10g for the Database Platform. For the TopLink Map, click on the New ... button (note the underscore under the first point of the elipses. Hard to read.). In the Create Object-Relational Map window set the TopLink Map Name to surveyMap and click on the OK button. This will create a new TopLink map. Click on Next to go to the Select Tables step.
  • In step 2 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.
  • Click Next to go to wizard step 3. In step 3, General Options, accept the default value, the package name, and click Next to go to wizard step 4.
  • In step 4, Specify Object Details, we can change the names of the Java Classes from plural to singular. This is a bit cumbersome as we have to select the Table Name, change the Java Class name and so on. Add the end of this step we should have a singular Java Class name for every Table Name.
  • Click Next to go to the Summary and click on Finish to close the wizard. Now all six entities will be created.
You can find them in the Application Navigator under ModelTopLink->Application Sources->demo.survey.model.toplink. Also notice some additional files like sessions.xml and surveyMap under ModelTopLink->Application Sources->TopLink.

Step 3: Check the generated files

To check the generated files open them by selecting Answer.java, hold the Shift key, select Survey.java, and drag them into the working area right to Application Navigator. Every selected file will be opened in its own Code Editor. Select the tab named Answer.java to bring it to the front. You will see the completely generated code. This class has a small flaw, it is not Serializable, ie. you can not move it between two JVMs. We have to change the code to get the lines below:
import java.io.Serializable;

import oracle.toplink.indirection.ValueHolder;
import oracle.toplink.indirection.ValueHolderInterface;

public class Answer implements Serializable {
We do this by entering the implements Serializable part and let JDeveloper ask us to add the import statement via Alt-Enter. Be sure to select Serializable (java.io). This is the right package. We have to do this for every class, but rely on JDeveloper's support for knowing which import statement is the correct one for a specific piece of code:
  • Select the implements Serializable snippet (including the leading blank) and copy into the clipboard (Ctrl-C). Open Author.java. Point your text cursor right after the class name, hit Ctrl-V and you are done.
  • Repeat this for Item.java, Question.java, Response.java, and Survey.java.
  • Save your work.
Now is the right time to add the database sequences to the layer. This is not done automatically and must be configured in the TopLink configuration files sessions.xml and surveyMap.
  • In the Application Navigator select sessions.xml. In the Structure window, double-click on default to open the Mapping Editor for the sessions.xml.
  • Click on the Login tab and click on the Sequencing sub-tab. The information in the General sub-tab is good enough at the moment. In the Sequencing sub-tab we change the Use Native Sequencing to True. Also check the Preallocation Size check-box to make the number field editable. Change the value to 1 to make it work with the current database sequence increment.
  • Save your work.
  • Now click on the surveyMap.
  • In the Structure window expand demo.survey.model.toplink to get all available Java Classes in this map.
  • Double-click on the Answer mapping. This opens the Mapping Editor for the Answer class.
  • In the Descriptor Info tab check the Use Sequencing check-box. This makes this block editable.
  • The values for Table and Field are correct. Set the value for Name to SEQ_ANSWERS.
  • Now repeat setting of the database sequence for the classes Author, Item, Question, Response, and SurveySequencing Name to SEQ_AUTHORS, SEQ_ITEMS, SEQ_QUESTIONS, SEQ_RESPONSES, and SEQ_SURVEYS, respectively.
  • Save your work.

Step 4: Create a Session Facade

Click on ModelTopLink in the Application Navigator. Any other entry of the ModelTopLink project will do, also. Select New... from the Context Menu and navigate to Business Tier -> EJB in the New Gallery. On the right-hand side select Session Bean (EJB 1.1/2.x/3.0) and click on the OK button to open the 4 step Create Session Bean wizard.
  • In wizard step 1 change the EJB Name to SurveySession, accept the pre-selected defaults. Note that JDeveloper already recognized the Entity implementation as TopLink POJOs. Click on Next.
  • In wizard step 2 make sure that all methods are checked (expand the tree to see which methods are available). Click Next to go to the next wizard step.
  • Step 3 shows the default values for the bean class and step 4 - Click Next to go from step 3 to step 4 - shows the implemented interfaces. Remote and Local interfaces are good enough for our scenario.
  • Clicking Next in step 4 directs us to the wizard Summary window which we close by clicking on the Finish to go to the next wizard step.
A SurveySessionBean will be added to the project. Double-click on it and you'll see the generated Session Facade in the Code Editor.

Step 5: Create a Test Client

To finish the JDeveloper 10.1.3 tasks the creation of a test client needs to be done. Click on the SurveySessionBean in the Application Navigator and open the Context Menu. Find the New Sample Java Client entry and click on it. Accept all default values and click on the OK button to generate the test client. Open it and change the main method as follows:
    public static void main(String[] args) {
        try {
            final Context context = getInitialContext();
            SurveySession surveySession =
                (SurveySession)context.lookup("SurveySession");
            Author author = new Author();
            author.setName("Olaf Heimburger");
            surveySession.persistEntity(author);
for (Author a : surveySession.queryAuthorFindAll()) {
System.out.println("id = " + a.getId());
System.out.println("name = " + a.getName());
}
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
To test the Session Facade and the Test Client do the following:
  1. Select the SurveySessionBean, open the Context Menu and select Run. Wait until you see Oracle Containers for J2EE 10g (10.1.3.3.0)  initialized.
  2. Select the SurveySessionClient, open the Context Menu and select Run. You should get some output.

Steps for JDeveloper 11g

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 ModelTopLink and click on the OK button.
  • Select the newly created ModelTopLink project in the Application Navigator and open the Project Properties window with a double-click.
  • In the Project Source Paths window, check the values of Project Source Paths and make sure that the Default Package is set to demo.survey.model.toplink. Click OK to close this window.

Step 2: Import TopLink Entities from Database Tables

  • Select the ModelTopLink project in the Application Navigator.
  • Open the New Gallery from the Context Menu -> New ....
  • In the New Gallery expand the Business Tier and select TopLink/JPA. On the right-hand side select Java Objects From Tables and click on the OK button.
  • In the Create Java Objects From Tables wizard step 1, Select Object-Relational TopLink Map, click on the New ... button. In the New TopLink Object Map window set the TopLink Map Name to surveyMap. The window instantly complaints about the missing Offline Database setting.
  • Click on the New ... button to open the Create Offline Database window. For Name enter SURVEY, for Default Schema enter SURVEY as well. Database to emulate should be Oracle10g Express Edition Release 2. Click on the OK button to close this window.
  • Click on OK to close the New TopLink Object Map window.
  • Click on Next to go to the Select Tables step.
  • In this step 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.
  • Click Next to go to step, Specify Default Package. Accept the default value, the package name, and click Next to go to the next step.
  • In step Specify Object Details, we can change the names of the Java Classes from plural to singular. Here we have table with Table Name and Class Name columns. To edit the Class Name double-click in the Authors cell and change the name to Author. To finish this action click on the next cell.
  • Repeat this for every Class Name.
  • Click Next to go to the Specify Relationships to Generate.
  • Click Next to go to the Summary and click on Finish to close the wizard. Now all six entities will be created.You can find them in the Application Navigator under ModelTopLink->Application Sources->demo.survey.model.toplink. Also notice some additional files like sessions.xml and surveyMapModelTopLink->Application Sources->TopLink.

Step 3: Check the generated files

To check the generated files open them by selecting Answer.java, hold the Shift key, select Survey.java, and drag them into the working area right to Application Navigator. Every selected file will be opened in its own Code Editor. Select the tab named Answer.java to bring it to the front. You will see the completely generated code. Note that this class implements Serializable already!

Now is the right time to add the database sequences to the layer. This is not done automatically and must be configured in the TopLink configuration files sessions.xml and surveyMap.
  • In the Application Navigator select sessions.xml. In the Structure window, double-click on default to open the Mapping Editor for the sessions.xml.
  • Click on the Login tab and scroll down to the Sequencing section. Here select the Native Sequencing radio button. Also change the Preallocation Size to 1 to make it work with the current database sequence increment.
  • Save your work.
  • Now click on the surveyMap.
  • In the Structure window expand demo.survey.model.toplink to get all available Java Classes in this map.
  • Double-click on the Answer mapping. This opens the Mapping Editor for the Answer class.
  • In the Descriptor Info tab check the Use Sequencing check-box. This makes this block editable.
  • The values for Table and Field are correct. Set the value for Name to SEQ_ANSWERS.
  • Now repeat setting of the database sequence for the classes Author, Item, Question, Response, and SurveySequencing Name to SEQ_AUTHORS, SEQ_ITEMS, SEQ_QUESTIONS, SEQ_RESPONSES, and SEQ_SURVEYS, respectively.
  • Save your work.

Step 4: Create a Session Facade

Click on ModelTopLink in the Application Navigator. Any other entry of the ModelTopLink project will do, also. Select New... from the Context Menu and navigate to Business Tier -> EJB in the New Gallery. On the right-hand side select Session Bean and click on the OK button to open the Create Session Bean wizard.
  • In step Select EJB Version, accept the Enterprise JavaBeans 3.0 (Java EE 5.0) selection. Click Next to continue.
  • In step EJB Name and Queries change the EJB Name to SurveySession, accept the pre-selected defaults. Note that JDeveloper already recognized the Entity implementation as TopLink POJOs. Click on Next.
  • In step Session Facade - Select JPA Entity Methods make sure that all methods are checked (expand the tree to see which methods are available). Click Next to go to the next wizard step.
  • Step EJB Home and Component Interfaces shows the implemented interfaces. Remote and Local interfaces are good enough for our scenario.
  • Clicking Next directs us to the wizard Summary window which we close by clicking on the Finish to go to the next wizard step.
A SurveySessionBean will be added to the project. Double-click on it and you'll see the generated Session Facade in the Code Editor.

Step 5: Create a Test Client

To finish the JDeveloper 11g tasks the creation of a test client needs to be done. Click on the SurveySessionBean in the Application Navigator and open the Context Menu. Find the New Sample Java Client entry and click on it. Accept all default values, select the Connect to OC4J Embedded in JDeveloper and click on the OK button to generate the test client. Open it and change the main method as follows:
    public static void main(String[] args) {
        try {
            final Context context = getInitialContext();
            SurveySession surveySession =
                (SurveySession)context.lookup("SurveySession");
            Author author = new Author();
            author.setName("Olaf Heimburger 11g");
            surveySession.persistEntity(author);
for (Author a : surveySession.queryAuthorFindAll()) {
System.out.println("id = " + a.getId());
System.out.println("name = " + a.getName());
}
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
To test the Session Facade and the Test Client do the following:
  1. Select the SurveySessionBean, open the Context Menu and select Run. Wait until you see somethink like Embedded OC4J Server startup time: 83594 ms.
  2. Select the SurveySessionClient, open the Context Menu and select Run. You should get some output.

What have we done so far?

Essentially the same as for the EJB 3.0 Model Layer. The generation was quite similar but the setup of the sequencing was different. Configuration files vs. Annotations in the source code.

A Note on EclipseLink

EclipseLink is an open-sourced version of Oracle TopLink 11 and available from the Eclipse site.
EclipseLink is interesting because it will be the JPA 2.0 reference implementation and although not covered in this entry, I'll definitely give it a try.

References

Oracle TopLink Getting Started Guide
Oracle TopLink Developer's Guide


July 8, 2008

ADF in Action: Building the Model Layer with ADF BC (BC4J)

It has been a while. Being busy is a lame excuse, I was also investigating some other things and worked a lot with Oracle Application Diagnostics for Java (AD4J)...

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:

  1. Move the Associations and the Entities in separate package.
  2. 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:

  1. Move the Associations and the Entities in separate package.
  2. 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.

October 23, 2008

ADF in Action: Migrating to JDeveloper 11.1.1

Since JDeveloper 11.1.1 has been released on OTN, a number of posts have done on the new features of JDev 11g. We will use all these blogs to complete our task...

Why migrate? Can't we stick with the old 11g?

When a new production release is available it is better to migrate to this release as long as it makes sense. With JDeveloper 11.1.1 this makes perfect sense, because all previous version where just technical previews and not intended for production. Many of us hit one or the other problem and sometimes nearly all of our work was lost. For this reason we happily migrate to the production release.

Start the Migration

Stop, hold it. Not so quick! Before we start migrating, we make a backup copy of the complete application directory. Just in case we want to return to our old version.

Now, we are ready to start. Since migration from Technical Previews to Production Releases is not supported, we take the JDeveloper 10.1.3 version of the project and copy it to the 11g directory. To get the application workspace into JDeveloper we open it with Open Application ... in the Application Navigator. JDeveloper will tell us that the project files will be migrated. This what we want. Great, go ahead. A quick scan through all the projects looks pretty promising. OK, let us continue.

The Database Project

This is easiest project. We can check the database connection from Application Resources -> Connections -> Database -> survey. On my copy the login details where gone. This is a security measure and acceptable. After supplying the necessary details, the connection to the database should work like a charm. If you do not have your database up and running, restart it.

The ModelTopLink Project

OK, move on to the ModelTopLink project. Nearly everything is fine here. But we have to check the libraries and the client must be changed a bit. To change the libraries open the Project Properties (double-click on the ModelTopLink project), select Libraries and Classpath. You need only the libraries Oracle XML Parser v2, EJB 3.0, TopLink, Java EE 1.5 API, and WebLogic 10.3 Remote-Client. Everything else should be removed.

Finally we need to adjust the code of the SurveySessionClient a bit.

1. The parameter for the context.lookup() has to be changed to

(SurveySession)context.lookup("SurveySessionBean#demo.survey.model.toplink.SurveySession");

2. The method getInitialContext() needs some extra settings to connect to the integrated WebLogic server:

private static Context getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
// WebLogic Server 10.x connection details
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
return new InitialContext(env);
}

To avoid problems while testing, click on the ModelEJB30 project, open the Context Menu and choose Remove from OnlineSurvey.jws. Click on the SurveySessionBean, open the Context Menu and select Run. When you see [Application OnlineSurvey deployed to Server Instance DefaultServer] in the Running: Default Server - Log window, click on the SurveySessionClient, open the Context Menu and select Run. The output should show that the client is working.

The ModelEJB30 Project

This project is similar to the ModelTopLink project: we have to check the libraries and the client must be changed a bit. If the ModelEJB30 project is not in your OnlineSurvey application, go to the Application Menu left to the OnlineSurvey name and select Open Project ..., in the Open Project file selector navigate to the ModelEJB30 directory and select the ModelEJB30.jpr file.

To change the libraries open the Project Properties (double-click on the ModelEJB30 project), select Libraries and Classpath. You need only the libraries Oracle XML Parser v2, EJB 3.0, TopLink, Java EE 1.5 API, and WebLogic 10.3 Remote-Client. Everything else should be removed.

Finally we need to adjust the code of the SurveySessionClient a bit.

1. The parameter for the context.lookup() has to be changed to

(SurveySession)context.lookup("SurveySessionBean#demo.survey.model.ejb30.SurveySession");

2. The method getInitialContext() needs some extra settings to connect to the integrated WebLogic server:

private static Context getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
// WebLogic Server 10.x connection details
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
return new InitialContext(env);
}

To avoid problems while testing, click on the ModelTopLink project, open the Context Menu and choose Remove from OnlineSurvey.jws. Click on the SurveySessionBean, open the Context Menu and select Run. When you see [Application OnlineSurvey deployed to Server Instance DefaultServer] in the Running: Default Server - Log window, click on the SurveySessionClient, open the Context Menu and select Run. The output should show that the client is working.

The ModelBC Project

Finally, the ModelBC project. You will notice that you have a second ModelBC0.jpx file in the Application Navigator. This will be used in the future. Everything is fine so far and no further actions are to be done.

Exercise

Because the creation of the database is quite a tedious task, create an Ant file to drop the old schema, create the tablespace, create the user, and create the tables of the schema. It might be necessary to change the some of the SQL files.

Coming Up Next

The next post will start building the UI part of the application.

October 31, 2008

JDev 11g: Packaging Libraries at EAR Level

Many users have problems to use their libraries in their applications. But sometimes a working solution is too obvious to be noticed.

To include all the libraries you need for your project in an EAR file:

  1. Create a package project called common-libs and put all the libraries in a directory included in the Project Source Path. Don't use the src directory but lib at the same hierarchy level. Add this new directory to the Project Source Paths.
  2. Open the Application Properties
  3. Create a deployment profile for the EAR file.
  4. To include the libraries within the EAR file we have two options to include the libraries are available: either put into APP-INF/lib or a directory of your choice (eg. lib). In the Deployment Profile select Application Assembly, tick the check box of every JAR file you need and enter the directory name (APP-INF/lib or lib) in the Path in EAR text field at the bottom. This must be done for every JAR file!
  5. If you do not want to use APP_INF/lib you need to create an application.xml for the application. Open the Application Resources accordion and expand Descriptors and META-INF to see which Deployment Descriptors are available. On Descriptors, chose New JEE Deployment Descriptor... from the Context Menu and select application.xml. In the newly created application.xml insert the tag <library-directory>lib</library-directory>.
  6. From the Application Menu select Deploy to EAR File to check the contents before deploying it to the server.


April 6, 2009

Upgrading your WebLogic to ADF 11.1.1.0.2

One of the first questions that come up with the JDev /ADF 11.1.1.0.2 release is how to upgrade your WebLogic 10.3 to it. Here is my solution:

  1. Open the Uninstaller of your WebLogic installation
  2. Select JDeveloper 11g (11.1.1.0.x) Studio Edition 11.1.1.0
  3. Uncheck everything under WebLogic Server
  4. Click Next to finish the deinstallation. If you're interested you can click on the Details button to see what has be uninstalled.
  5. Now open the JDev 11.1.1.0.2 Installer
  6. Select the WebLogic home where to install the new ADF version
  7. Now deselect everything and select the Application Development Framework Runtime only. Note: This will also select the JDeveloper and ADF folder again.
  8. To avoid class loading issues, rebuild all your EAR files.

Enjoy.

Note: You will find the Uninstaller either as $WLSHOME/wlserver_10.3/uninstall/uninstall.sh or %WLSHOME%\wlserver_10.3\uninstall\uninstall.cmd. On a headless server (like a Linux or Unix-based system) you may need to set the DISPLAY variable to an X Server like VNC or Xming. (Assuming that $WLSHOME or %WLSHOME% is your home directory for the WebLogic Server 10.3. installation.)

About ADF

This page contains an archive of all entries posted to Olaf Heimburger's Blog in the ADF category. They are listed from oldest to newest.

JDEV is the next category.

Many more can be found on the main index page or by looking through the archives.

Top Tags

Powered by
Movable Type and Oracle