Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3931320
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:20:26+00:00 2026-05-19T23:20:26+00:00

I am using JPA 2.0 and hibernate. I have a User class and a

  • 0

I am using JPA 2.0 and hibernate. I have a User class and a Group class as follows:

public class User implements Serializable {
    @Id
    @Column(name="USER_ID")
    private String userId;

    @ManyToMany
    @JoinTable(name = "USER_GROUP",
               joinColumns = {
                   @JoinColumn(name = "GROUP_ID")
               },
               inverseJoinColumns = {
                   @JoinColumn(name = "USER_ID")
               }
    )
    private Set<Group> groupList;

    //get set methods
}

public class Group
{
    @Id
    @Column(name="GROUP_ID")
    private String groupId;

    @ManyToMany(mappedBy="groupList")
    private Set<User> memberList;
    //get set methods
}

And then, I create a user and group and then assign the user to the group.

What I want to have is when I delete the group, the group will be deleted (of course) and all the user-group relationship that the group has will be automatically deleted from the USER_GROUP join table but the user itself is not deleted from the USER table.

With the code I have above, only the row in the GROUP table will be deleted when I delete a group and the user will still have an entry to the deleted group in the USER_GROUP join table.

If I put cascade in the User class like this:

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name = "USER_GROUP",
joinColumns =
{
    @JoinColumn(name = "GROUP_ID")
},
inverseJoinColumns =
{
    @JoinColumn(name = "USER_ID")
})
private Set<Group> groupList;

When I delete the group, the user will be deleted as well!

Is there any way to achieve what I want?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:20:26+00:00Added an answer on May 19, 2026 at 11:20 pm

    The way you have it mapped, the User is the managing side of the relationship, therefore it will be responsible for updating the join table.

    Change the JoinTable mapping from User to Group, and make the groupList property of User so it has the mappedBy attribute. That will change the group to the managing side of the relationship, and make persist/update calls to the group manage the join table.

    But take note of that, you won’t be able to simply add a group to a user, save the user, and continue, you’ll instead have to add a user to the group and save the group to see the changes, but having the bi-directional many-to-many hopefully you’ll be closely managing that relationship anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using JPA and Hibernate I have an entity class @Entity @Table(name = "ROLES") public
I have following Hibernate 3 mapping for User class: <class name=org.test.User table=users> ... some
We have a setup where we are using an embedded HSQLDB for backing Hibernate/JPA
JPA/Hibernate missing column I am using JPA with Hibernate as the provider. I had
Is it possible to perform date arithmetic using JPA/Hibernate? For example, I have an
i have a strange situation: I'm using jpa/hibernate to get rows from a mySql
I have a table with images stored in it as BLOB. I'm using JPA/Hibernate.
I'm trying to set up the following tables using JPA/Hibernate: User: userid - PK
I have been using Hibernate Restrictions in JPA 1.0 ( Hibernate driver ). There
We are using JPA (hibernate) and have certain tables in a specific schema. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.