解決spring-data-jpa 事物中修改屬性自動(dòng)更新update問(wèn)題
問(wèn)題還原
項(xiàng)目orm層用的是spring-data-jpa,服務(wù)端接口實(shí)現(xiàn)的是樹(shù)節(jié)點(diǎn)間的拖拽功能,測(cè)試環(huán)境聯(lián)調(diào)發(fā)現(xiàn)異常問(wèn)題,其中拖拽到目標(biāo)目錄后節(jié)點(diǎn)名稱(chēng)總會(huì)重名,重名規(guī)則是originName轉(zhuǎn)化為originName(n)
@Transactional(rollbackFor = Exception.class) public void move(MoveWorkSpaceDto moveWorkSpaceDto) { /*** * 個(gè)人空間節(jié)點(diǎn)移動(dòng) * 1、源節(jié)點(diǎn)移動(dòng)到空目錄,那么目標(biāo)父節(jié)點(diǎn)必傳,前置和后置節(jié)點(diǎn)都為空 * 2、源節(jié)點(diǎn)移動(dòng)到非空目錄: * 2.1、移動(dòng)到目標(biāo)目錄的第一個(gè)節(jié)點(diǎn),那么后置節(jié)點(diǎn)必傳,前置節(jié)點(diǎn)為空 * 2.2、移動(dòng)到目標(biāo)目錄的最后個(gè)節(jié)點(diǎn),那么前置節(jié)點(diǎn)畢傳,后置節(jié)點(diǎn)為空 * 2.3、移動(dòng)到目標(biāo)目錄的中間節(jié)點(diǎn),那么前置節(jié)點(diǎn)和后置節(jié)點(diǎn)都要傳入 * 3、節(jié)點(diǎn)拖動(dòng)到目錄中,此時(shí)不知道目錄是否非空,前置后置節(jié)點(diǎn)都不傳,服務(wù)器追加到目錄的最后個(gè)節(jié)點(diǎn) */ log.info(">>>MoveParams: [{}]", JSONObject.toJSONString(moveWorkSpaceDto)); WorkSpaceEntity sourceEntity = workSpaceRepository.findById(moveWorkSpaceDto.getSourceWorkId()).orElse(null); if (sourceEntity == null) { throw new PublicsException("源工作空間節(jié)點(diǎn)不存在!"); } WorkSpaceEntity parentEntity = workSpaceRepository.findById(moveWorkSpaceDto.getDestParentId()).orElse(null); if (parentEntity == null) { throw new PublicsException("目標(biāo)父工作空間節(jié)點(diǎn)不存在!"); } 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); }
排查后發(fā)現(xiàn)代碼邏輯正常,于是加日志定位,果然在更新項(xiàng)目前多了update操作,具體位置如下:
說(shuō)明entity實(shí)體更新屬性后,jpa自動(dòng)執(zhí)行了update屬性,導(dǎo)致count判重始終大于0。
問(wèn)題原因
jpa在hibernate上更進(jìn)一步,我把單表的查詢(xún)邏輯定義在Repository層的方法上,不用謝SQL,簡(jiǎn)單明了,不曾想,忽略的hibernate的entity在session的3種狀態(tài)。
- 臨時(shí)態(tài),剛創(chuàng)建new的對(duì)象,還沒(méi)有持久化,session緩存中也沒(méi)有。
- 游離態(tài),已經(jīng)持久化,但不在session緩存中。
- 持久態(tài),已經(jīng)持久化,也在session緩存中。
問(wèn)題原因明確,sourceEntity在持久態(tài),修改屬性自然會(huì)更新到數(shù)據(jù)庫(kù),判重查詢(xún)已經(jīng)更新了,查的還是自己,所以總是誤認(rèn)為有重復(fù)節(jié)點(diǎn)。
解決辦法--隔離entity
避開(kāi)session中緩存的sourceEntity的修改,創(chuàng)建新entity,修改臨時(shí)態(tài)的entity,設(shè)置好屬性后再調(diào)用save更新數(shù)據(jù)
@Transactional(rollbackFor = Exception.class) public void move(MoveWorkSpaceDto moveWorkSpaceDto) { /*** * 個(gè)人空間節(jié)點(diǎn)移動(dòng) * 1、源節(jié)點(diǎn)移動(dòng)到空目錄,那么目標(biāo)父節(jié)點(diǎn)必傳,前置和后置節(jié)點(diǎn)都為空 * 2、源節(jié)點(diǎn)移動(dòng)到非空目錄: * 2.1、移動(dòng)到目標(biāo)目錄的第一個(gè)節(jié)點(diǎn),那么后置節(jié)點(diǎn)必傳,前置節(jié)點(diǎn)為空 * 2.2、移動(dòng)到目標(biāo)目錄的最后個(gè)節(jié)點(diǎn),那么前置節(jié)點(diǎn)畢傳,后置節(jié)點(diǎn)為空 * 2.3、移動(dòng)到目標(biāo)目錄的中間節(jié)點(diǎn),那么前置節(jié)點(diǎn)和后置節(jié)點(diǎn)都要傳入 * 3、節(jié)點(diǎn)拖動(dòng)到目錄中,此時(shí)不知道目錄是否非空,前置后置節(jié)點(diǎn)都不傳,服務(wù)器追加到目錄的最后個(gè)節(jié)點(diǎn) */ log.info(">>>MoveParams: [{}]", JSONObject.toJSONString(moveWorkSpaceDto)); WorkSpaceEntity sourceEntity = workSpaceRepository.findById(moveWorkSpaceDto.getSourceWorkId()).orElse(null); if (sourceEntity == null) { throw new PublicsException("源工作空間節(jié)點(diǎn)不存在!"); } WorkSpaceEntity parentEntity = workSpaceRepository.findById(moveWorkSpaceDto.getDestParentId()).orElse(null); if (parentEntity == null) { throw new PublicsException("目標(biāo)父工作空間節(jié)點(diǎn)不存在!"); } 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); }
場(chǎng)景復(fù)現(xiàn)
1、無(wú)事物的service中修改
public void test() { CronTaskEntity cronTaskEntity = cronTaskRepository.findById(18L).orElse(null); // 更新記錄 cronTaskEntity.setUsername("魯班七號(hào)"); CronTaskEntity newEntity = cronTaskRepository.findById(18L).orElse(null); Assert.isTrue(cronTaskEntity.getUsername().equals(newEntity.getUsername()), "查詢(xún)‘魯班七號(hào)'沒(méi)有更新啊"); }
運(yùn)行后:
Caused by: java.lang.IllegalArgumentException: 查詢(xún)‘魯班七號(hào)'沒(méi)有更新啊
2、有事物中的service修改
@Transactional public void test() { CronTaskEntity cronTaskEntity = cronTaskRepository.findById(18L).orElse(null); // 更新記錄 cronTaskEntity.setUsername("魯班七號(hào)"); CronTaskEntity newEntity = cronTaskRepository.findById(18L).orElse(null); Assert.isTrue(cronTaskEntity.getUsername().equals(newEntity.getUsername()), "查詢(xún)‘魯班七號(hào)'沒(méi)有更新啊"); }
運(yùn)行后:
Hibernate: update `cron_task` set `createtime`=?, `updatetime`=?, `is_deleted`=?, `cron_expression`=?, `remark`=?, `staff_id`=?, `tag_id`=?, `username`=? where `id`=?
通過(guò)
由此可見(jiàn),事物中會(huì)保存entity的緩存,修改entity屬性引起jpa自動(dòng)update,因此避免誤操作,建議創(chuàng)建臨時(shí)態(tài)的entity修改屬性。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 實(shí)現(xiàn)滑動(dòng)時(shí)間窗口限流算法的代碼
這篇文章主要介紹了Java 實(shí)現(xiàn)滑動(dòng)時(shí)間窗口限流算法的代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11全局記錄Feign的請(qǐng)求和響應(yīng)日志方式
這篇文章主要介紹了全局記錄Feign的請(qǐng)求和響應(yīng)日志方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06eclipse自動(dòng)提示和自動(dòng)補(bǔ)全功能實(shí)現(xiàn)方法
這篇文章主要介紹了eclipse自動(dòng)提示和自動(dòng)補(bǔ)全的相關(guān)內(nèi)容,文中向大家分享了二者的實(shí)現(xiàn)方法代碼,需要的朋友可以了解下。2017-09-09spring @Transactional 無(wú)效的解決方案
這篇文章主要介紹了spring @Transactional 無(wú)效的解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03java實(shí)現(xiàn)兩個(gè)對(duì)象之間傳值及簡(jiǎn)單的封裝
這篇文章主要介紹了java實(shí)現(xiàn)兩個(gè)對(duì)象之間傳值及簡(jiǎn)單的封裝,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11