Skip Headers

# Associating With Reference to the Cache Object

Previous

Next

Example: Associating With Reference to the Cache Object shows how to associate a new target with an existing source with reference to the cache object.

Associating With Reference to the Cache Object

    UnitOfWork uow = session.acquireUnitOfWork();
        Pet petClone = (Pet)uow.readObject(Pet.class);

        PetOwner petOwner = new PetOwner();
        PetOwner petOwnerClone = (PetOwner)uow.registerObject(petOwner);
        petOwnerClone.setId(400);
        petOwnerClone.setName("Donald Smith");
        petOwnerClone.setPhoneNumber("555-1212");

        VetVisit vetVisit = new VetVisit();
        VetVisit vetVisitClone = (VetVisit)uow.registerObject(vetVisit);
        vetVisitClone.setId(500);
        vetVisitClone.setNotes("Pet was shedding a lot.");
        vetVisitClone.setSymptoms("Pet in good health.");
        vetVisitClone.setPet(petClone);

        petClone.setPetOwner(petOwnerClone);
        petClone.getVetVisits().addElement(vetVisitClone);
    uow.commit();

Now, after the unit of work commit transaction:

    petOwner == session.readObject(petOwner)

This means that we have a handle to the cache copy after the commit transaction, rather than a clone.

Example: Resolving Issues When Adding New Objects shows how to use unit of work method `registerNewObject` to add a new object when a bidirectional relationship exists. For more information, see "Using registerNewObject".

Note: You cannot use `UnitOfWork` methods `registerObject`, `registerNewObject`, or `registerExistingObject` with an aggregate object (see "Relational Aggregate Descriptors"). Doing so will raise a ValidationException or other errors at commit time. For more information, see "Working With Aggregates".

Resolving Issues When Adding New Objects

    // Get an employee read from the parent session of the unit of work
    Employee manager = (Employee)session.readObject(Employee.class);

    // Acquire a unit of work
    UnitOfWork uow = session.acquireUnitOfWork();

    // Register the manager to get its clone
    Employee managerClone = (Employee)uow.registerObject(manager);

    // Create a new employee
    Employee newEmployee = new Employee();
    newEmployee.setFirstName("Spike");
    newEmployee.setLastName("Robertson");

    /* INCORRECT: Do not associate the new employee with the original manager. This will cause a QueryException when TopLink detects this error during  commit */
    //newEmployee.setManager(manager);

    /* CORRECT: Associate the new object with the clone. Note that in this example, the setManager method is maintaining the bidirectional managedEmployees relationship and adding the new employee to its managedEmployees. At commit time, the unit of work will detect that this is a new object and will take the appropriate action */
    newEmployee.setManager(managerClone);

    /* INCORRECT: Do not register the newEmployee: this will create two copies and cause a QueryException when TopLink detects this error during commit */
    //uow.registerObject(newEmployee);

    /* CORRECT:
    In the above setManager call, if the managerClone's managedEmployees was not
    maintained by the setManager method, then you should call registerObject before the new employee is related to the manager. If in doubt, you could use the  registerNewObject method to ensure that the newEmployee is registered in the unit of work. The registerNewObject method registers the object, but does not make a clone */
    uow.registerNewObject(newEmployee);

    // Commit the unit of work
    uow.commit();