Hibernate Mapping Types Example
Hello, in this tutorial we will see the different mapping types that are available in Hibernate.
1. Introduction
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:
- Oracle
- DB2
- Microsoft SQL Server
- MySQL
- PostgreSQL
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 |
org.hibernate.type.StringType | string | VARCHAR | string, java.lang.String |
org.hibernate.type.MaterializedClob | string | CLOB | materialized_clob |
org.hibernate.type.TextType | string | LONGVARCHAR | text |
org.hibernate.type.CharacterType | char, java.lang.Character | CHAR | char, java.lang.Character |
org.hibernate.type.BooleanType | boolean | BIT | boolean, java.lang.Boolean |
org.hibernate.type.NumericBooleanType | boolean | INTEGER, 0 is false, 1 is true | numeric_boolean |
org.hibernate.type.YesNoType | boolean | CHAR, ‘N’/’n’ is false, ‘Y’/’y’ is true. The uppercase value is written to the database. | yes_no |
org.hibernate.type.TrueFalseType | boolean | CHAR, ‘F’/’f’ is false, ‘T’/’t’ is true. The uppercase value is written to the database. | true_false |
org.hibernate.type.ByteType | byte, java.lang.Byte | TINYINT | byte, java.lang.Byte |
org.hibernate.type.ShortType | short, java.lang.Short | SMALLINT | short, java.lang.Short |
org.hibernate.type.IntegerTypes | int, java.lang.Integer | INTEGER | int, java.lang.Integer |
org.hibernate.type.LongType | long, java.lang.Long | BIGINT | long, java.lang.Long |
org.hibernate.type.FloatType | float, java.lang.Float | FLOAT | float, java.lang.Float |
org.hibernate.type.DoubleType | double, java.lang.Double | DOUBLE | double, java.lang.Double |
org.hibernate.type.BigIntegerType | java.math.BigInteger | NUMERIC | big_integer |
org.hibernate.type.BigDecimalType | java.math.BigDecimal | NUMERIC | big_decimal, java.math.bigDecimal |
org.hibernate.type.TimestampType | java.sql.Timestamp | TIMESTAMP | timestamp, java.sql.Timestamp |
org.hibernate.type.TimeType | java.sql.Time | TIME | time, java.sql.Time |
org.hibernate.type.DateType | java.sql.Date | DATE | date, java.sql.Date |
org.hibernate.type.CalendarType | java.util.Calendar | TIMESTAMP | calendar, java.util.Calendar |
org.hibernate.type.CalendarDateType | java.util.Calendar | DATE | calendar_date |
org.hibernate.type.CurrencyType | java.util.Currency | VARCHAR | currency, java.util.Currency |
org.hibernate.type.LocaleType | java.util.Locale | VARCHAR | locale, java.utility.locale |
org.hibernate.type.TimeZoneType | java.util.TimeZone | VARCHAR (Using the TimeZone ID) | timezone, java.util.TimeZone |
org.hibernate.type.UrlType | java.net.URL | VARCHAR | url, java.net.URL |
org.hibernate.type.ClassType | java.lang.Class | VARCHAR (Using the class name) | class, java.lang.Class |
org.hibernate.type.BlobType | java.sql.Blob | BLOB | blog, java.sql.Blob |
org.hibernate.type.ClobType | java.sql.Clob | CLOB | clob, java.sql.Clob |
org.hibernate.type.BinaryType | primitive byte[] | VARBINARY | binary, byte[] |
org.hibernate.type.MaterializedBlobType | primitive byte[] | BLOB | materized_blob |
org.hibernate.type.ImageType | primitive byte[] | LONGVARBINARY | image |
org.hibernate.type.BinaryType | java.lang.Byte[] | VARBINARY | wrapper-binary |
org.hibernate.type.CharArrayType | char[] | VARCHAR | characters, char[] |
org.hibernate.type.CharacterArrayType | java.lang.Character[] | VARCHAR | wrapper-characters, Character[] , java.lang.Character[] |
org.hibernate.type.UUIDBinaryType | java.util.UUID | BINARY | uuid-binary, java.util.UUID |
org.hibernate.type.UUIDCharType | java.util.UUID | CHAR, VARCHAR | uuid-char |
org.hibernate.type.PostgresUUIDType | java.util.UUID | PostgreSQL UUID | pg-uuid |
org.hibernate.type.SerializableType | Implementors of java.lang.Serializable | VARBINARY | Unlike the other value types, multiple instances of this type are registered. It is registered once under java.io.Serializable and registered under the specific java.io.Serializable implementation class names |
3. Conclusion
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.