title: UnsupportedOperationException when updating cascading children in Spring+JPA
body: <p>I'm using OneToMany mapping in my SpringBoot project, while I'm having problems when updating the children along with parent update, sample code is like below:</p>
  
  <p><strong>User.java</strong></p>
  
  <pre><code>@Table(name = "user")
  @Entity
  public class User {
      @Id
      @GeneratedValue
      private Integer id;
  
      @OneToMany(mappedBy = "groupUser", cascade = {CascadeType.ALL}, orphanRemoval = true)
      private List&lt;UserGroup&gt; userGroups = new ArrayList&lt;&gt;();
  }
  </code></pre>
  
  <p><strong>UserGroup.java</strong></p>
  
  <pre><code>@Table(name = "user_group")
  @Entity
  public class UserGroup {
      @Id
      @GeneratedValue
      private Integer id;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name="user_id")
      private User groupUser; 
  }
  </code></pre>
  
  <p><strong>SampleUsageCode.java</strong></p>
  
  <pre><code>    @Service
      public class UserService {
          @Autowired
          private UserRepository userRepositry;
  
          @Transactaional
          public batchUpdateUsers(Collection&lt;User&gt; toBeSavedUsers) {
              Map&lt;Integer, User&gt; toBeSavedIdUserMap = toBeSavedUsers.stream()
  .collect(groupBy(User::getId(), toList()));
              Collection&lt;User&gt; existingUsers = userRepositry.findByIdIn(toBeSavedIdUserMap.entrySet().stream()
  .map(Map.Entry::getKey).collect(toList()));
              existingUsers.forEach(user -&gt; user.getUserGroups().add(toBeSavedIdUserMap.get(user.getId()).getUserGroups()));
          }
      }
  </code></pre>
  
  <p>To simplify the problem, Let's just assume the user groups in to-be-saved users is totally different with the existing ones in the database. The problem is when I try to add new user groups to existing users, it throws <code>java.lang.UnsupportedOperationException</code>. It seems the <code>persistentBag</code> type of <code>userGroups</code> in <code>User</code> is not editable. 
  I tried with just creating a new collection to store both existing and new user groups, but another error with <code>A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance</code> occurs when I try to save the updated users. How can I achieve this cascading-children merge requirement?</p>