Advanced Database Persistence for Java

Examples & Reference Manual

 

Example 19 - Custom DAO Property Types

This example shows how the DAO properties from tables, views, and selects can be forced by the developer.

HotRod automatically produces Java types according to the database column type. Even though these Java types are usually adecuate to accomodate database values, sometimes the developer may have different preference. If this is the case, the type can be forced by the developer by specifying it in the configuration file.

Example Source Code

package examples;

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

import daos.VehicleValuationDAO;

/**
 * Example 19 - Custom Property Java Types for DAOs
 * 
 * @author Vladimir Alarcon
 * 
 */
public class Example19 {

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

    System.out.println("=== Running Example 19 - Custom Property Java Types for DAOs ===");

    // 1. Insert using custom property Java types

    // The table column "$valuation" used in Java as totalBranchValuation is
    // defined using the custom Java type java.lang.Double (See the hotrod.xml
    // configuration file). This overrides the Java type (java.math.BigDecimal)
    // provided by default for this database column type.

    Date closingDate = Date.valueOf("2017-02-14");
    VehicleValuationDAO v = new VehicleValuationDAO();
    v.setValuationDate(closingDate);
    v.setBranchId(104);
    v.setTotalBranchValuation(156780.0); // It's a Double value!
    v.insert();
    System.out.println(" ");
    System.out.println("1. Insert using custom property Java types.");

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

  }

}

How to Run this example

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

Case #1: Using a Double rathen than a java.math.BigDecimal

By default HotRod produces a column with the Java type java.math.BigDecimal for the column of type NUMERIC(10, 2) . However, in this case the developer prefers to treat this number as a java.lang.Double and specifies it in the configuration file as shown below:

    <column name="$valuation" java-name="totalBranchValuation"
      java-type="java.lang.Double" />