Python 官方文档:入门教程 => 点击学习
问题还原 项目ORM层用的是spring-data-jpa,服务端接口实现的是树节点间的拖拽功能,测试环境联调发现异常问题,其中拖拽到目标目录后节点名称总会重名,重名规则是origi
项目ORM层用的是spring-data-jpa,服务端接口实现的是树节点间的拖拽功能,测试环境联调发现异常问题,其中拖拽到目标目录后节点名称总会重名,重名规则是originName转化为originName(n)
@Transactional(rollbackFor = Exception.class)
public void move(MoveWorkSpaceDto moveWorkSpaceDto) {
log.info(">>>MoveParams: [{}]", JSONObject.tojsONString(moveWorkSpaceDto));
WorkSpaceEntity sourceEntity = workSpaceRepository.findById(moveWorkSpaceDto.getSourceWorkId()).orElse(null);
if (sourceEntity == null) {
throw new PublicsException("源工作空间节点不存在!");
}
WorkSpaceEntity parentEntity = workSpaceRepository.findById(moveWorkSpaceDto.getDestParentId()).orElse(null);
if (parentEntity == null) {
throw new PublicsException("目标父工作空间节点不存在!");
}
log.info(">>>>Noooooooo<<<<<");
sourceEntity.setParentId(parentEntity.getId());
log.info(">>>>UpdateNow<<<<<");
Long cnt = workSpaceRepository.countAllByParentIdAndSpaceName(parentEntity.getId(), sourceEntity.getSpaceName());
log.info("destParentId: [{}], spaceName: [{}], destCount: [{}]", parentEntity.getId(), sourceEntity.getSpaceName(), cnt);
if (cnt > 0L) {
String newName = RenameUtil.rename(sourceEntity.getSpaceName(), sourceEntity.getRunType(), cnt);
sourceEntity.setSpaceName(newName);
}
if (moveWorkSpaceDto.getFrontWorkId() == null && moveWorkSpaceDto.getPostWorkId() == null) {
List<WorkSpaceEntity> children = workSpaceRepository.findByParentIdOrderBySortIndexDesc(
moveWorkSpaceDto.getDestParentId());
if (CollectionUtils.isEmpty(children)) {
sourceEntity.setSortIndex(sourceEntity.getId() * SORT_INDEX_STEP);
} else {
sourceEntity.setSortIndex(children.get(0).getSortIndex() + SORT_INDEX_STEP);
}
} else if (moveWorkSpaceDto.getFrontWorkId() != null && moveWorkSpaceDto.getPostWorkId() == null) {
WorkSpaceEntity frontEntity = getFrontEntity(moveWorkSpaceDto);
sourceEntity.setSortIndex(frontEntity.getSortIndex() + SORT_INDEX_STEP);
} else if (moveWorkSpaceDto.getFrontWorkId() == null && moveWorkSpaceDto.getPostWorkId() != null) {
WorkSpaceEntity postEntity = getPostEntity(moveWorkSpaceDto);
sourceEntity.setSortIndex(postEntity.getSortIndex() - SORT_INDEX_STEP);
} else {
WorkSpaceEntity frontEntity = getFrontEntity(moveWorkSpaceDto);
WorkSpaceEntity postEntity = getPostEntity(moveWorkSpaceDto);
sourceEntity.setSortIndex((frontEntity.getSortIndex() + postEntity.getSortIndex()) / 2);
}
workSpaceRepository.save(sourceEntity);
}
排查后发现代码逻辑正常,于是加日志定位,果然在更新项目前多了update操作,具体位置如下:
说明entity实体更新属性后,jpa自动执行了update属性,导致count判重始终大于0。
jpa在hibernate上更进一步,我把单表的查询逻辑定义在Repository层的方法上,不用谢sql,简单明了,不曾想,忽略的hibernate的entity在session的3种状态。
问题原因明确,sourceEntity在持久态,修改属性自然会更新到数据库,判重查询已经更新了,查的还是自己,所以总是误认为有重复节点。
避开session中缓存的sourceEntity的修改,创建新entity,修改临时态的entity,设置好属性后再调用save更新数据
@Transactional(rollbackFor = Exception.class)
public void move(MoveWorkSpaceDto moveWorkSpaceDto) {
log.info(">>>MoveParams: [{}]", JSONObject.toJSONString(moveWorkSpaceDto));
WorkSpaceEntity sourceEntity = workSpaceRepository.findById(moveWorkSpaceDto.getSourceWorkId()).orElse(null);
if (sourceEntity == null) {
throw new PublicsException("源工作空间节点不存在!");
}
WorkSpaceEntity parentEntity = workSpaceRepository.findById(moveWorkSpaceDto.getDestParentId()).orElse(null);
if (parentEntity == null) {
throw new PublicsException("目标父工作空间节点不存在!");
}
WorkSpaceEntity updateEntity = new WorkSpaceEntity();
BeanUtils.copyProperties(sourceEntity, updateEntity);
Long cnt = workSpaceRepository.countByParentIdAndSpaceName(parentEntity.getId(), updateEntity.getSpaceName());
log.info("destParentId: [{}], spaceName: [{}], destCount: [{}]", parentEntity.getId(), updateEntity.getSpaceName(), cnt);
if (cnt > 0L) {
String newName = RenameUtil.rename(updateEntity.getSpaceName(), updateEntity.getRunType(), cnt);
updateEntity.setSpaceName(newName);
}
if (moveWorkSpaceDto.getFrontWorkId() == null && moveWorkSpaceDto.getPostWorkId() == null) {
List<WorkSpaceEntity> children = workSpaceRepository.findByParentIdOrderBySortIndexDesc(
moveWorkSpaceDto.getDestParentId());
if (CollectionUtils.isEmpty(children)) {
updateEntity.setSortIndex(updateEntity.getId() * SORT_INDEX_STEP);
} else {
updateEntity.setSortIndex(children.get(0).getSortIndex() + SORT_INDEX_STEP);
}
} else if (moveWorkSpaceDto.getFrontWorkId() != null && moveWorkSpaceDto.getPostWorkId() == null) {
WorkSpaceEntity frontEntity = getFrontEntity(moveWorkSpaceDto);
updateEntity.setSortIndex(frontEntity.getSortIndex() + SORT_INDEX_STEP);
} else if (moveWorkSpaceDto.getFrontWorkId() == null && moveWorkSpaceDto.getPostWorkId() != null) {
WorkSpaceEntity postEntity = getPostEntity(moveWorkSpaceDto);
updateEntity.setSortIndex(postEntity.getSortIndex() - SORT_INDEX_STEP);
} else {
WorkSpaceEntity frontEntity = getFrontEntity(moveWorkSpaceDto);
WorkSpaceEntity postEntity = getPostEntity(moveWorkSpaceDto);
updateEntity.setSortIndex((frontEntity.getSortIndex() + postEntity.getSortIndex()) / 2);
}
updateEntity.setParentId(parentEntity.getId());
workSpaceRepository.save(updateEntity);
}
1、无事物的service中修改
public void test() {
CronTaskEntity cronTaskEntity = cronTaskRepository.findById(18L).orElse(null);
// 更新记录
cronTaskEntity.setUsername("鲁班七号");
CronTaskEntity newEntity = cronTaskRepository.findById(18L).orElse(null);
Assert.isTrue(cronTaskEntity.getUsername().equals(newEntity.getUsername()), "查询‘鲁班七号'没有更新啊");
}
运行后:
Caused by: java.lang.IllegalArgumentException: 查询‘鲁班七号'没有更新啊
2、有事物中的service修改
@Transactional
public void test() {
CronTaskEntity cronTaskEntity = cronTaskRepository.findById(18L).orElse(null);
// 更新记录
cronTaskEntity.setUsername("鲁班七号");
CronTaskEntity newEntity = cronTaskRepository.findById(18L).orElse(null);
Assert.isTrue(cronTaskEntity.getUsername().equals(newEntity.getUsername()), "查询‘鲁班七号'没有更新啊");
}
运行后:
Hibernate: update `cron_task` set `createtime`=?, `updatetime`=?, `is_deleted`=?, `cron_expression`=?, `remark`=?, `staff_id`=?, `tag_id`=?, `username`=? where `id`=?
通过
由此可见,事物中会保存entity的缓存,修改entity属性引起jpa自动update,因此避免误操作,建议创建临时态的entity修改属性。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: 解决spring-data-jpa 事物中修改属性自动更新update问题
本文链接: https://lsjlt.com/news/131976.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0