Package org.hibernate.type

A Hibernate Type is a strategy for mapping a Java property type to a JDBC type or types. Every persistent attribute of an entity or embeddable object has a Type, even attributes which represent associations or hold references to embedded objects.

On the other hand, in modern Hibernate, Type itself is of receding importance to application developers, though it remains a very important internal abstraction.

Basic types

For basic types, we prefer to model the type mapping in terms the combination of:

A JdbcType is able to read and write a single Java type to one, or sometimes several, JDBC types.

A JavaType is able to determine if an attribute of a given Java type is dirty, and then convert it to one of several other equivalent Java representations at the request of its partner JdbcType.

For example, if an entity attribute of Java type BigInteger is mapped to the JDBC type Types.VARCHAR, the VarcharJdbcType will ask its BigIntegerJavaType to convert instances of BigInteger to and from String when writing to and reading from JDBC statements and result sets.

An important point is that the set of available JavaTypes and of available JdbcTypes is not fixed—a TypeConfiguration is customizable during the bootstrap process.

This approach provides quite some flexibility in allowing a given Java type to map to a range of JDBC types. However, when the built-in conversions don't handle a particular mapping, a converter may assist in the conversion process. For example, a JPA AttributeConverter might be provided.

A JavaType comes with a built-in MutabilityPlan, but this may be overridden when types are composed.

See org.hibernate.annotations for information on how to influence basic type mappings using annotations.

Custom types

The package org.hibernate.usertype provides a way for application developers to define new types without being exposed to the full complexity of the Type framework defined in this package.
  • A UserType may be used to define single-column type mappings, and thus competes with the "compositional" approach to basic type mappings described above.
  • On the other hand, a CompositeUserType defines a way to handle multi-column type mappings, and is a much more flexible form of Embeddable object mapping.

Built-in converters for boolean mappings

In older versions of Hibernate there were dedicated Types mapping Java boolean to char(1) or integer database columns. These have now been replaced by the converters:

These converters may be applied, as usual, using the JPA-defined Converter annotation.

See Also:
Type, SqlTypes