About the java serialization problem

1. Scene description: recently I was watching serialization and encountered some doubts

  //
  public class parent implements Serializable{
        private static final long serialVersionUID = 7604030015683112269L; //UID
        private String name;
        
        //setter/getter...
  }
  //
  public class Child extends parent {
        //private String attr1;//
  }

  //
  public class Test{
    public static void main(String[] args) throws Exception{
        //serializeTest(); //
        Child c = deserializeTest();
        System.out.println(c.toString());
    }

    /**/
    public static void serializeTest() throws IOException {
        Child c= new Child();
        c.setName("wyy");
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(new File("d:/test.txt")));
            oos.writeObject(c);
            System.out.println(c.toString());
            System.out.println("success");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            oos.close();
        }

    }

    /**/
    public static Rich deserializeTest() throws IOException {
        Child c = null;
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(new File("d:/test.txt")));
            c = (Child) ois.readObject();
            System.out.println("");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            ois.close();
        }

        return c;
    }
  }

2. Problem description:

a.
b.
c. InvalidClassExceptionUID..
d.ID  
("" "" "" 
)

3. Doubt:

    ID
 
@TransientInvalidClassException...

Why is there an error if no serialVersionUID, subclass adds new attributes and then deserializes in the test case, while the attributes of the class are often added in the development, and there is no serialVersionUID, in the class but no error is reported? Is it because you connected to the database?

=
I hope the boss who knows about this will give us some advice. Thank you

Mar.22,2021

I wonder if the landlord has tried cache such as memcache to do the test. Your test method uses java object serialization, of course, there is a strict judgment on class bytecode, in actual development, if the scenario is to extract this entity class from the database, or save to the database, this process does not involve java object serialization, so you can add fields at will. However, if cache storage is involved, for example, some memcache library storage objects are stored with java object serialization, then there will be inconsistent serialization and deserialization versions. So this question still depends on whether the usage scenario is designed to serialize java objects.


Test as follows

BA A id



B

clipboard.png

id

w

clipboard.png

r
clipboard.png


entity id


clipboard.png

if there is an inconsistency in the source code that is directly compared in this way, an InvalidClassException exception is thrown

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e47b80-43990.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e47b80-43990.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?