我正在嘗試使用Save from JPA Repository將一些資料保存到資料庫中。我在另一種方法中呼叫了 save 方法。問題是在呼叫 .save() 后沒有保存創建的物體。我必須從保存的資料中訪問索引,但由于未保存提到的資料,我得到“索引 -1 超出長度 0 的范圍”。在此之后,我認為它會回滾,因為執行仍在繼續。代碼如下:
public void method(Double number, Integer entityId){
Entity entity= findById(entityId);
CalculatedValue calculatedValue= new CalculatedValue ();
LocalDateTime dateTime = LocalDateTime.now();
monitoredValue.setDateTime(dateTime);
List<CalculatedValue > calculatedValueList= calculatedValueservice.findAllById(entityId);
if(calculatedValueList.isEmpty()){
calculatedValue.setValue(0.0);
calculatedValue.setEntity(entity);
calculatedValueRespository.save(calculatedValue);
}
CaclulatedValue lastCalculatedValue = calculatedValueList.get(calculatedValueList.size() - 1);
}
我的問題是:如何將calculatedValue保存到資料庫中,以便我可以在lastCalculatedValue串列中包含元素并訪問它們的索引?
uj5u.com熱心網友回復:
您的串列不會自動更新。您必須手動添加新元素
List<CalculatedValue > calculatedValueList= calculatedValueservice.findAllById(entityId);
if(calculatedValueList.isEmpty()){
calculatedValue.setValue(0.0);
calculatedValue.setEntity(entity);
calculatedValueRespository.save(calculatedValue);
// ADD IT MANUALLY
calculatedValueList.add(calculatedValue)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/345741.html
