1

Since we implemented Entity Auditing, we were able to store CreatedBy using AuditorAware also @lastmodifiedby too. But here the problem is, once we updated the record again we are trying to fetch the updated record using findbyId but its giving @createdby and @createddate as null, and @lastmoidified date also a different example in DB 2021-10-02 17:43:05 but in the response 2021-10-02T12:30:29.596038100Z

Even I got a few solutions from the web listed below

  1. SaveAndFlush - This will disturb the @transactional model, so we cannot use this,

Sample Code :

@GetMapping(value = "/saveandshow")
public Object GetsavedValue(@RequestParam("name") String name,@RequestParam("id") Long id ) {
    //List<Account> list =  accRepo.findAll();
    Account acc_find = accRepo.getOne(Long.valueOf(id));
    Account account = new Account();
    account.setId(acc_find.getId());
    account.setAccountname(name);
    //account.setAge(name);
    Account acc = accRepo.save(account);
    Optional<Account> fetchAccount = accRepo.findById(acc.getId());
    System.out.println("Accont "+fetchAccount); 
    return "Success";       
}

am i doing something wrong ..?

ref : https://github.com/vivekjava/Audit/blob/main/src/main/java/com/vivek/rest/AccountResource.java#L37

Please provide youre thoughts or inputs, I really appreciate it.

vivek java
  • 59
  • 6
  • Can you provide sample of how you're saving and `find`ing? It could be because of the changes have not been flushed. https://stackoverflow.com/help/minimal-reproducible-example – Mansoor Oct 02 '21 at 12:54
  • Where are those "few solutions"? – James Z Oct 02 '21 at 13:01
  • Here you can find the sample project https://github.com/vivekjava/Audit/blob/main/src/main/java/com/vivek/rest/AccountResource.java#L37 , – vivek java Oct 03 '21 at 03:52
  • I found this (https://stackoverflow.com/questions/20382586/spring-data-repository-save-not-returning-instance-with-updated-audit-fields) as an alternate solution, but I realize that save and flush may cause problems – vivek java Oct 03 '21 at 03:57
  • Please include your `Account` entity declaration in the question. – Ratul Sharker Jan 26 '22 at 05:34

0 Answers0