gpt4 book ai didi

java - Spring JpaRepository 不更新表

转载 作者:行者123 更新时间:2023-12-02 13:37:37 26 4
gpt4 key购买 nike

我有下一个 JPARepo 代码:

public interface MyObjectRepository extends JpaRepository<MyObject, UUID> {
@Modifying
@Query("UPDATE MyObject obj SET obj.deleted = true WHERE obj.objectId = ?1")
void deleteById(UUID uuid);
}

及其调用的服务:

@Service
@Slf4j
@Transactional(readOnly = true)
public class MyService {
@Autowired
private MyObjectRepository myObjectRepository;

// some methods here

@Transactional
public void deleteMyObject(MyObject myObject) {
** first way **
myRepository.deleteById(myObject.getMyObjectId());
** second way **
myObject.setDeleted(true);
myObjectRepository.save(myObject);
}

//some methods here

}

两种更新方式只执行一次。下次调用方法时,删除列中的实体值默认为 false,更新后仍保持 false。但是,当在调试中时,我一步步查看表,我看到删除的列被设置为 true,当我释放调试时,删除标志再次设置为 false。是缓存还是注释不正确?

最佳答案

我认为类级别的@Transactional(readOnly = true)适用于所有方法,能否在方法中将其更改为@Transactional(readOnly = false)级别。

 @Transactional(readOnly = false)
public void deleteMyObject(MyObject myObject) {

关于java - Spring JpaRepository 不更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42903511/

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