0
点赞
收藏
分享

微信扫一扫

Spring Data Jpa出现莫名其妙丢失数据现象

归零者245号 2022-02-17 阅读 95

一、Spring Data Jpa

因为我们使用共通类BeanUtils,BeanUtils默认把ID主键值设置数值,通过Jpa插入数据库时如果主键在数据库中存在Jpa会默认做update操作。所以导致数据丢失。

import org.apache.commons.beanutils.BeanUtils;

BeanUtils.copyProperties(aftBean,befBean);

pom依赖

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.2</version>
        </dependency>

解决方案:

重新对id主键设置默认值0/null,数据库中不存在的主键值。

使用Jpa  savaAndFlush()方法更新。

举报

相关推荐

0 条评论