File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes @ManyToMany remove records from join rows Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "@ManyToMany remove records from join rows" Watch "@ManyToMany remove records from join rows" New topic

@ManyToMany remove records from join rows

Marcus Casey

Joined: Feb 25, 2006
Posts: 12
How do I remove a record from the join table in a many-to-many relationship?

I am using JPA. I have created a many-to-many relationship between users and groups. A MyUser can be in more than one MyGroup and a MyGroup can have more than one MyUser. Pretty straightforward.

Here is some of my code for MyUser:

and for MyGroup:

As you can tell from the code, I have a join table called my_user_group that just has the foreign keys from each table. (You may also notice it is unidirectional. I don't have a need to list the users by group, only the groups by user).

Now to my question--how do I remove a group from a user? I notice that if I remove a group from the List on my object and the perform a merge(), the foreign key records for the removed group are still there.

Any suggestions would be most welcome.
Mike Keith
Ranch Hand

Joined: Jul 14, 2005
Posts: 304

You are checking for the rows after committing the transaction, right? The rows won't typically be removed until commit-time. Maybe if you could post your code I could see what else could be going wrong. Your mappings look okay.

Pro JPA 2: Mastering the Java Persistence API
I agree. Here's the link:
subject: @ManyToMany remove records from join rows
It's not a secret anymore!