liveBook · Manning

Hibernate type system

As explained in Java Persistence with Spring Data and Hibernate epub

The Hibernate type system is the framework subsystem that selects and performs mappings between Java property types and SQL or vendor-specific database types.

The Hibernate type system maps Java property types to SQL types. The configured SQL dialect determines the database-specific column type used for schema generation; for example, H2Dialect maps ANSI NUMERIC to DECIMAL. section 6.3.1

Mapping parameters influence the generated schema. A BigDecimal defaults to NUMERIC(19, 2); use @Column precision and scale to change it. A String defaults to VARCHAR(255), while @Column(length = ...) or Bean Validation’s @Length can affect type selection. For nationalized character types, use @Nationalized. section 6.3.1

String length and MySQL

With the MySQL dialect, a declared length up to 65,535 generates VARCHAR(length); a length up to 16,777,215 generates MEDIUMTEXT, and greater lengths use LONGTEXT. Extending the dialect can customize this selection. section 6.3.1

Boolean stored as a character

Annotate a boolean property with @org.hibernate.annotations.Type(type = "yes_no") to use the built-in CHAR(1) representation, with 'Y' or 'N' values. 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 built-in numeric and boolean Java types with their ANSI SQL mappings. section 6.3.1

Table 6.2 Adapters for character and string values (view table figure)

Name

Java type

ANSI SQL type

string

java.lang.String

VARCHAR

character

char[], Character[], java.lang.String

CHAR

yes_no

boolean, java.lang.Boolean

CHAR(1), 'Y' or 'N'

true_false

boolean, java.lang.Boolean

CHAR(1), 'T' or 'F'

class

java.lang.Class

VARCHAR

locale

java.util.Locale

VARCHAR

timezone

java.util.TimeZone

VARCHAR

currency

java.util.Currency

VARCHAR

Compare string and character adapters, including boolean representations. section 6.3.1

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains built-in mappings and schema-selection settings through dialect and annotation examples.
Examples
Illustrates H2 NUMERIC-to-DECIMAL mapping, MySQL string-length thresholds, and a yes_no boolean adapter.
What you can do after
Apply it via Hibernate annotations and dialect configuration to control property mappings and generated column types.
How it connects
Connects SQL dialects, schema generation, Bean Validation, and nationalized character types.

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.