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实体的修改


相关推荐
衍生星球8 分钟前
JSP 程序设计之 JSP 基础知识
java·web·jsp
m0_749317528 分钟前
力扣-字母异位词
java·算法·leetcode·职场和发展
黑暗也有阳光19 分钟前
java中为什么hashmap的大小必须是2倍数
java·后端
fatsheep洋23 分钟前
XSS-DOM-1
java·前端·xss
七七软件开发1 小时前
一对一交友小程序 / APP 系统架构分析
java·python·小程序·系统架构·php
TDengine (老段)1 小时前
TDengine 中 TDgpt 异常检测的数据密度算法
java·大数据·算法·时序数据库·iot·tdengine·涛思数据
YuTaoShao1 小时前
【LeetCode 热题 100】155. 最小栈
java·算法·leetcode
程序视点1 小时前
Java语言核心特性全解析:从面向对象到跨平台原理
java·后端·java ee
Warren981 小时前
MySQL查询语句详解
java·开发语言·数据库·mysql·算法·蓝桥杯·maven
丶小鱼丶2 小时前
Spring之【循环引用】
java·spring