How can I get the oldObject at any point of time?

Viewed 51

Let say I have the following POJO

public class Plan{
    private Long id;
    private String name;
    //other fields,getters, builder..
}

Now let say there is some Plan which already exist in db.

Now I am trying to modify/update various fields of this particular Plan by using the restAPI(api/v1/Plan/123). Now we use Jackson here,so I will get the updated Plan object.I will be validating this updated Plan object and if all good will persist in the db. Till here there is no issue.

But we using an internal service to audit this,i.e what all details got modified in this particular Plan and done by whom at what time etc. For ex:Plan name changed from (Plan1 to Plan2) Internal service will accept argument as (oldPlan,newPlan).

Now how can I get the oldPlan from the newPlan object?[Since the file which I have now got the method as public Plan update(Plan newPlan),inside this method only we will be calling an internal service method which requires oldPlan]

Constraints:

1.I should not fire the query getting the id from the newPlan,since that will be duplicate query coz it was already queried when creating newPlan object using jackson.

2.I should not change the update method signature and in update method the plan object which we receive via argument is the object which got updated details in it.

What I have in my mind is: a)Having the Plan object itself in the POJO ,so that when I get newPlan.getPlan() will return the oldInstance?

The above approach can be done but I am not sure abt this.Whats the best approach to achieve this?

0 Answers
Related