Skip Headers

# Partial Object Reading

Previous

Next

Partial object reading is a query designed to extract only the required information from a selected record in a database, rather than all the information the record contains. Because partial object reading does not fully populate objects, you can neither cache nor edit partially read objects.

For more information about partial object queries, see "Partial Object Queries".

In Example: No Optimization, the query builds complete employee objects, even though the list displays only employee last names. With no optimization, the query reads all the employee data.

No Optimization

    /* Read all the employees from the database, ask the user to choose one and return it. This must read in all the information for all the employees */
    List list;

    // Fetch data from database and add to list box
    Vector employees = (Vector) session.readAllObjects(Employee.class);
    list.addAll(employees);

    // Display list box
    ....

    // Get selected employee from list
    Employee selectedEmployee = (Employee) list.getSelectedItem();

    return selectedEmployee;

Example: Optimization Through Partial Object Reading demonstrates the use of partial object reading. It reads only the last name and primary key for the employee data. 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 names 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;