Skip to main content

Type Support

The table recommends Java property types for common columns. Use wrapper types for nullable fields. See Java/JDBC types and enum mapping.

Type Mappings

Database field typeJava typeDescription
TINYINTByteCovers the range of this signed integer type.
SMALLINTShortCovers the range of this signed integer type.
INTIntegerCovers the range of this signed integer type.
BIGINTLongCovers the range of this signed integer type.
REALFloatApproximate values; not for amounts requiring exact decimal arithmetic.
DOUBLEDoubleApproximate values; not for amounts requiring exact decimal arithmetic.
DECIMAL(10, 2)BigDecimalRetains decimal values; writes are constrained by column precision and scale.
NUMERIC(20)BigInteger / BigDecimalUse BigInteger for integral business values, or BigDecimal for a common decimal model.
BITBooleanUse Boolean semantics, not a general integer property.
BOOLEANBooleanUse Boolean semantics, not a general integer property.
CHAR(1)StringText content; the column definition constrains length.
VARCHAR(255)StringText content; the column definition constrains length.
NVARCHAR(255)StringText content; the column definition constrains length.
VARBINARY(1000)byte[]Binary content without character encoding conversion.
BLOBbyte[]Binary content without character encoding conversion.
DATEjava.sql.DateDate only; not for preserving a complete timestamp.
TIMEjava.sql.TimeTime only; text storage requires a parseable time format.
TIMESTAMPjava.sql.TimestampPreserves date and time, subject to column precision.
VARCHAR(2000)Map / List / BeanWhen storing JSON text, see JSON Field Mapping.
INT ARRAYInteger[]Array elements match the database element type; configure the handler explicitly when needed.
VARCHAR ARRAYString[]Array elements match the database element type; configure the handler explicitly when needed.
REAL ARRAYFloat[]Array elements match the database element type; configure the handler explicitly when needed.

Array Types

CREATE TABLE array_example (id INTEGER PRIMARY KEY, values_col INTEGER ARRAY);
import java.sql.Types;
import net.hasor.dbvisitor.types.SqlArg;
import net.hasor.dbvisitor.types.handler.array.ArrayTypeHandler;

Integer[] values = { 10, 20, 30 };
jdbcTemplate.executeUpdate("INSERT INTO array_example (id, values_col) VALUES (?, ?)",
new Object[] { 1, new SqlArg(values, Types.ARRAY, new ArrayTypeHandler()) });
Integer[] loaded = jdbcTemplate.queryForObject(
"SELECT values_col FROM array_example WHERE id = ?",
new Object[] { 1 }, Integer[].class);

Time Types: Historical Dates

With H2 JDBC 2.2.224 and the JVM time zone set to Asia/Shanghai, reading 1900-01-01 as java.sql.Date can produce 1899-12-31. The stored date is unchanged; the shift occurs during JDBC date conversion.

Use a typed JDBC read for these historical dates to avoid conversion through java.sql.Date:

import java.time.LocalDate;

LocalDate date = jdbcTemplate.queryForObject(
"SELECT DATE '1900-01-01'",
(rs, rowNum) -> rs.getObject(1, LocalDate.class));
// date is 1900-01-01

Limits

  • TIMESTAMP does not preserve a time zone. Precision follows the column definition and the value returned by the JDBC driver.