The following table illustrates the mapping of Java object attribute types to SQL database field types as they are included in PriDE by default. The row 'Java function type' determines the function type being used for the specified attribute type to access results from a ResultSet or to pass inputs to a PreparedStatement. The 'SQL type' specifies the actual SQL row types, the attributes can usually be mapped to. Not all SQL databases support all the mentioned type identifiers and it may also depend on the JDBC driver's capabilities which mappings are supported. Primitive attribute types should of course only be used, if the corresponding row must not be NULL. Otherwise an exception will be thrown at runtime when attempting to process NULL values.
Java attribute type | Java function type | SQL type |
String | String | VARCHAR, CHAR |
java.util.Date | java.sql.Timestamp | DATETIME, TIMESTAMP, TIME |
java.sql.Date | java.sql.Date | DATE |
java.sql.Timestamp | java.sql.Timestamp | DATETIME, TIMESTAMP, TIME |
int | Integer | INTEGER |
Integer | Integer | INTEGER |
float | Float | DECIMAL, REAL |
Float | Float | DECIMAL, REAL |
double | Double | DECIMAL, REAL |
Double | Double | DECIMAL, REAL |
Any enum | String | VARCHAR |
boolean | Boolean | BOOLEAN, INTEGER, SMALLINT, TINYINT, CHAR |
Boolean | Boolean | BOOLEAN, INTEGER, SMALLINT, TINYINT, CHAR |
BigDecimal | BigDecimal | DECIMAL, NUMBER |
long | Long | INTEGER, DECIMAL, NUMBER, BIGINT |
Long | Long | INTEGER, DECIMAL, NUMBER, BIGINT |
short | Short | INTEGER, SMALLINT, TINYINT, DECIMAL |
Short | Short | INTEGER, SMALLINT, TINYINT, DECIMAL |
byte | Byte | TINYINT |
Byte | Byte | TINYINT |
byte[] | byte[] | BLOB, LONGVARBINARY, VARBINARY |
Blob | Blob | BLOB, LONGVARBINARY, VARBINARY |
Clob | Clob | CLOB, LONGVARCHAR |
Map<String, String> |
Object |
HSTORE, Postgres only |
Any [] |
Object |
ARRAY, Postgres only |
As it can be seen from the list above, java.util.Date
is
treated
like a time stamp, assuming that the time information is relevant.
Date
attributes play a special role among the ordinary SQL types as the
capabilities
and formatting rules vary significantly between different SQL
databases.
PriDE's base class AbstractResourceAccessor
provides different formatting already based on the properties pride.dbtype,
pride.format.date and pride.format.time.
The database server's system time can be referred to by passing
the
return value of Database.getSystime()
to the attribute of interest. The function does not actually
return the
current database system time but a java.util.Date being
used
as
an indicator which is later replaced by the database' specific
constants
like CURRENT_TIMESTAMP or SYSDATE.
BLOBS and CLOBS can currently only be written to the database
using
prepared statements and are known to be incompatibly to use for
different
DB vendors (especially Oracle).
Enum types are generically mapped to strings, using Enum.name() rather than
the toString() which
ensures safe
forth and back mapping. Arrays and Key-Value-Maps can only be
mapped for Postgres as this database provides corresponding
dynamic column types.
If there must a different attribute type be mapped to the database, it is recommended simply to provide additional get and set methods for this attribute, performing a conversion to one of the attribute types listed above and being referred to in the object type's RecordDescriptor. Adding access methods only for database interaction is of course a slight loss of transparence, but it is just simple. As an alternative the mappings can be extended in general by the following steps:
Home | Introduction | Javadoc |