After some investigation it looks like the path of least resistance is to just use SQL Alter Table to add the foreign keys with delete cascade (I'm using forward mapping to create the database). It is not clear to me how to use the @ForeignKey annotation with a ManyToMany relation (possibly requires using the ContainerTable annotation also?). I also tried setting the MappingDefaults property ForeignKeyDeleteAction value but this added foreign keys on all the OneToMany and OneToOne relations and changed the generated SQL forward mapping in other ways. At this point the annotations seem less user friendly than SQL alter table.

As far as I can tell the only JPA "standard" way to do this is to remove the elements one at a time from the owner side collection of the ManyToMany relation before removing the owner entity.

- Paul

On 4/3/2009 11:38 AM, Paul Copeland wrote:
Looks like I can use the OpenJPA @ForeignKey annotation deleteAction=CASCADE - will give it a try.

On 4/3/2009 11:18 AM, Paul Copeland wrote:
This is from the JPA spec - Sounds like cascade=REMOVE should not be used on ManyToMany. However I'm recalling that some databases do provide DDL for the kind of cascade delete behavior that I want.

2.1.7 - "The relationship modeling annotation constrains the use of the cascade=REMOVE specification. The cascade=REMOVE specification should only be applied to associations that are specified as One- ToOne or OneToMany. Applications that apply cascade=REMOVE to other associations are not portable."

On 4/3/2009 10:43 AM, Paul Copeland wrote:
Will CascadeType.REMOVE on a ManyToMany mapping remove the join table row? Even if the Collections has not been loaded lazily? Or do I have to iterate over the Collection and remove each element before removing the enity? I do NOT want to remove the referenced Entity on the other side of the ManyToMany association, just the row in the join table.

@ManyToMany (fetch=FetchType.LAZY, cascade={CascadeType.PERSIST,CascadeType.REMOVE})

- Paul











Reply via email to