File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Difference between serializing an object and saving its state in the Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between serializing an object and saving its state in the Database" Watch "Difference between serializing an object and saving its state in the Database" New topic
Author

Difference between serializing an object and saving its state in the Database

Nittin singla
Greenhorn

Joined: Jul 02, 2011
Posts: 24
Just a basic Question..What is the difference between serializing an object i.e extending serializable interface , then saving its state in some .ser file and saving the state of an object on Database??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Welcome to the Ranch

In the case of serialisation, the whole object (its whole graph) is saved as a single file. If that object is a list, it is still one file. You can reconstruct that object from that single file. But you can't readily use that for anything else.
Storing the state in a database (unless you store the whole object as a BLOB of its serialised state) means you would have to use constructors and set methods to reconstruct it from the database. But its state inthe database can readily be used by anything else which uses the database.

Anybody else?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Campbell Ritchie wrote:In the case of serialisation, the whole object (its whole graph) is saved as a single file. If that object is a list, it is still one file. You can reconstruct that object from that single file. But you can't readily use that for anything else.

I'd replace "file" with "sequence of bytes". You can use an in-memory byte[], use sockets, or as you said store the serialized object in the database as a BLOB. A file is only one form of storing a sequence of bytes.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Rob Spoor wrote:I'd replace "file" with "sequence of bytes". You can use an in-memory byte[], use sockets, or as you said store the serialized object in the database as a BLOB. A file is only one form of storing a sequence of bytes.


Are they really an array of bytes or an array of octets? Sure, if we all are using Intel X86 architectures, it works. But if you have another architecture on the other end of your socket or network connection, then it gets more complex.

The short answer is if you save it into a DBMS package, it worries about a lot of the marshalling, conversion, big-endian, etc. stuff. If you just serialize it, you get to worry about all that stuff.

Also, there is a difference between storing the array of bytes/octets in a blob in the DBMS and actually storing the fields is columns of the DBMS. With the first, you can have serious problems when the format of the data changes, field types change, additional fields are added, etc. If you use the DBMS to manage that, you can write code that is robust against many reasonable changes to the data, and even get both upward and backward compatibility.

Java Serialized objects have real issues when you change the format of the complex object.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between serializing an object and saving its state in the Database