Hibernate.orgCommunity Documentation
As an Object/Relational Mapping solution, Hibernate deals with both the Java and JDBC representations of
application data. An online catalog application, for example, most likely has Product
object with a number of attributes such as a sku
, name
, etc. For these
individual attributes, Hibernate must be able to read the values out of the database and write them back. This
'marshalling' is the function of a Hibernate type, which is an implementation of the
org.hibernate.type.Type
interface. In addition, a
Hibernate type describes various aspects of behavior of the Java type such as "how is
equality checked?" or "how are values cloned?".
A Hibernate type is neither a Java type nor a SQL datatype; it provides a information about both.
When you encounter the term type in regards to Hibernate be aware that usage might refer to the Java type, the SQL/JDBC type or the Hibernate type.
Hibernate categorizes types into two high-level groups: value types (see Section 6.1, “Value types”) and entity types (see Section 6.2, “Entity types”).
The main distinguishing characteristic of a value type is the fact that they do not define their own lifecycle. We say that they are "owned" by something else (specifically an entity, as we will see later) which defines their lifecycle. Value types are further classified into 3 sub-categories: basic types (see Section 6.1.1, “Basic value types”), composite types (see Section 6.1.2, “Composite types”) amd collection types (see Section 6.1.3, “Collection types”).
org.hibernate.type.StringType
Registered under string
and java.lang.String
in the type registry (see Section 6.5, “Type registry”).
org.hibernate.type.MaterializedClob
Maps a string to a JDBC CLOB type
Registered under materialized_clob
in the type registry (see
Section 6.5, “Type registry”).
org.hibernate.type.TextType
Maps a string to a JDBC LONGVARCHAR type
Registered under text
in the type registry (see
Section 6.5, “Type registry”).
org.hibernate.type.BooleanType
Maps a boolean to a JDBC BIT type
Registered under boolean
and java.lang.Boolean
in
the type registry (see Section 6.5, “Type registry”).
org.hibernate.type.NumericBooleanType
Maps a boolean to a JDBC INTEGER type as 0 = false, 1 = true
Registered under numeric_boolean
in the type registry (see
Section 6.5, “Type registry”).
org.hibernate.type.YesNoType
Maps a boolean to a JDBC CHAR type as ('N' | 'n') = false, ( 'Y' | 'y' ) = true
Registered under yes_no
in the type registry (see
Section 6.5, “Type registry”).
org.hibernate.type.TrueFalseType
Maps a boolean to a JDBC CHAR type as ('F' | 'f') = false, ( 'T' | 't' ) = true
Registered under true_false
in the type registry (see
Section 6.5, “Type registry”).
org.hibernate.type.CalendarType
Maps a java.util.Calendar
to a JDBC TIMESTAMP
Registered under calendar
, java.util.Calendar
and
java.util.GregorianCalendar
in the type registry (see
Section 6.5, “Type registry”).
org.hibernate.type.CalendarDateType
Maps a java.util.Calendar
to a JDBC DATE
Registered under calendar_date
in the type registry (see
Section 6.5, “Type registry”).
org.hibernate.type.BinaryType
Maps a primitive byte[] to a JDBC VARBINARY
Registered under binary
and byte[]
in the
type registry (see Section 6.5, “Type registry”).
org.hibernate.type.MaterializedBlobType
Maps a primitive byte[] to a JDBC BLOB
Registered under materialized_blob
in the type registry (see
Section 6.5, “Type registry”).
org.hibernate.type.ImageType
Maps a primitive byte[] to a JDBC LONGVARBINARY
Registered under image
in the type registry (see
Section 6.5, “Type registry”).
org.hibernate.type.UUIDBinaryType
Maps a java.util.UUID to a JDBC BINARY
Registered under uuid-binary
and java.util.UUID
in the type registry (see Section 6.5, “Type registry”).
org.hibernate.type.UUIDCharType
Maps a java.util.UUID to a JDBC CHAR (though VARCHAR is fine too for existing schemas)
Registered under uuid-char
in the type registry (see
Section 6.5, “Type registry”).
org.hibernate.type.PostgresUUIDType
Maps a java.util.UUID to the PostgreSQL UUID data type (through
Types#OTHER
which is how the PostgreSQL JDBC driver defines it).
Registered under pg-uuid
in the type registry (see
Section 6.5, “Type registry”).
The definition of entities is covered in detail in Chapter 4, Persistent Classes. For the purpose of
this discussion, it is enough to say that entities are (generally application-specific) classes which
correlate to rows in a table. Specifically they correlate to the row by means of a unique identifier.
Because of this unique identifier, entities exist independently and define their own lifecycle. As an example,
when we delete a Membership
, both the User
and
Group
entities remain.
This notion of entity independence can be modified by the application developer using the concept of cascades. Cascades allow certain operations to continue (or "cascade") across an association from one entity to another. Cascades are covered in detail in Chapter 8, Association Mappings.
The first approach is to directly implement the org.hibernate.type.Type
interface (or one of its derivatives). Probably, you will be more interested in the more specific
org.hibernate.type.BasicType
contract which would allow registration of
the type (see Section 6.5, “Type registry”). The benefit of this registration is that whenever
the metadata for a particular property does not specify the Hibernate type to use, Hibernate will
consult the registry for the exposed property type. In our example, the property type would be
Money
, which is the key we would use to register our type in the registry:
It is important that we registered the type before adding mappings.
There is not much difference between the org.hibernate.type.Type
example
and the org.hibernate.usertype.UserType
example, but that is only because
of the snippets shown. If you choose the org.hibernate.type.Type
approach
there are quite a few more methods you would need to implement as compared to the
org.hibernate.usertype.UserType
.
Internally Hibernate uses a registry of basic types (see Section 6.1.1, “Basic value types”) when
it needs to resolve the specific org.hibernate.type.Type
to use in certain
situations. It also provides a way for applications to add extra basic type registrations as well as
override the standard basic type registrations.
To register a new type or to override an existing type registration, applications would make use of the
registerTypeOverride
method of the org.hibernate.cfg.Configuration
class when bootstrapping Hibernate. For example, lets say you want Hibernate to use your custom
SuperDuperStringType
; during bootstrap you would call:
The argument to registerTypeOverride
is a org.hibernate.type.BasicType
which is a specialization of the org.hibernate.type.Type
we saw before. It
adds a single method:
Example 6.5. Snippet from BasicType.java
/**
* Get the names under which this type should be registered in the type registry.
*
* @return The keys under which to register this type.
*/
public String[] getRegistrationKeys();
One approach is to use inheritance (SuperDuperStringType
extends
org.hibernate.type.StringType
); another is to use delegation.
Copyright © 2004 Red Hat, Inc.