Why does hibernate pojo implement the serializable interface?

Source: Internet
Author: User
Tags object serialization

 

If you do not want to think about this problem, Google it. Find an article about serialization. Many people have explained this question: hibernate has a second-level cache, And the cache writes the object to the hard disk, so it must be serialized, and transfer of compatible objects in the network ...?

Objective: to save the content in the second-level cache persistently to facilitate the restoration of cache information. By using serialization, Hibernate's cache mechanism determines that the cache should be based on serialization. If the serializable interface is not available, during serialization, an exception occurs when you use the write (object) method of objectoutputstream to save an object to a file .?

How many common classes (such as interger and string) in Java have implemented the serializable interface?

Interface java. Io. serializable?

Public interface serializable?

Classes that implement the java. Io. serializable interface can be serialized. Classes that do not implement this interface Cannot serialize or deserialize any of their statuses. All subclasses of the serialization class are serializable. This serialization interface does not have any methods and fields and is only used to identify the semantics of serialization .?

When controlling serialization, you may also encounter a situation where Java serialization mechanism is prohibited to automatically access a sub-object. This is a common requirement. You certainly do not want to serialize sensitive information such as passwords. This is because, once serialized, someone else can crack it by analyzing the file and intercepting the network transmission, so that even if the access permission is private, it cannot be guaranteed ..?
Serialization is a mechanism for processing object streams. Object streams are called object streams that stream object content. You can perform read and write operations on Streaming objects, or transfer streaming objects between networks. Serialization aims to solve the problems caused by read/write operations on Object streams .?
Serialization implementation: implement the serializable interface for the class to be serialized. There is no implementation method for this interface. implements serializable is only to mark that the object can be serialized, then, an output stream (such as fileoutputstream) is used to construct an objectoutputstream (Object stream) object. Then, the writeobject (Object OBJ) of the objectoutputstream object is used) you can write the object whose parameter is OBJ (that is, save its status). To restore the object, use the input stream .?

Serialization: serialization is the process of converting an object into a format that is easy to transmit. For example, you can serialize an object and Use http to transmit the object between the client and the server over the Internet. At the other end, deserialization reconstructs the object from the stream .?
Is a permanent object mechanism .?

Specifically, it should be object serialization. Generally, objects are generated when a program is running. These objects disappear as the program stops running, but if we want to put some objects (because they are objects, so they have different features). After the program stops running, these objects still exist and can be read when the program runs again, or use these stored objects in other programs. In this case, Object serialization is required .?

Only serialized objects can be stored on storage devices. The interface to be inherited for Object serialization is only a symbolic interface. That is to say, inheriting this interface indicates that this object can be serialized and has no other purpose. Object serialization is required because sometimes objects need to be transmitted over the network. During transmission, the serialized objects need to be taken out from the server hard disk and uploaded to the client through the network, then, the client reads the serialized object into the memory and executes the corresponding processing .?

Object serialization is a feature of Java. Through this feature, an object can be written into a group of bytecode. When reading these bytecode from other locations, a new object can be created accordingly, the new object is in the same state as the original object. To achieve Object serialization, you must be able to store and restore private variables of the objects class to ensure that the object state can be properly saved and restored. Correspondingly, the object serialization API can restore these values to private data members during object reconstruction. This is a challenge to Java access permissions. It is usually used for Object Exchange on the server client, and stored on the local machine .?

The most important use of Object serialization is to ensure the integrity and portability of objects when passing and saving objects. For example, when transmitting data over the network or saving an object as a file, you need to implement a serialization interface .?

Even if you have never used Object serialization, you may know it. But do you know that JAVA supports another form of Object Persistence and Externalization )??

Below are the code-level associations between serialization and Externalization :?

Public interface serializable {}?

Public interface externalizable extends serializable {?
Void readexternal (objectinput in );?
Void writeexternal (objectoutput out );?
}?

Comparison between Java. Io. externalizable and Java. Io. serializable?
What are the main differences between serialization and Externalization?

Externalization and serialization are two different methods to achieve the same goal. Next let's analyze the main differences between serialization and externalization .?

The support for Object serialization through the serializable interface is built in the core API, but all the implementers of Java. Io. externalizable must provide read and write implementations. Java already has built-in support for serialization. That is to say, as long as you make your own Java. Io. serializable class, Java will try to store and reorganize your objects. If you use externalization, you can choose to read and write data completely by yourself. The only support for externalization in Java is interface :?

Voidreadexternal (objectinput in )?
Void writeexternal (objectoutput out )?

Now, how can we implement readexternal () and writeexternal .?

Serialization automatically stores necessary information for deserializing the stored instance, while externalization only stores the identifier of the stored class. When you serialize an object through the java. Io. serializable interface, the information about the class, such as its attributes and the types of these attributes, are stored together with the instance data. When selecting the externalizable path, Java only stores very little information about each stored type .?

What are the advantages and disadvantages of each interface?

Serializable interface?

· Advantages: built-in support?

· Advantages: easy to implement?

· Disadvantage: Is the occupied space too large?

· Disadvantages: Is the speed slow due to additional overhead?

Externalizable interface?

· Advantage: low overhead (what does a programmer decide to store )?

· Advantages: Possible Speed improvement?

· Disadvantages: virtual machines do not provide any help. That is to say, all the work falls on the shoulders of developers .?

The choice between the two depends on the needs of the application. Serializable is usually the simplest solution, but it may lead to unacceptable performance problems or space problems; in the case of these problems, externalizable may be a feasible path .?

Remember that if a class is externalizable, The externalizable method will be used to serialize the instance of the class, even if this type provides the serializable method :?

Private void writeobject ()?
Private void readobject ()?

Add another note :?

Equals/hashcode to view the JDK documentation, which is generally used for you to define when two objects are equal (equals ). For example, you can define that two person objects with the same ID card are equal. When you Overload equals, you must override hashcode?

The serializable interface indicates that the object can be serialized, or the object can be written to a file from the memory, or transmitted over the network. This is the requirement of the EJB specification. Can these objects be transmitted over the network for remote calls?
Hashcode is rewritten because hibernate has its own hashcode algorithm to distinguish various pojo states, such as free States .?
It is best to implement each pojo of the serialized interface. It is not necessary to compose the primary key .?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.