Creating handler classes from implementation classes and placing them in separate files

You can customize separately the handler classes that the workbench creates in the implementation classes that it generates for your annotated-method interfaces.

About this task

For example, suppose that you want to customize the way that pureQuery processes the results that an annotated method returns for a query. Rather than write your own RowHandler class from scratch, you can modify a RowHandler class that already exists in the implementation class that the workbench generated for the interface that declares that method.

Although you can modify the handler class directly in the implementation class, in some situations it is more convenient to edit the handler class by itself.

With the Create Handler Classes for Interface wizard, you can separate handler classes from the implementation classes in which the workbench generated them. The workbench creates those handler classes in new files. The workbench also adds @Handler annotations to the interfaces.

Procedure

To create a handler classes from an implementation class and place it in a separate file:

  1. In the Package Explorer, right-click the source file in which the interface is defined and select Data Access Development > Create Handler Classes. The Create Handler Classes for Interface opens.
  2. Use the controls in the wizard to specify the handler classes that you want to create and where you want to place them in your Java project.
  3. Click Finish. The handler classes appear in your project. On the next build of the project, the workbench regenerates the implementation class and does not include the handler classes, unless you removed the @generated tag from the implementation class. Removing this tag causes the workbench not to regenerate the implementation class.

Example

For example, suppose that an interface contains this annotated method:
	@Select(sql = "SELECT PRODUCT_NUMBER, BASE_PRODUCT_NUMBER, INTRODUCTION_DATE,"
	               + "  DISCONTINUED_DATE, PRODUCT_TYPE_CODE, PRODUCT_COLOR_CODE, PRODUCT_SIZE_CODE,"
	               + "  PRODUCT_BRAND_CODE, PRODUCTION_COST, GROSS_MARGIN, PRODUCT_IMAGE"
	               + "  FROM GOSALES.PRODUCT")
	Iterator<Product> getProducts();

The implementation class for the interface contains an @generated tag and the following handler class for this method:

  public static class GetProductsRowHandler extends BaseRowHandler<Product>
   {
     public Product handle (java.sql.ResultSet rs, Product returnObject) throws java.sql.SQLException
     {
       returnObject = new Product ();
       returnObject.setProduct_number(getInt (rs, 1));
       returnObject.setBase_product_number(getInt (rs, 2));
       returnObject.setIntroduction_date(getTimestamp (rs, 3));
       returnObject.setDiscontinued_date(getTimestamp (rs, 4));
       returnObject.setProduct_type_code(getInt (rs, 5));
       returnObject.setProduct_color_code(getInt (rs, 6));
       returnObject.setProduct_size_code(getInt (rs, 7));
       returnObject.setProduct_brand_code(getInt (rs, 8));
       returnObject.setProduction_cost(getBigDecimal (rs, 9));
       returnObject.setGross_margin(getDouble (rs, 10));
       returnObject.setProduct_image(getString (rs, 11));

       return returnObject;
     }
   }

After you use the Create Handler Classes for Interface wizard, the workbench creates the file GetProductsRowHandler.java to contain the handler class. The workbench also regenerates the implementation class without the handler class. You can edit the handler class directly, without having to open the implementation class for editing.

In the interface, the method now has an @Handler annotation.

	@Handler(rowHandler=aforementioned.GetProductsRowHandler.class)
	@Select(sql = "SELECT PRODUCT_NUMBER, BASE_PRODUCT_NUMBER, INTRODUCTION_DATE,"
	               + "  DISCONTINUED_DATE, PRODUCT_TYPE_CODE, PRODUCT_COLOR_CODE, PRODUCT_SIZE_CODE,"
	               + "  PRODUCT_BRAND_CODE, PRODUCTION_COST, GROSS_MARGIN, PRODUCT_IMAGE"
	               + "  FROM GOSALES.PRODUCT")
	Iterator<Product> getProducts();

Feedback