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 7966337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:23:52+00:00 2026-06-04T06:23:52+00:00

This is a question about Hibernate’s generated sql about deleting one relationship under many-to-many

  • 0

This is a question about Hibernate’s generated sql about deleting one relationship under many-to-many mapping, not ‘cascade’ problem.

I use JPA 2 and hibernate as its implementation.

I have two models, User and Role. One user can have many role, and one role can have many users, so they are many-to-many mapping:

@Entity
class User{
    @Id Long id;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
    @JoinTable(name = "user_role", inverseJoinColumns = @JoinColumn(name = "role_id"),
                joinColumns = @JoinColumn(name = "user_id"))
    private List<Role> roles;
 }

@Entity
class Role{
    @Id Long id;

    @ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.REFRESH, mappedBy = "roles")
    private List<User> users; 
 }

and the mapping works well , hibernate auto create three tables for this mapping

table user
table role
table user_role

Now here is the problem, what I want is just remove one role from one user (not remove a user or a role, just one relation between one user and one role, means only need remove one record from the table user_role). Here is the code I tried:

public void removeOneRoleFromUser(long userId, long roleId){
    User user = userService.getById(userId);
    Role role = roleService.getById(roleId);
    user.getRoles().remove(role);     //here
    userService.update(user);
}

when I execute this code it work, the role was removed the from the user indeed. But when I check the sql which hibernate generated for it, it’s not what I expected, The hibernate generated sql is:

delete from user_role where user_id = {userId}
insert into user_role values({user_id}, {role_id_not_removed}) 
...
insert into user_role values({user_id}, {another_role_id_not_removed}) 

So for deleting one role from one user, hibernate first delete all roles from the user, then add those role which should not be removed back to the user one by one.

And what I expect is just one sql sentence archive it:

delete from user_role where user_id = {userId} and role_id = {role_id}

I know there is some other ways I can archive this like introducing another entity UserRoleMapping which mapping to the table user_role, then directly remove one UserRoleMapping instance will remove one role from one user; but I want to know is there any solution I can get the expect with the current solution.

  • 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-06-04T06:23:53+00:00Added an answer on June 4, 2026 at 6:23 am

    I’ve not checked that this explanation is true, but it has good points.

    Without any index column, a List is in fact a bag: no order, and duplicates allowed. So Hibernate considers it possible that you have the same role twice in the list of roles of a user.

    So issuing delete from user_role where user_id = ? and role_id = ? is not possible because it would potentially remove several roles instead of just the one you removed from the list.

    Try adding an index column, or using a Set<Role> instead of a List<Role>.

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

Sidebar

Related Questions

I tried a sample project about Hibernate Many-to-Many relationship downloaded from http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html Then I
This is question about java hibernate. my hibernate.cfg.xml is <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE hibernate-configuration
This is a basic question about Hibernate Caching , but I've to be sure
Does anybody have any idea about unions in hibernate? I got this question in
I know this question is posted many times, but I want to ask about
I have one question about optimistic locking in Hibernate. I am trying to go
I think this is a moderately neophyte question. I've been using NHibernate/FluentNHibernate for about
This question about Timers for windows services got me thinking: Say I have (and
Followed this question about delayed_job and monit Its working on my development machine. But
I have seen this question about deploying to WebSphere using the WAS ant tasks.

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.