2

I'm developping an application spring-boot, I want to compare to users object

public class User {

    private String name;
    private String firstName;
    private String codeEmpl;
    private String codeCompany;

    // Getters & setters
}

When I update user I got from the frontend only the modified fields then I have to compare these fields with the saved fields in database. The copy the saved information on the User that I have received. My question is how I can compare only the dynamique fields received from frontend ? and how I can copy only the saved information on the received object ?

Concerning Compare: I override the equals but the equals compare all the fields one by one.

Concerning Copy: I used

BeanUtils.copyProperties(orig, dest); 

But this it copy all fields

Would you have any ideas ?

Best regards

piet.t
  • 11,718
  • 21
  • 43
  • 52
Victor
  • 385
  • 2
  • 12
  • 26
  • 2
    Here's an answer that seems extremely similar to what you're trying to do: https://stackoverflow.com/a/3521314/7303349 – Simon Visser Jun 21 '17 at 12:27
  • Did you see this answer? https://stackoverflow.com/a/3521314/2387977 – Dherik Jun 21 '17 at 12:42
  • thanks @SimonVisser but after copy I have to keep alse the origin fields in the destination. For example if I copy a user that have 2 fields in another that have 3 fields I want that the destination user have 3 fields not 2 – Victor Jun 21 '17 at 12:43
  • thanks @Dherik I see it but the copy in the example doesn't keep the fields that not are in the orig object. I need for example if I have an origObj that contain only two fields and the origObj that conatain the hole information to copy only the two fields. This method do the cut not the copy – Victor Jun 21 '17 at 12:57

1 Answers1

0

Unless you want to implement a complex solution, I suggest you to send the bean with all the field values from the back-end to the front-end as a first step of the edition process. Then, after the edition, send back the bean from the front-end to the back-end with the modified values and the unmodified ones. You could use one flag field to indicate if any modification has occurred.

Once you have the bean with all the values in the back-end you can persist to the database or any persistence infrastructure you use.

Actually, that's the way frameworks like Angular.js or Backbone.js are supposed to work when interacting with a REST API.

Andres
  • 10,561
  • 4
  • 45
  • 63
  • thanks @Andres I know is complex but the problem is that we have two frontend system one that can send all value to the back and another that send only a piece of information because it hasn't all the information about users – Victor Jun 21 '17 at 14:54