Spring Boot JPA 不会使用保存方法更新记录
Sprint Boot JPA won't update record with save method
我对 Spring Boot 和 JPA 比较陌生,正在尝试更新 mysql 数据库中的记录。在我下面的控制器中,我能够成功地将新记录保存到数据库中并查找现有记录。但是,当我尝试更新现有记录时,没有任何反应。 (例如,它不保存并跳过该方法。)
下面是我的控制器。请注意,errorUpdateService.fineErrorByName 可以成功检索数据库中的现有记录。
@Autowired
private ErrorUpdateService errorUpdateService;
@RequestMapping(value="/api/UpdateError", method=RequestMethod.POST)
@ResponseBody
public String updateError(@RequestBody Error error){
try{
Error retrievedError=errorUpdateService.findErrorByName((error.getName()));
if(!retrievedError.getName().equals("NVE")){
System.out.println("Here is error: "+retrievedError.toString());
errorUpdateService.updateError(retrievedError);
return retrievedError.getName() + " has been updated";
}
}catch(NullPointerException e){
errorUpdateService.updateError(error);
return "Error Name Does not Exist. Saving " +error.getName() ;
}
return "Failed Method";
}
当代码执行到 errorUpdateService.updateError(retrievedError);
时,它不会保存到数据库并且似乎跳过了。但是,如果您发送数据库中不存在的记录,则不会出现任何问题(请参阅 Null Pointer catch 块)。
参见下面的 ErrorUpdateServiceImpl
'
@Autowired
private ErrorRepository errorRepository;
@Override
public void updateError(Error error) {
System.out.println("Attempting to save a error: "+error.toString());
errorRepository.save(error);
}
@Override
public Error findErrorByName(String errName){
try{
Error error=errorRepository.findByErrName(errName);
return error;
}catch(NullPointerException e){
return new Error("NVE");
}
}
'
public interface ErrorRepository extends JpaRepository<Error, Long>{
Error findByErrName(String errName);
}
以下是尝试更新记录时的控制台输出示例:
Hibernate: select error0_.id as id1_0_, error0_.documentationurl as document2_0_, error0_.err_name as err_name3_0_, error0_.error_id as error_id4_0_, error0_.has_documentation as has_docu5_0_, error0_.last_incident_time as last_inc6_0_, error0_.throttle_minutes as throttle7_0_, error0_.throttle_threshold as throttle8_0_ from error error0_ where error0_.err_name=?
Here is error: Error [id=6, errorId=0, hasDocumentation=false, errName=Error0001, documentationURL=null, throttleThreshold=0, throttleMinutes=0, lastIncidentTime=null]
Attempting to save a error: Error [id=6, errorId=0, hasDocumentation=false, errName=Error0001, documentationURL=null, throttleThreshold=0, throttleMinutes=0, lastIncidentTime=null]
我是不是遗漏了什么,或者您正在尝试使用您检索到的相同记录来更新记录?您正在使用完全相同的值更新它,所以也许这就是您看不到任何更改的原因?
我对 Spring Boot 和 JPA 比较陌生,正在尝试更新 mysql 数据库中的记录。在我下面的控制器中,我能够成功地将新记录保存到数据库中并查找现有记录。但是,当我尝试更新现有记录时,没有任何反应。 (例如,它不保存并跳过该方法。)
下面是我的控制器。请注意,errorUpdateService.fineErrorByName 可以成功检索数据库中的现有记录。
@Autowired
private ErrorUpdateService errorUpdateService;
@RequestMapping(value="/api/UpdateError", method=RequestMethod.POST)
@ResponseBody
public String updateError(@RequestBody Error error){
try{
Error retrievedError=errorUpdateService.findErrorByName((error.getName()));
if(!retrievedError.getName().equals("NVE")){
System.out.println("Here is error: "+retrievedError.toString());
errorUpdateService.updateError(retrievedError);
return retrievedError.getName() + " has been updated";
}
}catch(NullPointerException e){
errorUpdateService.updateError(error);
return "Error Name Does not Exist. Saving " +error.getName() ;
}
return "Failed Method";
}
当代码执行到 errorUpdateService.updateError(retrievedError);
时,它不会保存到数据库并且似乎跳过了。但是,如果您发送数据库中不存在的记录,则不会出现任何问题(请参阅 Null Pointer catch 块)。
参见下面的 ErrorUpdateServiceImpl '
@Autowired
private ErrorRepository errorRepository;
@Override
public void updateError(Error error) {
System.out.println("Attempting to save a error: "+error.toString());
errorRepository.save(error);
}
@Override
public Error findErrorByName(String errName){
try{
Error error=errorRepository.findByErrName(errName);
return error;
}catch(NullPointerException e){
return new Error("NVE");
}
}
'
public interface ErrorRepository extends JpaRepository<Error, Long>{
Error findByErrName(String errName);
}
以下是尝试更新记录时的控制台输出示例:
Hibernate: select error0_.id as id1_0_, error0_.documentationurl as document2_0_, error0_.err_name as err_name3_0_, error0_.error_id as error_id4_0_, error0_.has_documentation as has_docu5_0_, error0_.last_incident_time as last_inc6_0_, error0_.throttle_minutes as throttle7_0_, error0_.throttle_threshold as throttle8_0_ from error error0_ where error0_.err_name=?
Here is error: Error [id=6, errorId=0, hasDocumentation=false, errName=Error0001, documentationURL=null, throttleThreshold=0, throttleMinutes=0, lastIncidentTime=null]
Attempting to save a error: Error [id=6, errorId=0, hasDocumentation=false, errName=Error0001, documentationURL=null, throttleThreshold=0, throttleMinutes=0, lastIncidentTime=null]
我是不是遗漏了什么,或者您正在尝试使用您检索到的相同记录来更新记录?您正在使用完全相同的值更新它,所以也许这就是您看不到任何更改的原因?