不可变对象和更新

Immutable Objects & Updates

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

参考说:

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.

所以,我认为您必须 delete() 实例,而不是自己用新名称再次 save() 它,因为脏检查不会发生在只读实体上。

参考:Read-only entities