Skip Headers

# Associating New Objects With One Another

Previous

Next

At commit time, TopLink can determine if an object is new or not. As described in "Associating a New Target to an Existing Source Object", if a new object is reachable from a clone, you do not need to register it. TopLink effectively uses the `registerNewObject` method on all new objects it can reach from registered objects.

When working with new objects, remember the following rules:

- Only reachable or registered objects will be persisted.

- New objects or objects that have been registered with `registerNewObject` are considered to be working copies in the unit of work.

- If you call `registerObject` with a new object, the clone, and the argument, is considered the cache version.

Example: Associating New Objects with the registerNewObject Method shows how to associate new objects with the `registerNewObject` method.

Associating New Objects with the registerNewObject Method

    UnitOfWork uow = session.acquireUnitOfWork();
        Pet newPet = new Pet();
        newPet.setId(150);
        newPet.setType("Horse");
        newPet.setName("Ed");

        PetOwner newPetOwner = new PetOwner();
        newPetOwner.setId(250);
        newPetOwner.setName("George");
        newPetOwner.setPhoneNumber("555-9999");

        VetVisit newVetVisit = new VetVisit();
        newVetVisit.setId(350);
        newVetVisit.setNotes("Talks a lot");
        newVetVisit.setSymptoms("Sore throat");

        newPet.getVetVisits().addElement(newVetVisit);
        newVetVisit.setPet(newPet);
        newPet.setPetOwner(newPetOwner);

        uow.registerNewObject(newPet);
    uow.commit();

However, after the unit of work commit, do not use the objects in the variables `newPet`, `newPetOwner`, and `newVetVisit` because they are technically copies from the unit of work.

If you need a handle to the cache version of these objects, query for them or use the unit of work as shown in Example: Associating New Objects with the newObjectMethod and Retaining a Handle to the Cache Objects.

Associating New Objects with the newObjectMethod and Retaining a Handle to the Cache Objects

    UnitOfWork uow = session.acquireUnitOfWork();
        Pet newPet = new Pet();
        Pet newPetClone = (Pet)uow.registerObject(newPet);
        newPetClone.setId(150);
        newPetClone.setType("Horse");
        newPetClone.setName("Ed");

        PetOwner newPetOwner = new PetOwner();
        PetOwner newPetOwnerClone =
            (PetOwner)uow.registerObject(newPetOwner);
        newPetOwnerClone.setId(250);
        newPetOwnerClone.setName("George");
        newPetOwnerClone.setPhoneNumber("555-9999");

        VetVisit newVetVisit = new VetVisit();
        VetVisit newVetVisitClone =
            (VetVisit)uow.registerObject(newVetVisit);
        newVetVisitClone.setId(350);
        newVetVisitClone.setNotes("Talks a lot");
        newVetVisitClone.setSymptoms("Sore throat");

        newPetClone.getVetVisits().addElement(newVetVisitClone);
        newVetVisitClone.setPet(newPetClone);
        newPetClone.setPetOwner(newPetOwnerClone);
    uow.commit();