Reputation: 1367
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.
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
Upvotes: 0
Views: 194
Reputation: 55
Just my understanding For #1 The stream of the object after serialization includes basically 5 parts:
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