You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Daryl Stultz <da...@6degrees.com> on 2009/10/14 18:49:32 UTC

ManyToMany delete

Hello, I have a ManyToMany relation of A to B like so:

in A entity:

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "special_a_to_b_table",
   joinColumns = { @JoinColumn(name = "a_id", referencedColumnName = "a_id")
},
   inverseJoinColumns = { @JoinColumn(name = "b_id", referencedColumnName =
"b_id") }
   )

private List<B> specialBList;

The special_a_to_b_table is a simple join table with foreign keys to A and
B.

I was thrilled to find that adding B instances to specialBList and merging
the A instance inserted the special_a_to_b_table records. I was horrified to
find that deleting the A instance deletes the B instances as well! I don't
want to delete the B instances but the special_a_to_b_table records
representing the join. What is the right way to do this? For now I have
removed CascadeType.DELETE from the mix and I'm using a foreign key cascade
in the database.

Thanks.

--Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: ManyToMany delete

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Oct 14, 2009 at 1:03 PM, Luis Fernando Planella Gonzalez <
lfpg.dev@gmail.com> wrote:

> Either ALL or DELETE will delete the related entities when deleting the
> owner.
> You can specify the other cascade types, such as:
> cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}
>
> I've set it to the CascadeType.MERGE as a test. Removing the A instance now
removes the special_a_to_b_table records before deleting the A record
without deleting the B's. I was under the impression
that special_a_to_b_table records would not be deleted without
CascadeType.DELETE included. Lesson learned. Thanks.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: ManyToMany delete

Posted by Luis Fernando Planella Gonzalez <lf...@gmail.com>.
Either ALL or DELETE will delete the related entities when deleting the owner.
You can specify the other cascade types, such as:
cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}

--
Luis Fernando Planella Gonzalez


Em Quarta-feira 14 Outubro 2009, às 13:49:32, Daryl Stultz escreveu:
> Hello, I have a ManyToMany relation of A to B like so:
> 
> in A entity:
> 
> @ManyToMany(cascade = CascadeType.ALL)
> @JoinTable(name = "special_a_to_b_table",
>    joinColumns = { @JoinColumn(name = "a_id", referencedColumnName =
>  "a_id") },
>    inverseJoinColumns = { @JoinColumn(name = "b_id", referencedColumnName =
> "b_id") }
>    )
> 
> private List<B> specialBList;
> 
> The special_a_to_b_table is a simple join table with foreign keys to A and
> B.
> 
> I was thrilled to find that adding B instances to specialBList and merging
> the A instance inserted the special_a_to_b_table records. I was horrified
>  to find that deleting the A instance deletes the B instances as well! I
>  don't want to delete the B instances but the special_a_to_b_table records
>  representing the join. What is the right way to do this? For now I have
>  removed CascadeType.DELETE from the mix and I'm using a foreign key
>  cascade in the database.
> 
> Thanks.
> 
> --Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
>