gpt4 book ai didi

java - 不可变对象(immutable对象)和更新

转载 作者:行者123 更新时间:2023-12-01 11:29:26 24 4
gpt4 key购买 nike

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

最佳答案

引用文献说:

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

关于java - 不可变对象(immutable对象)和更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30536152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com