I am using spring data JPA I am trying to update the entity using the JPARepository method save() to update the entity details. Code I have written to update the entity works fine in development as expected. But the same code does not work on the production server it does not give any error only the code written inside the map() does not work. Below is my code
public Long updateQrCodeUrlByBusinessDetails(Long businessId, String menuUrl, MENU_TYPE menuType) {
return qrCodeRepo.findByBusinessId(businessId).businessQrCode.stream().map(qr -> {
qr.setMenuUrl(menuUrl);
qr.setMenuType(menuType);
return qrCodeRepo.save(qr);
}).count();
}
just my two cents.
You should always return Optional from query results. You may have it as:
Optional<List<BusinessQRCode>> findByBusinessId(Long businessId);
public void updateQrCodeUrlByBusinessDetails(Long businessId, String menuUrl, MENU_TYPE menuType) {
// Fetch iterate, if exist
qrCodeRepo.findByBusinessId(businessId).ifPresent(qrCodes_ -> {
qrCodes_.forEach(code_-> {
qr.setMenuUrl(menuUrl);
qr.setMenuType(menuType);
});
qrCodeRepo.saveAll(qrCodes_);
});
Why use map when you are returning same qr. You can skip that part and also the collect function. We already have the list of qrCodes, just iterate them and set the values then save all at once.