Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialized JavaBeans

 
Dominic Paquette
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I think I'm not getting about the use of serialized java beans, here is what's confusing me. If I understand correctly we can use JavaBeans to permenantlly store data (like a database)for example if I have a web app that stores info on employees I could hava an employee java bean. Each employee would have one file in a special directory.
example:
/employees/employeeID.ser
I understand the mechanism of serialized java beans, I just wanted to know if it is a "good practice" to proceed this way. I mean, if you add a field to your employeeBean class then your existing serialized beans will be unusable (I think you get a InvalidCastException) so for me this technique is not really good if you need extensibility?
If anyone has a view on this please let me know. I haven't done any real enteprise apps in java before so I could you someone else's experience
Thanks in advance
Dominic
 
Sam Dalton
Author
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dominic,
I really would not recomend this as a way to persist data for any length of time. The reasons are varied and numerous, a few are:
a) If you ever change the implementation of the employee java bean, the previously serialised objects will no longer be unserialisable. You will get ClassCastExceptions all over the place.
b) Serialization is a performance killer. You will have no caching (unless you write it yourself) etc.
It would be much better to use a database to store your persistent information IMHO.
Hope this helps
Sam
 
Dominic Paquette
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering why is sun putting that in their objectives if it's an inefficient technique. Do you know any other uses for Serialized java beans or sould I just forget about them (after the exam )
Dominic
 
Sam Dalton
Author
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dominic Paquette:
I was wondering why is sun putting that in their objectives if it's an inefficient technique. Do you know any other uses for Serialized java beans or sould I just forget about them (after the exam )
Dominic

There are lots of uses for serialization in general. It is a powerful technique (and used internally by many app servers and databases) so it is well worth understanding it.

Sam
 
Rick Portugal
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This month's JavaPro magazine has an interesting article that relates to this topic. Check out
http://www.fawcette.com/javapro/2003_02/magazine/features/dsavarese/page2.asp
Essentially, the advantage of serialization is that it's simple to do.
The cons are that only Java programs can read the serialized objects, serialized objects can't be queried (like in SQL), and serialization is not good for storing a lot of information.
It can be the right choice if you have a small amount of data and you want to avoid all the overhead and licensing fees associated with a relational database.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic