Hello, in this tutorial we will see the different mapping types that are available in Hibernate.
Hibernate is a framework that simplifies the development of Java application to interact with the database. It is an open source, lightweight, ORM (Object Relational Mapping) tool.
1.1 What is Hibernate?
- Hibernate is the Opensource light weight ORM (Object Relational Mapping) based framework to communicate with the database. ORM is a programming technique that maps the object to the data stored in the database.
- Hibernate framework makes the development of Java application interact with the database in a simple manner.
- Hibernate handles mapping of Java classes to database tables (i.e. Java data types and SQL data types).
- Hibernate also provides data query and retrieval facilities.
- Hibernate is purely used for data persistence (i.e. to store/retrieve data from the database).
- Hibernate framework internally uses the JDBC API to interact with the database. It hides internal JDBC implementations from end users.
1.2 Hibernate Advantages
- Hibernate framework is Opensource and lightweight.
- The performance of hibernate framework is fast because of caching mechanism which it uses internally.
- Hibernate framework provides the facility to create the tables of the database automatically.
- With the help of HQL (Hibernate Query Language), SQL queries generated are independent of databases.
- Provides query facilities to fetch data from multiple databases.
- Provides Transaction management and automatic key generations.
- Provides various APIs for storing and retrieving Java objects directly to and from the database.
- If there is a change in the database, then developers only need to change XML file properties.
1.3 Hibernate Supported Databases
Hibernate frameworks support various databases. Below are few names of databases that it supports:
- Microsoft SQL Server
and much more.
1.4 Download and Install Hibernate
You can read this tutorial in order to download and install Hibernate in the Eclipse IDE.
2. Hibernate Mapping Types Example
- In the Hibernate mapping document, we have seen that Java data types are mapped to RDBMS data types
- The types declared and used in the mapping files are not exactly Java data types. They are also not either SQL database types
- These data types are called as Hibernate mapping types. This gets translated from Java to SQL data types and vice versa
Below is the complete list of the data type mapping:
|Hibernate Type||Database Type||JDBC Type||Type Registry|
|char, ||CHAR||char, |
|boolean||INTEGER, 0 is false, 1 is true||numeric_boolean|
|boolean||CHAR, ‘N’/’n’ is false, ‘Y’/’y’ is true. The uppercase value is written to the database.||yes_no|
|boolean||CHAR, ‘F’/’f’ is false, ‘T’/’t’ is true. The uppercase value is written to the database.||true_false|
|byte, ||TINYINT||byte, |
|short, ||SMALLINT||short, |
|int, ||INTEGER||int, |
|long, ||BIGINT||long, |
|float, ||FLOAT||float, |
|double, ||DOUBLE||double, |
|VARCHAR (Using the TimeZone ID)||timezone, |
|VARCHAR (Using the class name)||class, |
|Implementors of ||VARBINARY||Unlike the other value types, multiple instances of this type are registered. It is registered once under |
In this article, we discussed Hibernate types, which define the mapping of each Java type to an SQL type. It is the responsibility of the Hibernate dialect and the JDBC driver to convert the Java types to the actual target SQL types. This means a Java type may be transformed to different SQL types when different databases are used.
Although Hibernate provides a rich set of data types, called built-in types, some situations require the definition of a new type. Such situation occurs when you want to change Hibernate default behavior for mapping a Java type to an SQL type. Another situation is when you want to split up a class property to a set of table columns or merge a set of properties to a table column.
Built-in types include primitive, string, byte array, time, localization, serializable, and JDBC large types.