Using Cloudscape's Java Extensions
Page 4 of 12

Storing Java Objects in Cloudscape Tables

Cloudscape allows you to define a column as a serialization of a class or interface. Serialization is a Java mechanism for reading and writing objects to a stream. To store serialized instances of a class or interface in the database, the class must meet the requirements for serialization. For information about creating serializable Java data types, see Programming Serializable Classes.

Once a column is defined to hold serialized instances of a class (or serialized instances of a subclass or class that implements the interface), you can:

Defining a Column to Store a Java Data Type

When defining any column in a table, you assign it a name and a data type. For example, to define a column in which you want to store a city's integer id number, you need to give it a name and an SQL data type:

city_id INT

For Java data types, you must use the keyword SERIALIZE along with the class or interface name (or class alias) to specify the serialized Java class as the data type:

columnName SERIALIZE( { JavaClassName | ClassAlias })

The Java class must meet the requirements for serialization (see Requirements for Serialization).

For example, to store the Java class JBMSTours.serializabletypes.City (aliased as City) in a table along with its SQL-92 INT id, you would define the table as follows:

CREATE TABLE Cities (city_id INT,
   city SERIALIZE(City))

Storing Objects in the Column

Once the table is defined, you can store any object that is an instance of a class or a sub-class of the class named in the column definition. If the column is defined to serialize an interface, you can store an object that is an instance of a class that implements the interface.

From a Java application, you typically insert a row of object values using dynamic parameters and then pass in the in-memory objects. For example, to insert an instance of the Java class City assigned to the variable name theCity in your application, use JDBC methods as shown in the following example:

insert = conn.prepareStatement(
    "INSERT INTO Cities (city_id, City) VALUES(?, ?)");
insert.setInt (1, 1); insert.setObject (2, theCity); insert.executeUpdate();

Cloudscape Database Design Considerations

Because you have the ability to store data as objects or as SQL-92 data types, you must answer some basic design questions for a Cloudscape database:

  • What data do you want to store as standard SQL data types?
  • What data do you want to store as Java objects?

Here are a few guidelines:

  • Store well-defined Java objects you are already working with as objects.
  • Store complex data as objects.
  • If an object is not complex--i.e., if its only methods are simple set and get methods that access basic fields--store such data as SQL data types. Storing simple data as objects has some disadvantages:
    • Even if only a single field is updated in an object, Cloudscape must write out the entire object.
    • You may lose some database consistency features of the DBMS such as foreign keys.

      Even if you decide to store an object as an object, for performance reasons you might want to store some information separately in columns that store built-in types so that you can index them.

Another design question to answer is:

  • Is it better to store complex objects in the database or to store references to external files?

The advantages of storing the actual objects in the database are:

  • You gain transactional control of your objects.
  • You can use SQL-J to access the objects, which is a more natural integration. If you store objects as external files, you must manage them yourself.
  • You can use a program in either the embedded or the server mode (i.e., you don't have to worry about installing the external files in a deployed application).

In addition to these points, the standard relational database design considerations apply for designing a Cloudscape database.