2 min read

Hibernate allows transparent persistence, which means the application is absolutely isolated from the underlying database storage format. Three players in the Hibernate scene implement this feature: Hibernate dialect, Hibernate types, and HQL. The Hibernate dialect allows us to use a range of different databases, supporting different, proprietary variants of SQL and column types. In addition, HQL allows us to query persisted objects, regardless of their relational persisted form in the database.

Hibernate types are a representation of databases SQL types, provide an abstraction of the underlying database types, and prevent the application from getting involved with the actual database column types. They allow us to develop the application without worrying about the target database and the column types that the database supports. Instead, we get involved with mapping Java types to Hibernate types. The database dialect, as part of Hibernate, is responsible for transforming Java types to SQL types, based on the target database. This gives us the flexibility to change the database to one that may support different column types or SQL without changing the application code.

Built-in types

Hibernate includes a rich and powerful range of built-in types. These types satisfy most needs of a typical application, providing a bridge between basic Java types and common SQL types. Java types mapped with these types range from basic, simple types, such as long and int, to large and complex types, such as Blob and Clob. The following table categorizes Hibernate built-in types with corresponding Java and SQL types:

Java Type

Hibernate Type Name

SQL Type

Primitives

Boolean or boolean

boolean

BIT

true_false

CHAR(1)(‘T’or’F’)

yes_no

CHAR(1)(‘Y’or’N’)

Byte or byte

byte

TINYINT

char or Character

character

CHAR

double or Double

double

DOUBLE

float or float

float

FLOAT

int or Integer

integer

INTEGER

long or Long

long

BIGINT

short or Short

short

SMALLINT

String

java.lang.String

string

VARCHAR

character

CHAR(1)

text

CLOB

Arbitrary Precision Numeric

java.math.BigDecimal

big_decimal

NUMERIC

Byte Array

byte[] or Byte[]

binary

VARBINARY

 

Time and Date

java.util.Date

date

DATE

time

TIME

timestamp

TIMESTAMP

java.util.Calendar

calendar

TIMESTAMP

calendar_date

DATE

java.sql.Date

date

DATE

java.sql.Time

time

TIME

java.sql.Timestamp

timestamp

TIMESTAMP

Localization

java.util.Locale

locale

VARCHAR

java.util.TimeZone

timezone

java.util.Currency

currency

Class Names

java.lang.Class

class

VARCHAR

Any Serializable Object

java.io.Serializable

Serializable

VARBINARY

JDBC Large Objects

java.sql.Blob

blob

BLOB

java.sql.Clob

clob

CLOB


Subscribe to the weekly Packt Hub newsletter. We'll send you this year's Skill Up Developer Skills Report.

* indicates required

LEAVE A REPLY

Please enter your comment!
Please enter your name here