You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by C N Davies <cn...@cndavies.com> on 2009/08/03 20:08:46 UTC

Shouldn't cascade all delete orphans?

I'll 90% sure this worked ok when I was using Hibernate but doesn't seem to
work on OpenJPA.  I have an entity which has a collection contained in it,
each of the items of this collection are entities so the collection member
is annotated as cascade ALL.  If I programmatically remove one of the
entities from the collection then persist the main entity, the object that
was references in the list is not deleted from the DB. Is this the expected
behaviour? 

 

Here is a sample of what I mean:

 

Here is the main entity which as you see has a collection of Assets in it.

 

Public class AssetRegister{

      @OneToMany(cascade={CascadeType.ALL})

      private List<Asset> assets;

}

 

In my code I might do like this:

 

AssetRegister ar =  <find asset register code>

 

List<Asset> assets = ar.getAssets();

Assets.delete(<some asset>);

Assets.setAssets(assets);

 

 

The add and the delete to/from the collection works fine and the join in the
asset register table is removed however the asset entity itself is not
removed from the asset table and since it is a OneToMany relationship the
asset is now orphaned.

 

I know I can programmatically delete the asset entity but  I want to know if
this the expected behaviour?

 

Thanks for any advice

 

Chris

 


Re: Shouldn't cascade all delete orphans?

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi Chris,

Cascade ALL should not, and OpenJPA does not, include remove orphans.  
Simply removing an Entity from a relationship should not normally  
cause a life cycle change in the Entity.

There should be, and is in OpenJPA, a separate annotation for this  
kind of relationship. IIRC, remove orphans is being added in the JPA  
2.0 spec.

Regards,

Craig

On Aug 3, 2009, at 11:08 AM, C N Davies wrote:

> I'll 90% sure this worked ok when I was using Hibernate but doesn't  
> seem to
> work on OpenJPA.  I have an entity which has a collection contained  
> in it,
> each of the items of this collection are entities so the collection  
> member
> is annotated as cascade ALL.  If I programmatically remove one of the
> entities from the collection then persist the main entity, the  
> object that
> was references in the list is not deleted from the DB. Is this the  
> expected
> behaviour?
>
>
>
> Here is a sample of what I mean:
>
>
>
> Here is the main entity which as you see has a collection of Assets  
> in it.
>
>
>
> Public class AssetRegister{
>
>      @OneToMany(cascade={CascadeType.ALL})
>
>      private List<Asset> assets;
>
> }
>
>
>
> In my code I might do like this:
>
>
>
> AssetRegister ar =  <find asset register code>
>
>
>
> List<Asset> assets = ar.getAssets();
>
> Assets.delete(<some asset>);
>
> Assets.setAssets(assets);
>
>
>
>
>
> The add and the delete to/from the collection works fine and the  
> join in the
> asset register table is removed however the asset entity itself is not
> removed from the asset table and since it is a OneToMany  
> relationship the
> asset is now orphaned.
>
>
>
> I know I can programmatically delete the asset entity but  I want to  
> know if
> this the expected behaviour?
>
>
>
> Thanks for any advice
>
>
>
> Chris
>
>
>

Craig L Russell
Architect, Sun Java Enterprise System http://db.apache.org/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!