'Spring CrudRepository Update doesn't work

I have a project in spring boot and I'm using CrudRepository, but when I try to update, it doesn't do anything.

@Entity
public class PfmSelection implements Serializable{
    
    @Id
    private Integer releaseId; 
    private String preparedBy;
}

Repositiry

@Repository
public interface IPfmSelectionDao extends CrudRepository<PfmSelection, Integer> {

}

Service

public interface IPfmSelectionService {

    public PfmSelection save(PfmSelection pfmSelection);

    public PfmSelection findById(Integer id);

}

Service Impl

@Service
public class PfmSelectionService implements IPfmSelectionService {

    @Autowired
    private IPfmSelectionDao pfmSelectionDao;

    @Override
    @Transactional
    public PfmSelection save(PfmSelection pfmSelection) {
        return this.pfmSelectionDao.save(pfmSelection); 
    }

    @Override
    @Transactional(readOnly = true)
    public PfmSelection findById(Integer id) {
        return this.pfmSelectionDao.findById(id).orElse(null);
    }

}

Service where I use the other Service

@Autowired
    private IPfmSelectionService pfmSelectionService;

private void updatePfm(PushModel pushModel) {
    PfmSelection pfm = this.pfmSelectionService.findById(167427);
    
     pfm.setPreparedBy("Rodrige");
     pfmSelectionService.save(pfm);

}

I don't receive any error in the console.

enter image description here



Solution 1:[1]

You need to take a few steps to know what the problem is

  1. Take the return of pfmSelectionService.save(pfm) and print the saved instance returned like below:

    private void updatePfm(PushModel pushModel) {
        PfmSelection pfm = this.pfmSelectionService.findById(167427);
        pfm.setPreparedBy("Rodrige");
        PfmSelection pfm2 = pfmSelectionService.save(pfm);
        System.out.println(pfm2.getPreparedBy());
    }
    
  2. Put logger/debugger inside the save method, before and after the save method and check for the entry/exit sop/logger statements in log/console like

    @Override
    @Transactional
    public PfmSelection save(PfmSelection pfmSelection) {
         System.out.println("Inside save method");
         PfmSelection pfmSelectionSaved = 
                           this.pfmSelectionDao.save(pfmSelection);
         System.out.println("Exits save method");
         return pfmSelectionSaved; 
    }
    
  3. Check for any Aop around advice or any place where the exception is being caught but eaten/not thrown further.

  4. Check if there is any update query fired in the logs at the time of save call.

  5. Also check if the setter method pfm.setPreparedBy("Rodrige"); is Empty?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yogesh Sharma