Providing a Complete Data Persistence Solution
(Page 1 of 4 )
Have you ever wondered how to map your Java-based data to a relational database? This article explains how to do just that, with the help of JSQLMapper.
Mapping your Java-based data to a relational database, such as SQL Server 2000, presents many problems difficult to resolve. For example, the SQL concept of a variable width, single-byte character array is the VARCHAR data type. The Java programming language does not contain an equivalent data type. The closest Java type is the String class.
Most database engines internally support their own data types and convert them to a SQL data type. A perfect example is the way Oracle maps its numeric types to SQL’s NUMERIC type. Fortunately, the JDBC specification allows you to call the ResultSet.getLong() method to retrieve numbers as Java Longs. The following table displays the JDBC Specification SQL to Java datatype mappings.
Table 1 JDBC Specification SQL to Java datatype mappings
| SQL Type | Java Type | SQL Type | Java Type |
| BIT | boolean | LONGVARCHAR | java.lang.String |
| TINYINT | byte | DATE | java.sql.Date |
| SMALLLINT | Short | TIME | java.sql.Time |
| INTEGER | Int | TIMESTAMP | java.sql.Timestamp |
| BIGINT | Long | BINARY | byte |
| REAL | Float | VARBINARY | byte |
| FLOAT | Double | LONGVARBINARY | byte |
| DOUBLE | Double | BLOB | java.sql.Clob |
| DECIMAL | java.math.BigDecimal | CLOB | java.sql.Array |
| NUMERIC | java.math.BigDecimal | ARRAY | java.sql.String |
| CHAR | java.lang.String | REF | java.sql.Ref |
| VARCHAR | java.lang.string | STRUCT | java.sql.Struct |
Object-to-Relational mapping is complex for a number of reasons:
- A mismatch exists between procedural Java and declarative SQL.
- Objects can have one-to-many and many-to-many associations with other objects. Unfortunately, relational schema normalization doesn’t allow a column to have multiple values.
- Relational schemas do not support inheritance.
- Object models do not support transaction semantics.
How do you solve these complexities? Fortunately, the JDBC APIs
come to the rescue by assisting in mapping data from the object
model to a relational database.
Next: A Mapping Architectural Overview >>
More .NET Articles
More By Dwight Peltzer