persistence@glassfish.java.net

Re: TopLink equivalent to Hibernate delete-orphan cascade type

From: Jon Miller <jemiller_at_uchicago.edu>
Date: Mon, 8 Jan 2007 13:06:46 -0600

Thanks Gordon, good to know. I'll keep that in mind. The more I think about
it though, I'm thinking that if possible I want to try to figure out a way
to do it that is provider independent. I'm thinking that maybe I can do
something like query the database to see what's already in the database and
then if anything isn't in the collection delete it. I vaguely remember
trying this a long time ago and having problems. I think the issue was that
the changes were flushed when I did the query, so, I had to change the
FlushMode. I need to look into it again...

Jon

----- Original Message -----
From: "Gordon Yorke" <gordon.yorke_at_oracle.com>
To: <persistence_at_glassfish.dev.java.net>
Sent: Friday, January 05, 2007 7:14 AM
Subject: Re: TopLink equivalent to Hibernate delete-orphan cascade type


> 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
>