I have never seen the need for this, but I like to understand things in case I ever run across them “in the wild”. I did see it in a commercial ecommerce application in which the designers serialized an “Order” object into a BLOB in an Oracle database table column.
This avoided the overhead of a straight object relational mapper such as Hibernate or the ATG repository, so I guess that is a positive. In this case, they stored other columns such as the ORDER_ID for fast lookups, and then pulled the entire “object” back into the application server and de-serialized it.
Below is an example:
import java.sql.*;
import java.io.*;
public class Order implements Serializable {
int id;
int quantity;
int productId;
Order (int id, int quantity, int productId) {
this.id = id;
this.quantity = quantity;
this.productId = productId;
}
public static void main(String args[]) {
try {
Connection con = DriverManager.getConnection("jdbc:oracle:thin:foo/bar@dbhost:1521/dbservice");
PreparedStatement pst = con.prepareStatement("insert into orders values(?)");
for (int i = 1; i <= 1000; i++ ) {
Order o = new Order(i,i,i);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream objOstream = new ObjectOutputStream(baos);
objOstream.writeObject(o);
pst.setBytes(1, baos.toByteArray());
pst.execute();
con.commit();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Pulling it out of the database was done in a similar fashion, but is not shown here.