我正在制作一个休息api,其中我必须以id为基础更新并通过spring数据在表中插入没有id base的数据。但是我发现它运行的两个条件中的错误插入查询只更新不起作用
存储库文件 @Repository 公共接口CustomerRepository扩展了JpaRepository { CustomerEntity save(CustomerEntity customerEntity); CustomerEntity findById(int id);
访问管理器文件
public CustomerDO create(CustomerDO customerReqDO){CustomerEntity customerEntity = null; CustomerDO customerResDO = new CustomerDO(); if(null!= customerReqDO.getId()){ customerEntity = customerRepository.findById(Integer.parseInt(customerReqDO.getId())); if(null!= customerEntity){
if(null != customerReqDO.getName())
customerEntity.setName(customerReqDO.getName());
if(null != customerReqDO.getPhone())
customerEntity.setPhone(customerReqDO.getPhone());
if(null != customerReqDO.getEmail())
customerEntity.setEmail(customerReqDO.getEmail());
if(null != customerReqDO.getPassword())
customerEntity.setPassword(customerReqDO.getPassword());
//if(null !=customerReqDO.getCredits())
//customerReqEntity.setCredits(customerReqDO.getCredits());
customerEntity =customerRepository.save(customerEntity);
}else{
customerResDO.setError("Invalid Customer id "+customerReqDO.getId());
}}else{
customerEntity=new CustomerEntity();
customerEntity.setName(customerReqDO.getName());
customerEntity.setPhone(customerReqDO.getPhone());
customerEntity.setEmail(customerReqDO.getEmail());
//customerReqEntity.setFacebookID(customerReqDO.getFacebookID());
customerEntity.setPassword(customerReqDO.getPassword());
//customerReqEntity.setCredits(customerReqDO.getCredits());
customerEntity =customerRepository.save(customerEntity);
}if(null != customerEntity){
customerResDO.setId(String.valueOf(customerEntity.getId()));
customerResDO.setName(customerEntity.getName());
customerResDO.setPhone(customerEntity.getPhone());
customerResDO.setEmail(customerEntity.getEmail());
customerResDO.setPassword(customerEntity.getPassword());
//deliveryLocationResponseDO.setDeliverytimes(Arrays.asList(deliveryLocationEntity.getDeliverytimes()));
}
return customerResDO;
}
controller
@CrossOrigin(起源=" *") @PostMapping() public CustomerResponseDTO register(@RequestBody CustomerUpdateRequestDTO registerRequestDTO){ CustomerDO customerReqDO = new CustomerDO(); CustomerResponseDTO loginResponseDTO = new CustomerResponseDTO(); customerReqDO.setName(registerRequestDTO.getName()); customerReqDO.setEmail(registerRequestDTO.getEmail()); customerReqDO.setPhone(registerRequestDTO.getPhone()); customerReqDO.setPassword(registerRequestDTO.getPassword());
CustomerDO customerResDO = customerLoginManagerImpl.insert(customerReqDO);
if(null != customerResDO.getError()){
loginResponseDTO.setError(customerResDO.getError());
}else{
loginResponseDTO.setSucess(true);
}
return loginResponseDTO;
}
答案 0 :(得分:0)
使用Hibernate的Criteria:
Bad Request: The user already exists.