persistence@glassfish.java.net

Re: TopLink equivalent to Hibernate delete-orphan cascade type

From: Gordon Yorke <gordon.yorke_at_oracle.com>
Date: Fri, 05 Jan 2007 08:14:22 -0500
Hello Jon,
     TopLink does have functionality to remove collection elements that have been removed  from the collection TopLink refers to this as "private ownership".  However this functionality is not yet exposed though the Persistence APIs.  Currently the easiest way to configure this is to create a customizer class. Within the customizer find the descriptor for the Class with the One-to-Many mapping. Get the mapping from the descriptor and set the mapping to be privately owned.
     Please note that this "Private Ownership"  may not match fully with "delete-orphan". 'Private Ownership "setting means none of the related objects can exist without the parent so when the parent is deleted the related private owned objects will also be deleted. The private owned objects will also be deleted when they are removed (or moved from) the parent's collection.
 --Gordon

Persistence.xml entry:
       <property name="toplink.session.customizer" value="mypackage.PrivateOwnedCustomizer"/>

Customizer Class :

package mypackage;
import oracle.toplink.essentials.sessions.Session;
import oracle.toplink.essentials.mappings.OneToManyMapping;
/**
 * PUBLIC:
 * This interface is to allow extra customization on a TopLink Session
 */

public class PrivateOwnedCustomizer extends SessionCustomizer {

    public void customize(Session session) throws Exception {

        RelationalDescriptor entityDescriptor = (RelationalDescriptor) session.getDescriptor(domainmodel.MyEntity.class);
        OneToManyMapping mapping = (
OneToManyMapping)entityDescriptor.getMappingForAttributeName("<propertyName>");
        mapping.privateOwnedRelationship();
    }
}



Jon Miller wrote:
Hi all,

Hibernate has a cascade type named "delete-orphan" that will delete entities from the database that were removed from an entity's collection when it is persisted. I don't see an equivalent in JPA. I take it you just have to do it manually? Or, is there a TopLink extension that will do this?

Jon