org.gjt.tw.dbobjects
Class ObjectMapping

java.lang.Object
  |
  +--org.gjt.tw.dbobjects.ObjectMapping

public class ObjectMapping
extends java.lang.Object

This class provides all necessary information for the mapping of all objects of a class. For each class to be mapped an object of this class has to be created and initialized. This typically happens inside a static .init() method of that class (see examples in test package).


Field Summary
static int AUTOINCREMENT_GENERIC
          Description of the Field
static int AUTOINCREMENT_MYSQL
          Description of the Field
static int AUTOINCREMENT_NONE
          Description of the Field
 
Constructor Summary
ObjectMapping()
          ObjectMapping constructor.
 
Method Summary
 void addField(java.lang.String fieldName, java.lang.Class fieldClass, java.lang.String columnName)
          This method adds one of the object's fields to the fields that are mapped.
 void prepareSQLStatements()
          At the end of initialization this method has to be called.
 void setAutoIncrement(int autoIncrement)
          Defines which kind of auto increment should be used.
 void setKeyField(java.lang.String keyFieldName)
          Defines the field that is the key of an database entry.
 void setObjectClass(java.lang.Class newValue)
          Sets the class of the object.
 void setTableName(java.lang.String newValue)
          Sets the name of the database table in which objects are stored.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

AUTOINCREMENT_NONE

public static final int AUTOINCREMENT_NONE
Description of the Field

AUTOINCREMENT_MYSQL

public static final int AUTOINCREMENT_MYSQL
Description of the Field

AUTOINCREMENT_GENERIC

public static final int AUTOINCREMENT_GENERIC
Description of the Field
Constructor Detail

ObjectMapping

public ObjectMapping()
ObjectMapping constructor.
Method Detail

setAutoIncrement

public void setAutoIncrement(int autoIncrement)
Defines which kind of auto increment should be used. If nothing is set no auto increment is used.

It is possible to use MySQL's auto_increment feature (AUTOINCREMENT_MYSQL). If it is used the org.gjt.mm.mysql jdbc drivers must be used, because the ResultSet retrieved from the database is casted to org.gjt.mm.mysql.ResultSet to get the automatically created ID. The statement to create the table has to include auto_increment as a parameter for the key value.

If you use AUTOINCREMENT_GENERIC new keys are created automatically by querying the maximal key used so far

The AUTOINCREMENT_GENERIC feature works fine as long as only a single application (even if multi-threaded) connects to the database at once. If multiple instances try to access the same table at the same time, the first one succeeds while the second one raises an exception. (The reason for this behavior is the use of the sql statement MAX that returns the highest id used to far. Several applications might get the same value for MAX returned from the database before the first application stores the next entry.) If this happens your code should catch the exception and try to save the object again.

Moreover, if auto increment is used the key must be an int or long field.

See the examples in the test packages on how to use this feature.

Parameters:
autoIncrement - The new autoIncrement value

setKeyField

public void setKeyField(java.lang.String keyFieldName)
Defines the field that is the key of an database entry. This field is defined by its java field name.
Parameters:
keyFieldName - The new keyField value

setObjectClass

public void setObjectClass(java.lang.Class newValue)
Sets the class of the object. If you want to store an object like String, Integer, etc you have to use .class to determine the class of that object: String.class, Integer.class. To store a type like int, long, etc you have to use .TYPE: Integer.TYPE, Long.TYPE.
Parameters:
newValue - java.lang.Class

setTableName

public void setTableName(java.lang.String newValue)
Sets the name of the database table in which objects are stored.
Parameters:
newValue - java.lang.String

addField

public void addField(java.lang.String fieldName,
                     java.lang.Class fieldClass,
                     java.lang.String columnName)
              throws ObjectException
This method adds one of the object's fields to the fields that are mapped. Three statements are necessary: the name of the object's field, the class of the field (for example Integer.class, int.TYPE, java.sql.Date.class), and the corresponding column in the database.
Parameters:
fieldName - name of the field
fieldClass - class of the field
columnName - name of the corresponding database column
Throws:
ObjectException - Description of Exception

prepareSQLStatements

public void prepareSQLStatements()
                          throws IncompleteDefinitionException
At the end of initialization this method has to be called. It generates the needed SQL statements for the internal processing of storing, updating, and retrieving of the object's data.
Throws:
IncompleteDefinitionException - Description of Exception