UnsupportedOperationException merge-saving many-to-many relation with hibernate and JPA

Solution 1:

It is because of your

Arrays.asList(roleProvider.findAll().get(0))

This creates an unmodifiable list (in fact, a non-resizable list). Hibernate seems to expect a modifiable list. Try using this instead:

public void testAccountRole(){      
    Account returnedAccount = accountProvider.findAll().get(0);

    List<Role> list = new ArrayList<Role>();
    list.add(roleProvider.findAll().get(0));    
    returnedAccount.setRoles(list);  

    accountProvider.save(returnedAccount);
}

This solution won't explain why exactly you got the other exception (might be documented in the Hibernate docs), but it might be a valid workaround.

Solution 2:

Hibernate's persistent variant of the Collection in question attempts to delegate to an abstract base class (PersistenceBag) that doesn't implement the add method.