• Post Reply Bookmark Topic Watch Topic
  • New Topic

@ManyToMany: Removing all the associations?

Adriano Meis
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, (unluckily) I have a @ManyToMany,

MyUser (table MYUSER)
MyGroup (table MYGROUP)

I correctly defined a @ManyToMany and Hibernate generated a USER_GROUP table.

I need to remove all the existing associations between MyUser and MyGroup.
Current Solution
Now I'm using a entitymanager handled native query: "DELETE FROM USER_GROUP", for some reasons (very very long to explain...) I want to find a different and cleaner solution.

My first idea:
Since for what I should do, I need to remove both the user/group associations and all the groups too, I tried to set a Cascade Remove on the @ManyToMany, but this doesn't work.. if I execute HQL "delete from MyGroup" I receive an exception since a FK is violated.
And looking around seems that Cascade Remove should not be used with @ManyToMany...

1st Discarded idea
move to @OneToMany, @ManyToOne

2nd Discarded idea
not very performant..
iterating on all the groups and removing their associated users.

What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!