This is the sixth day of my participation in the More text Challenge. For details, see more text Challenge

The cognition of serialization has been convenient for network transmission, it is not clear how it works. At the same time, it is found that there is an item entity that does not show to call Serializable.

What is serialization

  1. Serialization: Converting a Java object to a sequence of bytes.
  2. Deserialization: Restore a sequence of bytes to the original Java object.

Why you need serialization

Facilitates object persistence and transfer.

How to serialize

To serialize an object in Java, you must implement the Serializable interface. Then you can persist and deserialize.

Create a table a and b

-- Table structure for a -- ---------------------------- DROP TABLE IF EXISTS `a`; CREATE TABLE `a` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, `address` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, `create_date` datetime(0) DEFAULT NULL, `remark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the Records of a -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- INSERT INTO ` a ` VALUES (10, 'daming', 'in guangdong, '2021-06-05 19:18:31', 'hi'); INSERT INTO ` a ` VALUES (11, 'daming', 'in guangdong,' the 2021-06-05 19:18:31 ', 'hi'); SET FOREIGN_KEY_CHECKS = 1;Copy the code

Generate the base code using the plug-in

See EasyCode MybatisCodeHelper for IDEA

Remove B implements Serializable,Entity A, B

public class A implements Serializable {
    private static final long serialVersionUID = -82776330359794256L;
    
    private Integer id;
    
    private String name;
    
    private String address;
    
    private Date createDate;

    private String remark;
Copy the code
public class B{
    private Integer id;
    
    private String name;
    
    private String address;
    
    private Date createDate;

    private String remark;
Copy the code

Call the save method of A and B respectively

Show that calling the implementation of the Serializable interface doesn’t seem useful at this point

 * @author  unascribed
 * @see java.io.ObjectOutputStream
 * @see java.io.ObjectInputStream
 * @see java.io.ObjectOutput
 * @see java.io.ObjectInput
 * @see java.io.Externalizable
 * @since   JDK11.* /public interface Serializable {}Copy the code

View the source code

The source data types already implement Serializable by default

So why do we want to show calls to Serializable?

deserialization

@SpringBootTest
class DemoApplicationTests {

    @Test
    void contextLoads(a) {
        A a = new A();
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("G:\\userA.txt"));
            objectOutputStream.writeObject(a);
            objectOutputStream.close();
        } catch(IOException e) { e.printStackTrace(); }}@Test
    void contextLoads2(a) {
        B b= new B();
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("G:\\userB.txt"));
            objectOutputStream.writeObject(b);
            objectOutputStream.close();
        } catch(IOException e) { e.printStackTrace(); }}Copy the code

Deserialization A is fine, deserialization B is abnormal

Step in and look at the mistakes

Obj instanceof Serializable judgment when deserialization, Serializable only serves as an identifier

Why declare serialVersionUID

The default serialVersionUID value for a class depends entirely on the Implementation of the Java compiler. For the same class, compiling with different Java compilers may result in different serialVersionUID values. Explicitly defining serialVersionUID serves two purposes:

1. You want different versions of the class to be compatible with serialization. Therefore, ensure that different versions of the class have the same serialVersionUID. In some cases, you don’t want different versions of a class to be serial-compatible, so you need to make sure that different versions of the class have different serialVersionUID. 2. When you serialize a class instance and want to change a field or add a field, do not set serialVersionUID. Any change will cause the old instance to be unable to be unordered and will throw an exception when deserialized. If you add the serialVersionUID, when you unsequence the old instance, the newly added or changed field value will be set to the initialization value (null for the object and the corresponding initial default for the base type), and the field will not be set if it is deleted.

summary

1. Facilitate object persistence and transmission. 2.Serializable is an empty interface and only serves as identification 3. When a parent implements serialization, a child class automatically implements serialization and does not need to explicitly implement the Serializable interface