![](/img/trans.png)
[英]How to fetch data from multiple tables in spring boot using mapping in Spring Boot's JPA repository
[英]How to save data immediately to the database using Spring Boot and JPA Repository
我正在嘗試使用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列表中包含元素並訪問它們的索引?
您的列表不會自動更新。 您必須手動添加新元素
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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.