Priyak Dey
Priyak Dey

Reputation: 1367

Serialization: Basic understanding and documentation

Within a stream, the first reference to any object results in the object being serialized or externalized and the assignment of a handle for that object. Subsequent references to that object are encoded as the handle. Using object handles preserves sharing and circular references that occur naturally in object graphs. Subsequent references to an object use only the handle allowing a very compact representation.

Except for serializable fields, primitive data is written to the stream in block-data records, with each record prefixed by a marker and an indication of the number of bytes in the record.

  1. The above is from the Oracle Docs for Serialization. I am reading into Serialization to get a better and in-depth understanding, can someone please help me decode the highlighted parts in simple English. (I am looking for an answer in the context of how the stream is generated, especially when it talk about how the primitive data is being stored in the buffer)

Serialization is useful when we want to:

  • Persist the state beyond the lifecycle of the JVM - file/db
  • Send data over to another jvm based application over a network - data exchange

Doc: https://docs.oracle.com/javase/6/docs/platform/serialization/spec/serial-arch.html

  1. I have been working with Spring/Hibernate, and in none of the applications, the models are defined as type Serializable. The main reason to use Serializable is the version control for future enhancement, but without Serializable Hibernate persists the data in the DB/but serialization to a file using ObjectOutputStream fails. Can someone help me understand this, why does Hibernate not require entities to be of the type Serializable?

Upvotes: 0

Views: 194

Answers (1)

Ran
Ran

Reputation: 55

Just my understanding For #1 The stream of the object after serialization includes basically 5 parts:

  • A header to claim this is a serialized file and the version of the serialization.
  • The information of the class, like name, serialization id(serialVersionUID), number of fields etc.
  • Description of the fields
  • Information of parent class
  • Value of the fields.

Check these codes from ObjectOutputStream:

if (obj instanceof String) {
    writeString((String) obj, unshared);
} else if (cl.isArray()) {
    writeArray(obj, desc, unshared);
} else if (obj instanceof Enum) {
    writeEnum((Enum<?>) obj, desc, unshared);
} else if (obj instanceof Serializable) {
    writeOrdinaryObject(obj, desc, unshared);
} else {
if (extendedDebugInfo) {
    throw new NotSerializableException(
    cl.getName() + "\n" + debugInfoStack.toString());
} else {
    throw new NotSerializableException(cl.getName());
}

For #2 I think hibernate/jpa just write them into database by queries, they do not need to serialize the object. You can implment the entity with Serializable if you need to transfer then cross JVMs or other usage (I think you better to have serialization in a web based application).

Upvotes: 1

Related Questions