This version of the manual is outdated. For the latest version, follow this link: http://www.jooq.org/doc/3.0/manual.
| The jOOQ User Manual. Multiple Pages : DSL or fluent API. Where SQL meets Java : Type casting | previous : next |
# Enforcing a specific type when you need it
jOOQ's source code generator tries to find the most accurate type mapping between your vendor-specific data types and a matching Java type. For instance, most VARCHAR, CHAR, CLOB types will map to String. Most BINARY, BYTEA, BLOB types will map to byte[]. NUMERIC types will default to java.math.BigDecimal, but can also be any of java.math.BigInteger, Long, Integer, Short, Byte, Double, Float.
Sometimes, this automatic mapping might not be what you needed, or jOOQ cannot know the type of a field (because you created it from a nested select). In those cases you would write SQL type CASTs like this:
-- Let's say, your Postgres column LAST_NAME was VARCHAR(30) -- Then you could do this: SELECT CAST(T_AUTHOR.LAST_NAME AS TEXT) FROM DUAL
in jOOQ, you can write something like that:
create.select(TAuthor.LAST_NAME.cast(PostgresDataType.TEXT));
The same thing can be achieved by casting a Field directly to String.class, as TEXT is the default data type in Postgres to map to Java's String
create.select(TAuthor.LAST_NAME.cast(String.class));
The complete CAST API in Field consists of these three methods:
public interface Field<T> {
<Z> Field<Z> cast(Field<Z> field);
<Z> Field<Z> cast(DataType<Z> type);
<Z> Field<Z> cast(Class<? extends Z> type);
}
// And additional convenience methods in the Factory:
public class Factory {
<T> Field<T> cast(Object object, Field<T> field);
<T> Field<T> cast(Object object, DataType<T> type);
<T> Field<T> cast(Object object, Class<? extends T> type);
<T> Field<T> castNull(Field<T> field);
<T> Field<T> castNull(DataType<T> type);
<T> Field<T> castNull(Class<? extends T> type);
}
| The jOOQ User Manual. Multiple Pages : DSL or fluent API. Where SQL meets Java : Type casting | previous : next |
