Home > Back-end >  How will be the same entity class two all attributes compared one by one
How will be the same entity class two all attributes compared one by one

Time:05-09

Have two instance variables, storing information is modified before and after modification,
For example,
LibraryDTO old=new LibraryDTO ();
LibraryDTO new=new LibraryDTO ();

I now need to look at all these two entity class attribute's value to compare, and will be listed there, divided into two fields into a table, divided into content after the modification of content and modify before
How do I achieve?

CodePudding user response:

Handwritten words cycle judgment is a little hard coded
Or each object obtained by reflecting all of the fields to compare

CodePudding user response:

Public static Map CompareTom (Tom before and after) Tom throws the Exception {
List FieldList=new ArrayList<> (a);
FieldList. AddAll (arrays.aslist (before getClass () getDeclaredFields ()));
The Object old;
The Object now;
For (Field Field: fieldList) {
field.setAccessible(true);
Old=field. The get (before);
Now=field. The get (after);
If (JSON. ToJSONString (old) equals (JSON. ToJSONString (now))) {
Field. The set (before, null);
Field. The set (after, null);
}
}
Map CompareResult=new HashMap<> (a);
CompareResult. Put (" beforeModify ", JSON. ToJSONString (before));
CompareResult. Put (" afterModify ", JSON. ToJSONString (after));
Return compareResult;
}

CodePudding user response:

2 floor have a reference to raw response:
public static Map CompareTom (Tom before and after) Tom throws the Exception {
List FieldList=new ArrayList<> (a);
FieldList. AddAll (arrays.aslist (before getClass () getDeclaredFields ()));
The Object old;
The Object now;
For (Field Field: fieldList) {
field.setAccessible(true);
Old=field. The get (before);
Now=field. The get (after);
If (JSON. ToJSONString (old) equals (JSON. ToJSONString (now))) {
Field. The set (before, null);
Field. The set (after, null);
}
}
Map CompareResult=new HashMap<> (a);
CompareResult. Put (" beforeModify ", JSON. ToJSONString (before));
CompareResult. Put (" afterModify ", JSON. ToJSONString (after));
Return compareResult;
}

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
You define I literally object Tom to you want to use the LibraryDTO is ok, any object, but is limited to its own attributes, excluding it from the parent class inherited properties, if the parent class inherited property, use the following method
Public static Map CompareTom (Tom before and after) Tom throws the Exception {
List FieldList=new ArrayList<> (a);
FieldList. AddAll (arrays.aslist (before getClass () getDeclaredFields ()));
The Class superClazz=before. GetClass (). GetSuperclass ();
While (superClazz!=null) {
FieldList. AddAll (arrays.aslist (superClazz getDeclaredFields ()));
SuperClazz=superClazz. GetSuperclass ();
}
The Object old;
The Object now;
For (Field Field: fieldList) {
field.setAccessible(true);
Old=field. The get (before);
Now=field. The get (after);
If (JSON. ToJSONString (old) equals (JSON. ToJSONString (now))) {
Field. The set (before, null);
Field. The set (after, null);
}
}
Map CompareResult=new HashMap<> (a);
CompareResult. Put (" beforeModify ", JSON. ToJSONString (before));
CompareResult. Put (" afterModify ", JSON. ToJSONString (after));
Return compareResult;
}

CodePudding user response:

Remember this demand front line
  • Related