< Previous | Next >

Lesson 1: Generating code from the INVENTORY_LEVELS table

Now that your project is ready, you can connect to the GSDB database and generate code.
One of your goals is to create a bean that represents the INVENTORY_LEVELS table and that you can use to access the table. Another of your goals is to create an interface that declares methods that run SQL statements to access the table.

To generate code from the INVENTORY_LEVELS table:

  1. By default, the fields in generated beans are lowercase copies of the names of corresponding table columns. For example, if the name of a column is INVENTORY_YEAR, pureQuery names the corresponding field inventory_year. However, you can choose to use camel case for field names and eliminate underscores. The field name inventory_year would instead be inventoryYear. Select the option to camel case the names of fields in generated beans.
    1. Select Window->Preferences.
    2. In the Preferences window, select Data Management->SQL Development->pureQuery.
    3. On the pureQuery page, select the option Use camel case when generating defaults for Java names, then click OK.
  2. In the Data Source Explorer, expand GSDB > Schemas > GOSALES > Tables.
  3. Right-click the INVENTORY_LEVELS table and select Generate pureQuery Code.

    The Generate pureQuery Code from a Table wizard opens.

  4. Complete the fields of the pureQuery Code Generation page of the wizard by specifying the settings that are shown in the following table.
    Field Setting
    Source folder Keep the default value. You use this control to specify the project in which you want pureQuery to save the code that it generates, and which folder within that project. Because there is only one Java project in the Package Explorer, the src folder for that project is the default value.
    Package Type this name: com.myCompany.pureQuery.test
    Name Keep the default value, which is the name of the bean that the pureQuery code will use to access the table.
    Superclass Keep the default value. You can use this control to specify any class that you want the generated bean to inherit attributes from.
    Generate annotated-method interface for table Keep the check box selected to indicate that you want pureQuery to generate an interface that contains one method for each of the SQL statements that you select later in the wizard.
    Package Keep the default value, which is the same package that you selected for the bean.
    Interface name Keep the default value, which is the word Data appended to the name of the bean.
    Advanced settings Keep the default value. This control matters only if you already have an interface of the same name.

    Click Next to go to page two of the wizard.

  5. Complete the fields of the pureQuery Test Code Generation page of the wizard by specifying the settings that are shown in the following table.
    You use this page to specify to generate a class that you can use to test the other code that pureQuery generates from the table.
    Field Setting
    Source folder Keep the default value. This control specifies where in your project to save the generated code.
    Package Keep the default value, which is the name that you typed on the previous page.
    Generate test class for annotated-method interface for table Select this check box to specify that pureQuery generates a test class for the interface that you named on the previous page of the wizard. By default, pureQuery names the test class by appending the word Test to the name of the interface. Keep this default name.
    Generate test class for inline style Leave this check box unselected. If you select this check box, pureQuery generates a test class in which the SQL statements are within the test class itself, not within an interface. However, for the purposes of this tutorial, you plan to use annotated methods in your applications, not inline methods.
    Test Style Leave the default selection. Although you can use the workbench to test Java applications with JUnit, for this tutorial you will use a simple test.
    Connection Information Leave the default selection. One way for the test class to authenticate with the GSDB database when the test class runs is to use a connection URL and user ID that are hard-coded within the test class. The default way is for you to specify the user ID and password when you run the test class.
    Click Next to go to the third page of the wizard.
  6. Complete the fields on the Bean Fields page of the wizard by specifying the settings that are shown in the following table. The Bean Fields page of the wizard specifies how pureQuery is to generate the fields of the InventoryLevels bean.
    Field Setting
    Select the scope of the bean fields Leave the default selection. Although pureQuery can create the fields so that they have PUBLIC scope, the best practice is to make the fields protected and to modify their values with get() and set() methods.
    Map the columns to the bean fields Leave the default names for the fields.
    Click Next to go to the fourth page of the wizard.
  7. On the SQL Statements page, you can specify which statements you want pureQuery to generate. By doing so, you specify how many methods and which types to include in the interface. Select Generate the SQL statements specified below, and then unselect the two check boxes for MERGE statements.
  8. Click Finish.
The following changes are made to your Java project:
Figure 1. Editing a class that pureQuery generates
Editing a class that pureQuery generates
< Previous | Next >

Feedback