Skip Headers

Oracle TopLink Developer's Guide 10g Release 3 (10.1.3) B13593-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Deleting Objects

To delete objects in a unit of work, use the `deleteObject` or `deleteAllObjects` method. When you delete an object that is not already registered in the unit of work, the unit of work registers the object automatically.

When you delete an object, TopLink deletes the object's privately owned child parts, because those parts cannot exist without the owning (parent) object. At commit time, the unit of work generates SQL to delete the objects, taking database constraints into account.

When you delete an object, you must take your object model into account. You may need to set references to the deleted object to null (for an example, see "Using privateOwnedRelationship").

This section explains how to delete objects within a unit of work, including the following:

- Using privateOwnedRelationship

- Explicitly Deleting from the Database

- Understanding the Order in Which Objects Are Deleted

## Using privateOwnedRelationship

Relational databases do not have garbage collection like a Java Virtual Machine (JVM) does. To delete an object in Java you just remove the reference to the object. To delete a row in a relational database, you must explicitly delete it. Rather than tediously manage when to delete data in the relational database, use the mapping attribute `privateOwnedRelationship` to have TopLink manage the garbage collection in the relational database for you.

As shown in Example 101-11, when you create a mapping using Java, use its `privateOwnedRelationship` method to tell TopLink that the referenced object is privately owned: that is, the referenced child object cannot exist without the parent object.

Example 101-11 Specifying a Mapping as Privately Owned

    OneToOneMapping petOwnerMapping = new OneToOneMapping();
    petOwnerMapping.setAttributeName("petOwner");
    petOwnerMapping.setReferenceClass(com.top.uowprimer.model.PetOwner.class);
    petOwnerMapping.privateOwnedRelationship();
    petOwnerMapping.addForeignKeyFieldName("PET.PET_OWN_ID", "PETOWNER.ID");
    descriptor.addMapping(petOwnerMapping);

When you create a mapping using TopLink Workbench, you can select the Private Owned check box under the General tab.

When you tell TopLink that a relationship is privately owned, you are specifying that:

- If the source of a privately owned relationship is deleted, then delete the target.

- If you remove the reference to a target from a source, then delete the target.

Do not configure privately owned relationships to objects that might be shared. An object should not be the target in more than one relationship if it is the target in a privately owned relationship.

The exception to this rule is the case when you have a many-to-many relationship in which a relation object is mapped to a relation table and is referenced through a one-to-many relationship by both the source and the target. In this case, if the one-to-many mapping is configured as privately owned, then when you delete the source, all the association objects will be deleted.

Consider the example shown in Example 101-12.

Example 101-12 Privately Owned Relationships

    // If the Pet-PetOwner relationship is privateOwned
    // then the PetOwner will be deleted at uow.commit()
    // otherwise, just the foreign key from PET to PETOWNER will
    // be set to null.  The same is true for VetVisit
    UnitOfWork uow = session.acquireUnitOfWork();
        Pet petClone = (Pet)uow.readObject(Pet.class);
        petClone.setPetOwner(null);
        VetVisit vvClone =
            (VetVisit)petClone.getVetVisits().firstElement();
        vvClone.setPet(null);
        petClone.getVetVisits().removeElement(vvClone);
    uow.commit();

If the relationships from `Pet` to `PetOwner` and from `Pet` to `VetVisit` are not privately owned, this code produces the following SQL:

    UPDATE PET SET PET_OWN_ID = NULL WHERE (ID = 150)
    UPDATE VETVISIT SET PET_ID = NULL WHERE (ID = 350)

If the relationships are privately owned, this code produces the following SQL:

    UPDATE PET SET PET_OWN_ID = NULL WHERE (ID = 150)
    UPDATE VETVISIT SET PET_ID = NULL WHERE (ID = 350)
    DELETE FROM VETVISIT WHERE (ID = 350)
    DELETE FROM PETOWNER WHERE (ID = 250)

## Explicitly Deleting from the Database

If there are cases where you have objects that will not be garbage collected through privately owned relationships (especially root objects in your object model), then you can explicitly tell TopLink to delete the row representing the object using the `deleteObject` API, as shown in Example 101-13.

Example 101-13 Explicitly Deleting

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

The preceding code generates the following SQL:

    DELETE FROM PET WHERE (ID = 100)

## Understanding the Order in Which Objects Are Deleted

The unit of work does not track changes or the order of operations. It is intended to insulate you from having to modify your objects in the order the database requires.

By default, at commit time, the unit of work correctly puts in order all insert and update operations using the constraints defined by your schema. After all insert and update operations are done, the unit of work will issue the necessary delete operations.

Constraints are inferred from one-to-one and one-to-many mappings. If you have no such mappings, you can add additional constraint knowledge to TopLink as described in "Controlling the Order of Delete Operations".