Skip Headers

# Disabling the Identity Map Cache During a Write Query

Previous

Next

When you write objects to the database, TopLink copies them to the session cache by default. To disable this within a query, call the `dontMaintainCache` method within the query. This improves query performance when you insert objects into the database, but must be used only on objects that will not be required later by the application.

Disabling the Identity Map Cache During a Write Query

Example: Disabling the Identity Map Cache During a Write Query reads all the objects from a flat file and writes new copies of the objects into a table.

    // Reads objects from an employee file and writes them to the employee table
    void createEmployeeTable(String filename, Session session) {
       Iterator iterator;
       Employee employee;
       // Read the employee data file
       List employees = Employee.parseFromFile(filename);
       Iterator iterator = employees.iterator();
       while (iterator.hasNext()) {
          Employee employee = (Employee) iterator.next();
          InsertObjectQuery query = new InsertObjectQuery();
          query.setObject(employee);
          query.dontMaintainCache();
          session.executeQuery(query);
       }
    }

Note: Disable the identity map only when object identity is unimportant in subsequent operations.