Advanced Database Persistence for Java

Examples & Reference Manual

 

Example 18 - Custom DAO Property Names

This example shows how the DAO properties from tables and views can have a custom user-specified names, rather than the equivalent names coming directly from the database object.

Example Source Code

package examples;

import java.sql.Date;
import java.sql.SQLException;

import daos.VehicleValuationDAO;

/**
 * Example 18 - Custom Property Names for DAOs
 * 
 * @author Vladimir Alarcon
 * 
 */
public class Example18 {

  public static void main(String[] args) throws SQLException {

    System.out.println("=== Running Example 18 - Custom Property Names for DAOs ===");

    // 1. Insert using alias names for properties in Java
    // Example: the table MT_VVAL002 is managed as the Java DAO
    // VehicleValuationDAO. See the hotrod.xml configuration file to see the
    // SQL-Java property names equivalence.

    // Column "IF"
    // -----------
    // If no alias is used for the first column "IF", it would generate the java
    // property name "if". This is actually a reserved word in Java and,
    // therefore, the DAO source code would be invalid. In this case the
    // property MUST be given a java name.

    // Column "SLDBRID"
    // ----------------
    // This column would generate a valid Java property name. However, it's
    // somewhat confusing legacy name. It's given a java name "branchId" that is
    // far more easy to use and understand.

    // Column "$valuation"
    // -------------------
    // This column has a special non-alphanumeric character in its name. This
    // can cause all kinds of confusion in the Java source code and for MyBatis
    // as well. It's better to give it an easier java name: totalBranchValuation

    Date closingDate = Date.valueOf("2017-02-14");
    VehicleValuationDAO v = new VehicleValuationDAO();
    v.setValuationDate(closingDate); // column IF!
    v.setBranchId(104); // column SLDBRID!
    v.setTotalBranchValuation(156780.0); // column $valuation!
    v.insert(); 
    System.out.println(" ");
    System.out.println("1. Insert using alias names for properties in Java.");

    System.out.println(" ");
    System.out.println("=== Example 18 Complete ===");

  }

}

How to Run this example

The Example 18 is included in the download package. To run this example please refer to the section How to Run the Examples above.

Case #1: Java reserved word

If no alias is used for the column " IF ", it would generate the java property name " if ". This is actually a reserved word in Java and, therefore, the DAO source code would be invalid. In this case the property MUST be given a different java name.

In the configuration file the if property is given the java name valuationDate , as shown below:

    <column name="if" java-name="valuationDate" />

Case #2: Cryptic column name

The column name " SLDBRID " is a little bit cryptic and probably comes from a legacy system that we cannot modify. If we want to treat this column with a different name within our java application we can specify it in the configuration file as in:

    <column name="sldbrid" java-name="branchId" />

In this case it's much clearer for our Java application to use it as branchId in all getters and setters.

Case #3: Safer Java identifier

The column “ $valuation ” (probably from a legacy database) has a special character in its name and could produce issues when interacting with the DAO properties. It's better to give it a different java name so it can be managed easily as shown below:

    <column name="$valuation" java-name="totalBranchValuation" />

The property name totalBranchValuation is much safer than “ $valuation ”.