gpt4 book ai didi

java - 我是否需要在 Spring 事务中显式保存修改后的域对象?

转载 作者:行者123 更新时间:2023-12-02 08:08:49 24 4
gpt4 key购买 nike

如果我有如下事务,其中我的域对象使用 Hibernate 映射为 Cascade.ALL:

@Transactional
public void transactionAllMethod(Domain domain) {
domain.addItemToCollection(new Item);
//Do I need to call domain.saveOrUpdate() here, or will changes to the domain be flushed
//automatically at the end of the transaction with FLUSHMODE.AUTO?

Domain domain2 = new Domain();
//set some fields here

//Do I need to save my second domain as it is new, or again will things be
//automatically persisted during dirtychecking at the end of the transaction?
}

如果我在这两种情况下都不需要显式保存,我应该在哪里进行保存?

最佳答案

您需要合并对现有实体的所有更改,然后保存新实体。

Session session = sessionFactory.getCurrentSession();
session.merge(domain);
session.save(domain2);

关于java - 我是否需要在 Spring 事务中显式保存修改后的域对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7765846/

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