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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:24:32+00:00 2026-05-19T09:24:32+00:00

We have three entities with bidirectional many-to-many mappings in a A <-> B <->

  • 0

We have three entities with bidirectional many-to-many mappings in a A <-> B <-> C “hierarchy” like so (simplified, of course):

@Entity
Class A {
  @Id int id;
  @JoinTable(
    name = "a_has_b",
    joinColumns = {@JoinColumn(name = "a_id", referencedColumnName = "id")},
    inverseJoinColumns = {@JoinColumn(name = "b_id", referencedColumnName = "id")})
  @ManyToMany
  Collection<B> bs;
}

@Entity
Class B {
  @Id int id;
  @JoinTable(
    name = "b_has_c",
    joinColumns = {@JoinColumn(name = "b_id", referencedColumnName = "id")},
    inverseJoinColumns = {@JoinColumn(name = "c_id", referencedColumnName = "id")})
  @ManyToMany(fetch=FetchType.EAGER,
    cascade=CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
  @org.hibernate.annotations.Fetch(FetchMode.SUBSELECT)
  private Collection<C> cs;
  @ManyToMany(mappedBy = "bs", fetch=FetchType.EAGER,
    cascade={CascadeType.MERGE,CascadeType.PERSIST,  CascadeType.REFRESH})
  @org.hibernate.annotations.Fetch(FetchMode.SUBSELECT)
  private Collection<A> as;
}

@Entity
Class C {
  @Id int id;
  @ManyToMany(mappedBy = "cs", fetch=FetchType.EAGER, 
    cascade={CascadeType.MERGE,CascadeType.PERSIST,  CascadeType.REFRESH})
  @org.hibernate.annotations.Fetch(FetchMode.SUBSELECT)
  private Collection<B> bs;
}

There’s no conecpt of an orphan – the entities are “standalone” from the application’s point of view – and most of the time we’re going to have a fistful of A:s, each with a couple of B:s (some may be “shared” among the A:s), and some 1000 C:s, not all of which are always “in use” by any B. We’ve concluded that we need bidirectional relations, since whenever an entity instance is removed, all links (entries in the join tables) have to be removed too. That is done like this:

void removeA( A a ) {
  if ( a.getBs != null ) {
    for ( B b : a.getBs() ) {  //<--------- ConcurrentModificationException here
      b.getAs().remove( a ) ;
      entityManager.merge( b );
    }
  }
  entityManager.remove( a );
}

If the collection, a.getBs() here, contains more than one element, then a ConcurrentModificationException is thrown. I’ve been banging my head for a while now, but can’t think of a reasonable way of removing the links without meddling with the collection, which makes underlying the Iterator angry.

Q1: How am I supposed to do this, given the current ORM setup? (If at all…)

Q2: Is there a more reasonable way do design the OR-mappings that will let JPA (provided by Hibernate in this case) take care of everything. It’d be just swell if we didn’t have to include those I'll be deleted now, so everybody I know, listen carefully: you don't need to know about this!-loops, which aren’t working anyway, as it stands…

  • 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-19T09:24:33+00:00Added an answer on May 19, 2026 at 9:24 am

    If the collection, a.getBs() here, contains more than one element, then a ConcurrentModificationException is thrown

    The issue is that the collections inside of A, B, and C are magical Hibernate collections so when you run the following statement:

    b.getAs().remove( a );
    

    this removes a from b’s collection but it also removes b from a’s list which happens to be the collection being iterated over in the for loop. That generates the ConcurrentModificationException.

    Matt’s solution should work if you are really removing all elements in the collection. If you aren’t however another work around is to copy all of the b’s into a collection which removes the magical Hibernate collection from the process.

    // copy out of the magic hibernate collection to a local collection
    List<B> copy = new ArrayList<>(a.getBs());
    for (B b : copy) {
       b.getAs().remove(a) ;
       entityManager.merge(b);
    }
    

    That should get you a little further down the road.

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

Sidebar

Related Questions

We have the following two entities with many-to-many association: @Entity public class Role {
I have three entities like this. Employee: id name EmployeeDepartment: id departmentID employeeID Department:
I have three entities: EntityA, EntityB and EntityC connected with to-many relationships. See schema
I have a case where i have three entities with one-to-many and one-to-many relationships:
I have UI automation tests. Tests involve three entities - Data object class -
I have three entities: Log, Employee, and Agency Log with a many to one
I have three entities, ChannelEntity -> MatchChannelEntity <- MatchEntity, the MatchChannelEntity saves the many
I have three related entities: a user has many device a user has a
I have three entities as shown below. Student { StudentID, Name, Age } Parent
I have three entities: User, Office and PhoneNumber. The user has many phone numbers,

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.