Skip Headers

# Using privateOwnedRelationship

Previous

Next

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: Specifying a Mapping as Privately Owned, 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.

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: Privately Owned Relationships.

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)