TopLink equivalent to Hibernate delete-orphan cascade type

View: New views
10 Messages — Rating Filter:   Alert me  

TopLink equivalent to Hibernate delete-orphan cascade type

by Jon Miller-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: TopLink equivalent to Hibernate delete-orphan cascade type

by Jon Miller-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

One other dumb question: Is this a "user" list intended for asking
questions, or, is it supposed to be just for developers working on the
internals of TopLink itself? Normally, Apache has -user and -dev lists, but,
I'm not sure if that's how java.net works or not...

Jon

----- Original Message -----
From: "Jon Miller" <jemiller@...>
To: "Glassfish Persistence List" <persistence@...>
Sent: Thursday, January 04, 2007 12:17 PM
Subject: TopLink equivalent to Hibernate delete-orphan cascade type


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

Re: TopLink equivalent to Hibernate delete-orphan cascade type

by Marina Vatkina :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Jon,

This list is used for both types of discussions. -user and -dev lists exist
for the whole GlassFish project, there are no separate lists for persistence.

thanks,
-marina

Jon Miller wrote:

> One other dumb question: Is this a "user" list intended for asking
> questions, or, is it supposed to be just for developers working on the
> internals of TopLink itself? Normally, Apache has -user and -dev lists,
> but, I'm not sure if that's how java.net works or not...
>
> Jon
>
> ----- Original Message ----- From: "Jon Miller" <jemiller@...>
> To: "Glassfish Persistence List" <persistence@...>
> Sent: Thursday, January 04, 2007 12:17 PM
> Subject: TopLink equivalent to Hibernate delete-orphan cascade type
>
>
>> 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
>>

Re: TopLink equivalent to Hibernate delete-orphan cascade type

by Markus Fuchs :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Jon,

the best way to mimic this behavior in JPA is the following. Instead of

Parent#removeChild(Child c) {
 this.childCollection.remove(c); //Also implies that c is deleted from the database
}

Do

Parent#removeChild(Child c) {
 this.childCollection.remove(c);
 *em.remove(c); // Explicitly remove the child entity*
}

And change the cascade setting on the parent side to, e.g.

HIBERNATE: cascade="all-delete-orphan" ==> JPA: CASCADE=ALL

-- markus.

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

Re: TopLink equivalent to Hibernate delete-orphan cascade type

by Gordon Yorke-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: TopLink equivalent to Hibernate delete-orphan cascade type

by Jon Miller-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Markus. I don't think that will work for me though because I want it
to work using web services as well. i.e. a web service client will receive
an entity from a web service containing a collection, it will modify the
collection and pass the object back to the service, the service will then
persist it, removing any objects in the database that are no longer in the
collection. I want to design it so that it works both with and without web
services. The objects may or may not already be associated with the
persistence context.

Jon

----- Original Message -----
From: "Markus Fuchs" <Markus.Fuchs@...>
To: <persistence@...>
Sent: Friday, January 05, 2007 5:10 AM
Subject: Re: TopLink equivalent to Hibernate delete-orphan cascade type


> Hi Jon,
>
> the best way to mimic this behavior in JPA is the following. Instead of
>
> Parent#removeChild(Child c) {
> this.childCollection.remove(c); //Also implies that c is deleted from
> the database
> }
>
> Do
>
> Parent#removeChild(Child c) {
> this.childCollection.remove(c);
> *em.remove(c); // Explicitly remove the child entity*
> }
>
> And change the cascade setting on the parent side to, e.g.
>
> HIBERNATE: cascade="all-delete-orphan" ==> JPA: CASCADE=ALL
>
> -- markus.
>
> 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
>

Re: TopLink equivalent to Hibernate delete-orphan cascade type

by Jon Miller-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...>
To: <persistence@...>
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
>

Re: TopLink equivalent to Hibernate delete-orphan cascade type

by Jon Miller-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

One other question, you mentioned "this functionality is not yet exposed
though the Persistence APIs." Does this mean that this functionality is
planned for a future version of JPA?

Jon

----- Original Message -----
From: "Jon Miller" <jemiller@...>
To: <persistence@...>
Sent: Monday, January 08, 2007 1:06 PM
Subject: Re: TopLink equivalent to Hibernate delete-orphan cascade type


> 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@...>
> To: <persistence@...>
> 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
>>
>

RE: TopLink equivalent to Hibernate delete-orphan cascade type

by Gordon Yorke-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Jon,
    I do not know if there are any plans to include this functionality within the JPA specification.  What is planned is for TopLink to expose this behaviour through JPA extensions in the form of annotations and proprietary interfaces.
--Gordon

-----Original Message-----
From: Jon Miller [mailto:jemiller@...]
Sent: Monday, January 08, 2007 4:49 PM
To: persistence@...
Subject: Re: TopLink equivalent to Hibernate delete-orphan cascade type


One other question, you mentioned "this functionality is not yet exposed
though the Persistence APIs." Does this mean that this functionality is
planned for a future version of JPA?

Jon

----- Original Message -----
From: "Jon Miller" <jemiller@...>
To: <persistence@...>
Sent: Monday, January 08, 2007 1:06 PM
Subject: Re: TopLink equivalent to Hibernate delete-orphan cascade type


> 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@...>
> To: <persistence@...>
> 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
>>
>

Re: TopLink equivalent to Hibernate delete-orphan cascade type

by Jon Miller-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

OK, thanks.

Jon

----- Original Message -----
From: "Gordon Yorke" <gordon.yorke@...>
To: <persistence@...>
Sent: Monday, January 08, 2007 4:00 PM
Subject: RE: TopLink equivalent to Hibernate delete-orphan cascade type


> Hello Jon,
>    I do not know if there are any plans to include this functionality
> within the JPA specification.  What is planned is for TopLink to expose
> this behaviour through JPA extensions in the form of annotations and
> proprietary interfaces.
> --Gordon
>
> -----Original Message-----
> From: Jon Miller [mailto:jemiller@...]
> Sent: Monday, January 08, 2007 4:49 PM
> To: persistence@...
> Subject: Re: TopLink equivalent to Hibernate delete-orphan cascade type
>
>
> One other question, you mentioned "this functionality is not yet exposed
> though the Persistence APIs." Does this mean that this functionality is
> planned for a future version of JPA?
>
> Jon
>
> ----- Original Message -----
> From: "Jon Miller" <jemiller@...>
> To: <persistence@...>
> Sent: Monday, January 08, 2007 1:06 PM
> Subject: Re: TopLink equivalent to Hibernate delete-orphan cascade type
>
>
>> 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@...>
>> To: <persistence@...>
>> 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
>>>
>>
>