无法使用 Spring Data JPA/Hibernate 在事务中两次保存具有不可变集合的实体

问题描述

我的 Spring Boot + Spring Data JPA 应用程序中有以下服务层函数

@Service
public class MyService {

    @Autowired
    MyEntityRepository repository;

    @Transactional
    public void serviceMethod() {
        MyEntity newEntity = new MyEntity("code","name","description");
        newEntity = repository.save(newEntity); // --> all good here

        // ...

        newEntity.setName("updated name");
        newEntity = repository.save(newEntity); // --> all good here

        Set<ChildEntity> newChildrenEntities = Set.of(new ChildEntity("childName"));
        newEntity.setChildren(newChildrenEntities);
        newEntity = repository.save(newEntity); // --> Exception here!
        
        // ...
    }
}

当执行到达最后一个 save 方法时,会引发异常:

java.lang.UnsupportedOperationException: null
    at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:73) ~[na:na]
    at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.clear(ImmutableCollections.java:79) ~[na:na]
    at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:581) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
    at org.hibernate.type.CollectionType.replace(CollectionType.java:757) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
    at org.hibernate.type.TypeHelper.replace(TypeHelper.java:167) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
    at org.hibernate.event.internal.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:451) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
...

如果我切换操作顺序,那么第二种方法也会引发异常:

@Transactional
public void serviceMethod() {
    MyEntity newEntity = new MyEntity("code","description");

    Set<ChildEntity> newChildrenEntities = Set.of(new ChildEntity("childName"));
    newEntity.setChildren(newChildrenEntities);
    newEntity = repository.save(newEntity); // --> all good here

    newEntity.setName("updated name");
    newEntity = repository.save(newEntity); // --> Exception here!
}

但是,如果我将 newChildrenEntities 实现更改为可修改的集合:

@Transactional
public void serviceMethod() {
    MyEntity newEntity = new MyEntity("code","description");
    newEntity = repository.save(newEntity); // --> all good here

    // ...

    newEntity.setName("updated name");
    newEntity = repository.save(newEntity); // --> all good here

    Set<ChildEntity> newChildrenEntities = new HashSet<>();
    newChildrenEntities.add(new ChildEntity("childName"));
    newEntity.setChildren(newChildrenEntities);
    newEntity = repository.save(newEntity); // --> all good here!
        
    // ...
}

或者如果我从我的方法删除 @Transactional 注释,那么一切正常。

我想知道为什么会发生这种情况,这似乎与 Hibernate 处理集合的方式有关。

解决方法

由于 Hibernate 在管理刷新/刷新/加载时需要控制/更改实体的状态,因此您在自动刷新期间会看到此异常。我建议您不要尝试将不可变集合用于实体映射。您可以改为确保仅向 API 用户公开不可变的版本,但 Hibernate 可能需要更改内容。如果您认为这是一个错误或可以改进以支持您的用例,您还可以使用重现问题的测试用例 (https://hibernate.atlassian.net) 在问题跟踪器 (https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) 中创建问题。