Possible Duplicates:
Do Hibernate table classes need to be Serializable?
What does Serializable mean?
public class ExampleEntity implements Serializable
{
@Id
private long id;
private int fieldInt;
private long fieldLong;
private String fieldString;
}
I am looking after JPA tutorials. I am able to understand the basic concepts, but in all the tutorials they have added, this serializable. What is the purpose of this? How does that help me? Any suggestions Please?
To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java.
Serialization is the process of converting a data object—a combination of code and data represented within a region of data storage—into a series of bytes that saves the state of the object in an easily transmittable form.
Serialization is the conversion of the state of an object into a byte stream; deserialization does the opposite. Stated differently, serialization is the conversion of a Java object into a static stream (sequence) of bytes, which we can then save to a database or transfer over a network.
Serialization in Java allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage. Deserialization is the process of converting Object stream to actual Java Object to be used in our program.
It means that the class can be serialized. This means that it can be converted into an stream of 0's and 1's and be sent to other service, like a webservice or and ORM (hibernate) or whatever. Then this service knows how store the stream.
Also, your program can receive a serialized class and create an instance for it from the stream of 0's and 1's.
It's the way to "save" instances of classes and "restore" them at any other moment in time.
More info at
To be able to make a class serializable, you must implement the interface "Serializable".
Why so? Because when the moment to serialize a class arrive, the function writeObtject
will be called to convert the object into bytes. On the other hand, when you have the bytes and you want to get the instance of the class, the function readObject
will be called.
Why is not automatic? A variable value it's already represented by bytes. Because the problem comes for example when a class holds instances of other classes. What do you do? You serialize the other classes as well? You just keep the reference address? It's pretty complex and it may be dependant on your type of project and design.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With