Skip Headers

# Reading Objects Using Partial Object Queries

Previous

Next

Example: Optimization Through Partial Object Reading demonstrates the use of partial object reading. It reads only the last name and primary key for the employees. This reduces the amount of data read from the database.

Optimization Through Partial Object Reading

    /* Read all the employees from the database, ask the user to choose one and return it. This uses partial object reading to read just the last name of the employees. Since TopLink automatically includes the primary key of the object, the full object can easily be read for editing */
    List list;
    // Fetch data from database and add to list box
    ReadAllQuery query = new ReadAllQuery(Employee.class);
    query.addPartialAttribute("lastName");
    // The next line avoids a query exception
    query.dontMaintainCache();
    Vector employees = (Vector) session.executeQuery(query);
    list.addAll(employees);

    // Display list box
    ....
    // Get selected employee from list
    Employee selectedEmployee = (Employee)session.readObject(list.getSelectedItem());
    return selectedEmployee;