liveBook · Manning

Data conversion

As explained in Java Persistence with Spring Data and Hibernate epub

Java-to-SQL type conversion is the transformation between Java property values and SQL-compatible database values.

Java-to-SQL data conversion turns an entity’s basic property values into database values. A JPA provider supplies standard mappings; Hibernate and Spring Data JPA with Hibernate also offer additional adapters. For example, an int maps to SQL INTEGER, a BigDecimal to NUMERIC, and a LocalDate to DATE. section 6.3.1

Table 6.1 Java primitive types that map to SQL standard types (view table figure)

Name

Java type

ANSI SQL type

integer

int, java.lang.Integer

INTEGER

long

long, java.lang.Long

BIGINT

short

short, java.lang.Short

SMALLINT

float

float, java.lang.Float

FLOAT

double

double, java.lang.Double

DOUBLE

byte

byte, java.lang.Byte

TINYINT

boolean

boolean, java.lang.Boolean

BOOLEAN

big_decimal

java.math.BigDecimal

NUMERIC

big_integer

java.math.BigInteger

NUMERIC

Compare Java primitives and numeric classes with their standard SQL types. section 6.3.1

The SQL type named by a mapping need not be the type named by a particular DBMS. JDBC partly abstracts vendor differences during INSERT and UPDATE; when Hibernate generates a schema, its configured SQL dialect translates standard types into vendor-specific column types. For an existing schema, check the dialect’s mapping. A BigDecimal defaults to NUMERIC(19, 2); use @Column precision and scale to change its generated definition. section 6.3.1

Choose a mapping that fits both the Java value and its stored representation:

  • Strings commonly use VARCHAR; Hibernate also offers character-based adapters, including boolean values stored as 'Y'/'N' or 'T'/'F'. section 6.3.1
  • Date and time values have distinct mappings: LocalDate uses DATE, LocalTime uses TIME, and Instant uses TIMESTAMP. section 6.3.1
  • Binary and large values have alternatives: byte[] maps to VARBINARY, while @Lob on byte[] or String expresses a large-object mapping. Blob and Clob properties can represent large objects directly. section 6.3.1

Select a boolean representation

If a boolean property must be stored as a single 'Y' or 'N' character, annotate it with Hibernate’s @Type(type = "yes_no") rather than relying on the built-in BOOLEAN mapping. This choice is Hibernate-specific. section 6.3.1

When built-in mappings do not fit a domain value, implement a custom converter through standard JPA extension interfaces or a lower-level adapter through Hibernate’s UserType API. Prefer a built-in mapping when its representation fits; use a custom conversion when the property needs one the built-in adapters do not provide. section 6.4

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Details built-in type mappings and selected annotations, then identifies custom JPA converters and Hibernate UserType as extension options.
Examples
Uses numeric and temporal mapping tables, @Lob entity properties, and a Hibernate 'yes_no' boolean mapping.
What you can do after
Operate/configure it with @Column precision and scale, @Lob, or a Hibernate-specific boolean adapter; identify when a custom converter is needed.
How it connects
Connects basic entity mappings with JDBC, SQL dialects, schema generation, large objects, and custom type converters.

Also explained in

Compare how 21 books explain data conversion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.