有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java不可变对象和更新

如果我使用一个名为Name的不可变类来存储人的姓名,并且有人想要更改他们的姓名,那么他们的姓名不应该更新吗(基本上是删除旧条目并插入新条目)?这似乎与不可变的定义(通过注释)相矛盾,因为更新实体将被忽略。如果数据库中存储的不可变类需要更新,该怎么办?即使它是一个不可变的类,也不应该映射为@Immutable


共 (1) 个答案

  1. # 1 楼答案

    参考资料说:

    When an entity is read-only:

    • Hibernate does not dirty-check the entity's simple properties or single-ended associations;
    • Hibernate will not update simple properties or updatable single-ended associations;
    • Hibernate will not update the version of the read-only entity if only simple properties or single-ended updatable associations are changed;

    它还说:

    In some ways, Hibernate treats read-only entities the same as entities that are not read-only:

    • Hibernate cascades operations to associations as defined in the entity mapping.
    • Hibernate updates the version if the entity has a collection with changes that dirties the entity;
    • A read-only entity can be deleted.

    因此,我认为必须删除()实例,然后用新名称再次保存(),因为脏检查不会发生在只读实体上

    参考:Read-only entities