Why should Hibernate's entity class inherit Serializable?

Hibernate has a second level cache, the cache will write the object to the hard disk, it must be serialized, and compatible with the transmission of the object in the network and so on.

Several common classes in java (such as: Interger, String, etc.) all implement the java.io.Serializable interface.

Classes that implement the java.io.Serializable interface are serializable. Classes that do not implement this interface will not be able to have any of their state serialized or deserialized. All subclasses of a serializable class are themselves serializable. This serialization interface does not have any methods and fields, and is only used to identify the semantics of serialization.

To be precise, it should be the serialization of objects. Generally, when the program is running, objects are generated. These objects disappear when the program stops running, but if we want to put some objects (because they are objects, they have different characteristics) Saved, after the program is terminated, these objects still exist, you can read the values ​​of these objects when the program runs again, or use these saved objects in other programs. In this case, serialization of objects is used.

Only serialized objects can be stored on storage devices. The interface that needs to be inherited for the serialization of objects is only a symbolic interface, that is to say, inheriting this interface means that the object can be serialized, and there is no other purpose. The reason why object serialization is needed is because sometimes objects need to be transmitted over the network. This serialization process is required during transmission. The serialized object is taken out from the server hard disk, and then transmitted to the client through the network, and then the client The terminal reads the serialized object into memory and performs the corresponding processing.

The content in the second-level cache is persisted and saved to facilitate the restoration of cached information. By using serialization, hibernate's cache mechanism determines that it should be a serialization-based cache. If there is no serializable interface, use the write of objectOutputStream during serialization. The (object) method will throw an exception when saving the object to a file.

Hibernate does not require that persistent classes must implement the java.io.Serializable interface, but for Java applications that use a distributed structure, when Java objects are transmitted between different process nodes, the class to which the object belongs must implement the Serializable interface. In addition, , In a Java Web application, if you want to persist the Java object stored in the HttpSession, the class to which the Java object belongs must also implement the Serializable interface.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324837403&siteId=291194637