Storing Serializable Objects in the Database Storing Serializable Objects in the Database database database

Storing Serializable Objects in the Database


You can pass a ByteArrayOutputStream and then store the resulting stream.toByteArray() in the database as blob.

Make sure you specify a serialVersionUID for the class, because otherwise you'll have hard time when you add/remove a field.

Also consider the xml version for object serialization - XMLEncoder, if you need a bit more human-readable data.

And ultimately, you may want to translate your object model to the relational model via an ORM framework. JPA (Hibernate/EclipseLink/OpenJPA) provide object-relational mapping so that you work with objects, but their fields and relations are persisted in a RDBMS.


Using ByteArrayOutputStream should be a simple enough way to convert to a byte[] (call toByteArray after you've flushed). Alternatively there is Blob.setBinaryStream (which actually returns an OutputStream).

You might also want to reconsider using the database as a database...


e.g. create ByteArrayOutputStream and pass it to ObjectOuputStream constructor