Skip Headers

# Registering a New Object With registerNewObject

Previous

Next

The `registerNewObject` method registers a new object as if it was a clone. At commit time, the unit of work creates another instance of the object to be the cache version of that object.

Use `registerNewObject` in situations where:

- You do not need a handle to the cache version of the object after the commit transaction and you do not want to work with clones of new objects.

- You must pass a clone into the constructor of a new object, then register the new object.

Example: Registering a New Object with the registerNewObject Method shows how to register a new object with the `registerNewObject` method.

Registering a New Object with the registerNewObject Method

    UnitOfWork uow = session.acquireUnitOfWork();
        PetOwner existingPetOwnerClone =
            PetOwner)uow.readObject(PetOwner.class);

        Pet newPet = new Pet();
        newPet.setId(900);
        newPet.setType("Lizzard");
        newPet.setName("Larry");
        newPet.setPetOwner(existingPetOwnerClone);

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

By using `registerNewObject`, the variable `newPet` should not be used after the unit of work is committed. The new object is the clone and if you need the cache version of the object, you need to query for it. If you needed a handle to the cache version of the `Pet` after the unit of work has been committed, then you should use the first approach described in "Associating a New Source to an Existing Target Object". In that example, the variable `newPet` is the cache version after the unit of work is committed.