Friday, April 28, 2006

Bodington DB Layer and Empty Subclasses

The Bodington VLE has its own DB layer that allows Java objects to be stored and loaded from the database without having to touch JDBC and SQL. If you have an object that can already be persisted in the database layer and want to subclass it without adding any instance fields (so you don't need an extra table) you can store both the orginal class and the subclass in the same table. The reason this can work is that for every object stored in the database the DB layer keeps track (through the objects table) which Java type it should map to. The SQL to allow this subclassed object to be stored is something like: INSERT INTO classes (type, super_type, db_name, table_name, java_class) VALUES(109, 10, null, null, 'org.bodington.server.resources.NewSubclass') The type being the new ID for this type. And the super_type being the persisted class that you are subclassing. Using this we could have a new resource class for every type of resource and then we can use polymophism, rather than having switch statements and constants.