Advanced Database Persistence for Java

Examples & Reference Manual

 

Example 8 - Selecting on Views

This example shows how to select rows from a view using the out-of-the-box Select by Example functionality.

Example Source Code

package examples;

import java.sql.SQLException;
import java.util.List;

import daos.CarDAO;
import daos.primitives.CarDAOPrimitives.CarDAOOrderBy;

/**
 * Example 08 - Select on Views
 * 
 * @author Vladimir Alarcon
 * 
 */
public class Example08 {

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

    System.out.println("=== Running Example 08 - Select on Views ===");
    CarDAO example;

    // 1. Select by Example
    // Example: Find all vehicles of brand Toyota on the view CAR

    example = new CarDAO();
    example.setBrand("Toyota");
    List<CarDAO> cars1 = CarDAO.selectByExample(example);
    Utilities.displayCars("1. Select by Example:", cars1);

    // 2. Select by Example - Using Ordering
    // Example: Find all vehicles of brand Toyota on the view CAR, ordered by
    // model, then descending by mileage

    example = new CarDAO();
    example.setBrand("Toyota");
    List<CarDAO> cars2 = CarDAO.selectByExample(example, CarDAOOrderBy.MODEL, CarDAOOrderBy.MILEAGE$DESC);
    Utilities.displayCars("2. Select by Example - Using Ordering:", cars2);

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

  }

}

How to Run this example

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

Case #1: Select on a view

To select using column as filters you must instantiate a new DAO to use as the example and set the desired value on the property(ies) you want to filter by. Leave the rest of the properties untouched.

This is demonstrated selecting from the view CAR .

Case #2: Select on a view using ordering

To select using column as filters you must instantiate a new DAO to use as the example and set the desired value on the property(ies) you want to filter by. Leave the rest of the properties untouched. Add extra parameters to the method call to specify the desired ordering.

This is demonstrated selecting from the view CAR .