JPA对数据库修改注意点

同一事务中获取不到修改数据

spring-boot-starter-parent版本2.7.12

Dao

java 复制代码
@Query(value = "select * from inventory_list where id in (?1) order by id desc",nativeQuery = true)
List<InventoryListEntity> getByIds(List<Integer> idList);

@Modifying
@Query(value = "update inventory_list set CLASSIFY_TYPE = ?2 where id in (?1)",nativeQuery = true)
void updateClassifyType(@Param(value = "ids") List<Integer> id,
						@Param(value = "classifyType") String classifyType);

Service

InventoryListService

java 复制代码
public interface InventoryListService {

    List<InventoryListEntity> getByIds(List<Integer> idList);

    void updateClassifyType(List<Integer> idList, String classifyType);

    boolean updateClassifyTypeToE(List<Integer> idList);

    boolean updateClassifyTypeToI(List<Integer> idList);
}

UserService

java 复制代码
public interface UserService {
	
	boolean updateClassifyType(List<Integer> idList);
}

ServiceImpl

InventoryListServiceImpl

java 复制代码
@Component
public class InventoryListServiceImpl implements InventoryListService {

    private final Log log = LogFactory.getLog(this.getClass());
    Gson gson = new Gson();

    @Autowired
    private InventoryListDao inventoryListDao;

    @Override
    public void updateClassifyType(List<Integer> idList, String classifyType) {
        inventoryListDao.updateClassifyType(idList, classifyType);
    }

    @Override
    public List<InventoryListEntity> getByIds(List<Integer> idList) {
        return inventoryListDao.getByIds(idList);
    }

    @Override
    public boolean updateClassifyTypeToE(List<Integer> idList) {

        List<InventoryListEntity> entityList = getByIds(idList);
        if (entityList.size() < idList.size()){
            log.warn("存在未查到数据");
            return false;
        }
        for (InventoryListEntity entity : entityList) {
            log.info("? to E,classifyType: " + gson.toJson(entity.getClassifyType()));
        }

        updateClassifyType(idList, "E");

        return true;
    }

    @Override
    public boolean updateClassifyTypeToI(List<Integer> idList) {
        List<InventoryListEntity> entityList = getByIds(idList);
        if (entityList.size() < idList.size()){
            log.warn("存在未查到数据");
            return false;
        }
        for (InventoryListEntity entity : entityList) {
            log.info("E to I,classifyType: " + gson.toJson(entity.getClassifyType()));
            if (!"E".equals(entity.getClassifyType())){

                return false;
            }
        }
        updateClassifyType(idList, "I");

        return true;
    }
}

UserServiceImpl

java 复制代码
@Component
public class UserServiceImpl implements UserService {
	private final Log log = LogFactory.getLog(this.getClass());
	@Autowired
    private InventoryListService inventoryListService;

	// 默认事务隔离级别
	@Override
    @Transactional
    public boolean updateClassifyType(List<Integer> idList) {

        boolean flag1 = inventoryListService.updateClassifyTypeToE(idList);
        if (!flag1){
            return false;
        }
        log.info("updateClassifyTypeToE success");

        boolean flag2 = inventoryListService.updateClassifyTypeToI(idList);
        if (!flag2){
            log.info("updateClassifyTypeToE false");
            return false;
        }

        return true;
    }

}

Controller

java 复制代码
@RestController
public class JpaEntityController {

    @Autowired
    private UserService userService;

    @PostMapping("/jpa/entity/test")
    public String jpaEntityTest(@RequestBody List<Integer> idList){

        boolean flag = userService.updateClassifyType(idList);

        if (flag){
            return "true";
        }

        return "false";
    }
}

请求结果日志


数据最终都会被改成之前第一次修改的状态

解决方式

将修改换成对entity实体的修改


相关推荐
qq_589568101 小时前
javaweb开发笔记—— 前端工程化
java·前端
码农小灰2 小时前
Kafka消息持久化机制全解析:存储原理与实战场景
java·分布式·kafka
程序员鱼皮3 小时前
太香了!我连夜给项目加上了这套 Java 监控系统
java·前端·程序员
L2ncE4 小时前
高并发场景数据与一致性的简单思考
java·后端·架构
武昌库里写JAVA4 小时前
使用 Java 开发 Android 应用:Kotlin 与 Java 的混合编程
java·vue.js·spring boot·sql·学习
小指纹4 小时前
河南萌新联赛2025第(六)场:郑州大学
java·开发语言·数据结构·c++·算法
叶~璃4 小时前
云计算:企业数字化转型的核心引擎
java
码luffyliu4 小时前
MySQL:MVCC机制及其在Java秋招中的高频考点
java·数据库·mysql·事务·并发·mvcc
程序员鱼皮4 小时前
这套 Java 监控系统太香了!我连夜给项目加上了
java·前端·ai·程序员·开发·软件开发
岁忧4 小时前
(nice!!!)(LeetCode 每日一题) 1277. 统计全为 1 的正方形子矩阵 (动态规划)
java·c++·算法·leetcode·矩阵·go·动态规划