Datasheet

Serialization
You can save data stored in an object by creating a flat-file representation of the object. In the Java lan-
guage, this process is called serialization. An object that is serialized has all its attributes and their class
types saved into a file or a buffer. The resulting information string can be saved to a file, placed in a col-
umn in a traditional relational database, or sent to another machine. An example is the CD class:
public class CD implements Serializable {
String title;
String artist;
public CD(String title, String artist) {
this.title = title;
this.artist = artist;
}
}
The CD class has two attributes that need to be saved when the object is serialized. In Java, all primitive
types as well as many of the foundational classes are defined such that they implement the Serializable
interface. The system automatically recurses into each of the attributes as needed.
The serialization of the CD class results in a binary representation of the data currently contained in the
represented object. The binary representation could be placed in a BLOB column type of a relational
database; this process would allow other applications to access the data. However, if a legacy application
has been tweaked to access the column where the object is held, it won’t be able to make sense of the
data unless the legacy application can deserialize Java objects. Even worse, the serialization process
doesn’t migrate well from Java application to Java application.
Further, the serialization process isn’t fast, and a large object can take a considerable amount of time to
be put into its binary representation. Thus, serialization as a practice has a specific place in the develop-
ment process; but as a mechanism for persisting data, it should be avoided.
XML
In the past few years, XML has been one of the hottest technologies. With this popularity comes the issue
of using XML in both objects and mapping to a database. First, consider an XML document like the
following:
<cd>
<title>
Grace Under Pressure
</title>
<artist>
Rush
</artist>
</cd>
A database to handle the XML document can be created with the following schema:
2
Chapter 1
03_576771_c01.qxd 9/1/04 12:09 PM Page 2