[[mapping-types]]
=== Mapping types
:sourcedir: ../../../../../test/java/org/hibernate/userguide/mapping
:extrasdir: extras/types

Hibernate understands both the Java and JDBC representations of application data.
The ability to read/write this data from/to the database is the function of a Hibernate _type_.
A type, in this usage, is an implementation of the `org.hibernate.type.Type` interface.
This Hibernate type also describes various behavioral aspects of the Java type such as how to check for equality, how to clone values, etc.

.Usage of the word _type_
[NOTE]
====
The Hibernate type is neither a Java type nor a SQL data type.
It provides information about mapping a Java type to an SQL type as well as how to persist and fetch a given Java type to and from a relational database.

When you encounter the term type in discussions of Hibernate, it may refer to the Java type, the JDBC type, or the Hibernate type, depending on the context.
====

To help understand the type categorizations, let's look at a simple table and domain model that we wish to map.

[[mapping-types-basic-example]]
.A simple table and domain model
====
[source, SQL, indent=0]
----
include::{extrasdir}/mapping-types-basic-example.sql[]
----

[source, JAVA, indent=0]
----
include::{sourcedir}/basic/TypeCategoryTest.java[tags=mapping-types-basic-example]
----
====

In the broadest sense, Hibernate categorizes types into two groups:

* <<categorization-value>>
* <<categorization-entity>>

[[categorization-value]]
==== Value types

A value type is a piece of data that does not define its own lifecycle.
It is, in effect, owned by an entity, which defines its lifecycle.

Looked at another way, all the state of an entity is made up entirely of value types.
These state fields or JavaBean properties are termed __persistent attributes__.
The persistent attributes of the `Contact` class are value types.

Value types are further classified into three sub-categories:

Basic types:: in mapping the `Contact` table, all attributes except for name would be basic types. Basic types are discussed in detail in <<chapters/domain/basic_types.adoc#basic,_Basic types_>>
Embeddable types:: the `name` attribute is an example of an embeddable type, which is discussed in details in <<chapters/domain/embeddables.adoc#embeddables,_Embeddable types_>>
*Collection* types:: although not featured in the aforementioned example, collection types are also a distinct category among value types. Collection types are further discussed in <<chapters/domain/collections.adoc#collections,_Collections_>>

[[categorization-entity]]
==== Entity types

Entities, by nature of their unique identifier, exist independently of other objects whereas values do not.
Entities are domain model classes which correlate to rows in a database table, using a unique identifier.
Because of the requirement for a unique identifier, entities exist independently and define their own lifecycle.
The `Contact` class itself would be an example of an entity.

Mapping entities is discussed in detail in <<chapters/domain/entity.adoc#entity,_Entity types_>>.
