Issue
Inside restController package
@PutMapping("update/{id}")
public void update(@RequestBody UpdateLanguagesRequest updateLanguages,int id) {
this.languagesService.update(updateLanguages, id);
}
@Override
public void update(UpdateLanguagesRequest updateLanguageRequest,int id) {
// TODO Auto-generated method stub
Languages language=this.languageRepository.findById(id).get();
language.setName(updateLanguageRequest.getName());
this.languageRepository.save(language);
this.languageRepository.flush();
//language=getLanguageById;
}
After updatind spesific element(e.g id=2) in database it becomes id 3 comes before than id 2 how can i fix this?(I did not add the other apis like get add that is because they are working without error)
Solution
I have taken a quick look at your repository, I'll show the two required modifications. You can add a findAllOrderByIdAsc
method to your repository:
public interface LanguagesRepository extends JpaRepository<Languages,Integer> {
List<Languages> findAllByOrderByIdAsc();
}
and use it in your service layer instead like this:
@Override
public List<GetAllLanguagesResponses> getAllResponse() {
// TODO Auto-generated method stub
List<Languages> languages=this.languageRepository.findAllByOrderByIdAsc();
//this.languageRepository.flush();
List<GetAllLanguagesResponses>languageResponse=new ArrayList<>();
for(Languages language:languages) {
GetAllLanguagesResponses responseItem=new GetAllLanguagesResponses();
responseItem.setId(language.getId());
responseItem.setName(language.getName());
languageResponse.add(responseItem);
}
return languageResponse;
}
Answered By - void void
Answer Checked By - David Marino (JavaFixing Volunteer)