JPQL:一次不会删除多个记录

时间:2018-10-30 08:12:22

标签: spring-data-jpa jpql

我正在使用spring data jpa。我正在尝试实施保存按钮服务。首先,在保存按钮上,我要delete现有记录,然后需要保存新记录。

首先,我只想删除此处的记录。从前端我没有得到RoomInvestigatorMapping表的主键。我正在获取具有重复值的nRoomAllocation ID和调查员ID。所以我在JPQL条件下使用AND查询

在下面的代码中,如果我一次发送一条记录,则将其正确删除,但是当我发送两条记录时,仅删除第二条记录。第一条记录没有删除

RoomInvestigatorMappingService类

public String updatePiDetails(List<PiDetails> roomInvestMapping) {  

        List<RoomInvestigatorMapping> currentRecord = new ArrayList<RoomInvestigatorMapping>();

        for(PiDetails inputRecorObj:roomInvestMapping) {            

            currentRecord =roomInvestigatorMappingRepo.findByNRoomAllocationId(
                    inputRecorObj.getnRoomAllocationId(),inputRecorObj.getnInvestigatorId());       
            }

        for(RoomInvestigatorMapping currentRecordObj:currentRecord) {

            roomInvestigatorMappingRepo.deleteByNRoomAllocationIdAndNInvestigatorId(
                    currentRecordObj.getnRoomAllocationId(), currentRecordObj.getnInvestigatorId());    
        }       

        return"sucessfully deleted";
    }
}

RoomInvestigatorMappingRepository接口

@Query("select roomInvestMapping from RoomInvestigatorMapping as roomInvestMapping "
            + "where nRoomAllocationId=?1 and nInvestigatorId=?2 ")
    List<RoomInvestigatorMapping> findByNRoomAllocationId(Integer nRoomAllocationId, Integer nInvestigatorId);

    @Modifying
    @Transactional
    @Query("Delete from RoomInvestigatorMapping as roomInvestMapping "
            + "where nRoomAllocationId=?1 and nInvestigatorId=?2")
    void deleteByNRoomAllocationIdAndNInvestigatorId(Integer nRoomAllocationId ,Integer nInvestigatorId);

输入

[
  {
    "nInvestigatorId": 910572,
    "nPercentageAssigned": 50,
    "nRoomAllocationId": 317
  },
 {
    "nInvestigatorId": 912713,
    "nPercentageAssigned": 50,
    "nRoomAllocationId": 317
  }
]

0 个答案:

没有答案