You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JPA更新Oracle数据库未生效问题排查及解决记录

问题:Spring Data JPA更新Oracle记录后数据库无变更,但代码查询显示已更新

代码实现

Service层更新逻辑

@Transactional
public void updateLedgerAccountDetails(LedgerAccount ledgerAccount) {

    try {
        GrlMaeItemDet grlMaeItemDetUpdate = grlMaeItemDetRepository.findByPk(ledgerAccount.getGrlItemSistCcosto().getCodSistem(),
                ledgerAccount.getGrlItemSistCcosto().getGrlCodItem(),
                ledgerAccount.getGrlItemSistCcosto().getCodEmpres(),
                ledgerAccount.getGrlItemSistCcosto().getNumPeriodo());
        grlMaeItemDetUpdate.setCodImpsto(ledgerAccount.getGrlMaeItemDet().getCodImpsto());

    } catch (Exception e){
        log.error("An error has occured: ", e);
    }
}

Repository定义

@Repository
public class LedgerAccountRepository {

public interface GrlMaeItemDetRepository extends JpaRepository<GrlMaeItemDet, PkGrlMaeItemDet> {
    @Query(value = AppConfiguration.MAE_ITEM_DET_QUERY, nativeQuery = true)
    GrlMaeItemDet findByPk(@Param("codSistem") String codSistem,
                           @Param("grlCodItem") String grlCodItem,
                           @Param("codEmpres") String codEmpres,
                           @Param("numPeriodo") BigInteger numPeriodo);

  }

}

异常现象

  • 已确认目标数据库记录存在
  • Hibernate日志输出了更新SQL:
Hibernate: update grl_mae_item_det set vige=?,fec_transc=?,hra_transc=?, cod_impsto=? where cod_empres=? and cod_sistem=? and grl_cod_item=? and num_periodo=?
  • 用DBeaver查看数据库,对应记录未发生变更;但重新运行程序查询该实体时,终端显示已更新的数据(已验证为同一条记录)
  • 手动执行相同更新脚本可正常修改记录,且其他两张表采用完全相同的代码实现可正常完成更新

根因及解决方式

测试使用的特定记录本身存在不可更新的限制,但程序未抛出任何错误、异常或警告;更换其他测试记录后,更新功能恢复正常。

内容的提问来源于stack exchange,提问作者Ignacio Thomson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 13:22:50