【发布时间】:2020-09-23 12:55:46
【问题描述】:
我有 2 个实体空间和类型。它们都相互关联。使用这些对象,当代码执行非常简单的操作时,我遇到了许多不需要的更新语句。
我在下面放了一个简单的场景。而且,在我的应用程序(Spring Boot API)中执行了一些更复杂的批处理操作。而且,这个问题导致所有链接实体都被更新,即使它们没有被修改。
我需要以某种方式摆脱这些不需要的更新,因为它们会导致某些操作出现严重的性能问题。
空间实体(部分显示):
@Entity
@Table(name = "spaces")
@Getter
@Setter
@NoArgsConstructor
public class SpaceDao {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
private byte[] uuid;
@ManyToOne
@JoinColumn(name = "type_id")
private TypeDao type;
}
类型实体(部分显示):
@Entity
@Table(name = "types")
@Getter
@Setter
@NoArgsConstructor
public class TypeDao {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="space_id")
private SpaceDao space;
}
Space repo 实现中的保存方法:
public Space saveSpace(Space space) {
SpaceDao spaceDao = SpaceMapper.toDao(space);
// Intentionally simplified this logic, to point out that
// I am only reading and saving the object, without any changes.
SpaceDao existingSpaceDao = relationalSpaceRepository.findById(spaceDao.getUuid()).get();
// Following line is where the magic happens
SpaceDao savedSpaceDao = relationalSpaceRepository.save(existingSpaceDao);
return SpaceMapper.toModelObject(savedSpaceDao, true);
}
空间碎片存储库:
public interface RelationalSpaceRepository extends CrudRepository<SpaceDao, byte[]> { }
代码命中 repository.save() 行时生成的休眠日志:
Hibernate: update spaces set description=?, location=?, name=?, parent_id=?, properties=?, status_id=?, status=?, subtype_id=?, subtype=?, type_id=?, type=? where uuid=?
Hibernate: update spaces set description=?, location=?, name=?, parent_id=?, properties=?, status_id=?, status=?, subtype_id=?, subtype=?, type_id=?, type=? where uuid=?
Hibernate: update types set category=?, definition=?, description=?, disabled=?, logical_order=?, name=?, space_id=? where id=?
Hibernate: update types set category=?, definition=?, description=?, disabled=?, logical_order=?, name=?, space_id=? where id=?
Hibernate: update spaces set description=?, location=?, name=?, parent_id=?, properties=?, status_id=?, status=?, subtype_id=?, subtype=?, type_id=?, type=? where uuid=?
Hibernate: update spaces set description=?, location=?, name=?, parent_id=?, properties=?, status_id=?, status=?, subtype_id=?, subtype=?, type_id=?, type=? where uuid=?
Hibernate: update types set category=?, definition=?, description=?, disabled=?, logical_order=?, name=?, space_id=? where id=?
Hibernate: update spaces set description=?, location=?, name=?, parent_id=?, properties=?, status_id=?, status=?, subtype_id=?, subtype=?, type_id=?, type=? where uuid=?
Hibernate: update types set category=?, definition=?, description=?, disabled=?, logical_order=?, name=?, space_id=? where id=?
Hibernate: update spaces set description=?, location=?, name=?, parent_id=?, properties=?, status_id=?, status=?, subtype_id=?, subtype=?, type_id=?, type=? where uuid=?
【问题讨论】:
-
需要更多信息,例如这些检测到的差异来自何处。我怀疑使用 find 读取的 SpaceDao 在您调用 find 到调用 save 之间变得陈旧 - 如果您使用类似合并的操作,则会出现问题。您需要确保保留最初在 SpaceDao 中读取的上下文以用于保存操作,因为它可用于跟踪您所做的实际差异,而不必检测它与当前内容之间的差异调用 save 时的数据库。
-
如果我没记错的话,JPA 是基于托管实体的。因此,每当您在会话中对托管实体应用一些更改时,它们将在您刷新/关闭会话时保持不变。您确定更新不是来自会话管理吗?编辑:但您似乎没有应用任何更改...
-
SpaceDao 和 TypeDao 有多对一的关系,而 type 和 SpaceDao 也有多对一的关系,这种关系是正确的还是多对多的关系?
-
两次多对一关系非常可疑。此外,仅在一侧(ManyToOne 一侧)使用 @JoinColumn。
-
@Chris,嗯,就像repo实现方法中展示的那样简单。这不合逻辑,但我简化了它以首先测试这种情况下的可能解决方案。如所见,在读取和写入之间没有修改或任何其他操作。此外,Spring Data 的 CrudRepository 默认使用 findById() 和 save() 方法。