To solve a problem, we need to set the non-basic data type of the object, such as some custom types, such as in my package: COM. shutao. and some java. util. the set type is set to null. A general class is written here, including the setter method that calls your own class and the parent class. For details, refer:
/*** Call the setter method of the object and set non-basic data types to null values * @ author linshutao **/public static void setfieldtonull (Object O) {// obtain your own attribute field [] fields1 = O. getclass (). getdeclaredfields (); // obtain the attribute field [] fields2 = O. getclass (). getsuperclass (). getdeclaredfields (); // merge all attributes field [] fields = new field [fields1.length + fields2.length]; for (INT I = 0; I <fields1.length; ++ I) {fields [I] = fields1 [I];} For (Int J = 0; j <fields2.leng Th; ++ J) {fields [fields1.length + J] = fields2 [J];} For (INT I = 0; I <fields. length; I ++) {string fieldname = Fields [I]. getname (); // attribute name string filedtype = Fields [I]. getType (). tostring (); // attribute type // remove the previous class, inteface. These descriptions are filedtype = filedtype. substring (filedtype. indexof ("") + 1); // if it is my custom type if (filedtype. indexof ("com. shutao. test ")! =-1 | filedtype. indexof ("set ")! =-1) {string firstletter = fieldname. substring (0, 1 ). touppercase (); // obtain the method name string setter = "set" + firstletter + fieldname. substring (1); try {// call its own method callmethod = O. getclass (). getmethod (setter, class. forname (filedtype); callmethod. invoke (O, new object [] {null});} catch (exception e) {try {// The method called above will throw an exception if the call fails, the method callmethod = O. getclass (). getsuperclass (). getdeclaredmethod (setter, class. forname (filedtype); callmethod. invoke (O, new object [] {null});} catch (exception E1) {log. error (e1.getmessage (), E1 );}}}}}
In addition, in order to avoid the lazy loading exception caused by the Set set due to lazy = true, the following method is required:
/*** Call the set-type getter method of the object to instantiate the set and avoid exceptions caused by lazy of hibernate * @ author linshutao **/public static void callgetmethodforsetfield (Object O) {field [] fields = getfieldsincludeparent (o); For (INT I = 0; I <fields. length; I ++) {string fieldname = Fields [I]. getname (); // attribute name string filedtype = Fields [I]. getType (). tostring (); // attribute type // remove the previous class, inteface. These descriptions are filedtype = filedtype. substring (filedtype. indexof ("") + 1); // if it is set type if (filedtype. indexof ("set ")! =-1) {string firstletter = fieldname. substring (0, 1 ). touppercase (); // obtain the method name string method = "get" + firstletter + fieldname. substring (1); try {// call its own method callmethod = O. getclass (). getmethod (method, new class [] {}); // call the set size to instantiate set = (SET) callmethod. invoke (O, new object [] {}); set. size ();} catch (exception e) {try {// The method called above will throw an exception if it cannot be called. Here, call the method callmethod = O of the parent class. getclass (). getsuperclass (). getdeclaredmethod (method, new class [] {}); Set set = (SET) callmethod. invoke (O, new object [] {}); set. size ();} catch (exception E1) {log. error (e1.getmessage (), E1 );}}}}}