Hibernate Mapping Types - Mapping between SQL and Hibernate Java objects
As you already know Hibernate is an ORM tool that retrieves the data from databases in the Java data types. So, you should be well aware of the SQL data types and its corresponding Java data types. With a solid understanding of Hibernate Mapping Types you will be able to write robust Hibernate based applications.
This tutorial is a Comprehensive Guide to understand Hibernate Mapping Types, where we will see all the SQL to Java mapping types for developing Hibernate based ORM layers. You will learn to correctly map SQL types to appropriate Java types in your Hibernate based ORM application.
In this post we are going to list the mapping between SQL Types to Hibernate (Java) Types. Hibernate Types is the actual Java type that is used in the Entity class for mapping table data to Java objects.
Importance of Hibernate ORM
Hibernate is a very popular and widely used Object-Relational Mapping (ORM) tool in Java programming language which is used to construct the data access layer for Java applications. This framework is feature rich and very efficient, which makes it an ideal framework for data based driven application development. Hibernate is database independent and supports all the major relational databases in the market. As a Java developer you must learn the Hibernate framework and master it by experimenting with the sample projects.
Understanding Hibernate Mapping Types
In Hibernate the Mapping Types are used to define the mapping between database field type (data type) to the equivalent Java types while writing the entity class. Based on this mapping information Hibernate converts the SQL data type to Java data types and builds entity objects. The command Java types used are String, int, Integer, double, Double, Date etc.
We can categorize these mapping types into following:
- Basic Types
- Temporal Types
- Boolean Type
- Binary Types
- Enumerations
- Custom Mapping Types
Lets see all these mapping types in detail.
Hibernate Mapping Types
1. Basic Types
- StringType: SQL VARCHAR is mapped to String data type.
- IntegerType, LongType, DoubleType: SQL numeric types are mapped to respective data types such as Long, Integer and Double.
2. Temporal Types
- DateType, TimeType, TimestampType: Database fields types SQL DATE, TIME, and TIMESTAMP are mapped to Java java.util.Date.
3. Boolean Type
- BooleanType: In we have to handle the Boolean data then then Java boolean data type is used and it is mapped to a database CHAR(1) or INTEGER, depending on the database.
4. Binary Types
- BinaryType: If we have Binary data type such as BLOB in the database then it is mapped to Java byte array.
5. Enumerations
- EnumType: If we have Java Enum type of data into database then it is mapped to a database VARCHAR or INTEGER.
6. Custom Mapping Types
Hibernate provides a wide range of built-in mapping types, but even if it is not meeting your requirements then you can go for Custom Mapping Types. The Custom Mapping Types can be developed by implementing the org.hibernate.usertype.UserType interface.
How to Use Mapping Types
There are two ways you can specify mapping in your Hibernate applications. These are:
- XML Mapping
- Annotation Mapping
Primitive Type Mapping in Hibernate
Here we will fist see the primitive Java types mapped to the SQL data types.
Sr No. | Mapping Type | Java Type | ANSI SQL Type | Description |
---|---|---|---|---|
1 | Integer | int or java.lang.Integer | INTEGER | Database INTEGER is mapped to int or Integer Java Type |
2 | long | long or java.lang.Long | BIGINT | SQL BIGINT is mapped to long or java.lang.Long |
3 | short | short or java.lang.Short | SMALLINT | SQL SMALLINT is mapped to short or java.lang.Short |
4 | float | float or java.lang.Float | FLOAT | float or java.lang.Float |
5 | double | double or java.lang.Double | Double | SQL Double is mapped to double or java.lang.Double |
6 | big_decimal | java.math.BigDecimal | NUMERIC | SQL NUMERIC is mapped to java.math.BigDecimal |
7 | character | java.lang.String | CHAR(1) | CHAR is mapped to Java java.lang.String |
8 | string | java.lang.String | VARCHAR | VARCHAR is mapped to java.lang.String |
9 | byte | byte or java.lang.Byte | TINYINT | TINYINT in SQL is mapped to byte or java.lang.Byte |
10 | boolean | boolean or java.lang.Booleon | BIT | SQL BIT is mapped to boolean or java.lang.Booleon in Java |
11 | yes/no | boolean or java.lang.Booleon | CHAR(1) ('Y' or 'N') | SQL CHAR is mapped to boolean or java.lang.Booleon in Java |
12 | true/false | boolean or java.lang.Booleon | CHAR(1) ('T' or 'F') | SQL CHAR is mapped to boolean or java.lang.Booleon in Java Entity class |
Date and Time Types
Now we will see how SQL Date and Time SQL types are mapped to Hibernate Entity class.
Sr. No. | Mapping Type | Java Type | ANSI SQL Type | Description |
---|---|---|---|---|
1 | date | java.util.Date or java.sql.Date | DATE | DATE is mapped to java.util.Date or java.sql.Date |
2 | DATE | java.util.Date or java.sql.Time | TIME | TIME of SQL is mapped to java.util.Date or java.sql.Time |
3 | timestamp | java.util.Date or java.sql.Timestamp | TIMESTAMP | If you have TIMESTAMP in SQL then you will map it to java.util.Date or java.sql.Timestamp Java classes. |
4 | calendar | java.util.Calendar | TIMESTAMP | SQL TIMESTAMP is mapped to java.util.Calendar |
5 | calendar_date | java.util.Calendar | DATE | DATE is mapped to java.util.Calendar |
Binary and Large Object Types
Database table column types Binary and Large Object Types mappings are given below:
Sr . No. | Mapping Type | Java Type | ANSI SQL Type | Description |
---|---|---|---|---|
1 | binary | byte[] | VARBINARY (or BLOB) | SQL VARBINARY (or BLOB) is mapped to Java byte[] types. |
2 | text | java.lang.String | CLOB | SQL CLOB should be mapped to java.lang.String |
3 | serializable | any Java class that implements java.io.Serializable | VARBINARY (or BLOB) | The table column type VARBINARY (or BLOB) is mapped to any Java class that implements java.io.Serializable. |
4 | clob | java.sql.Clob | CLOB | SQL CLOB should be mapped to java.sql.Clob |
5 | blob | java.sql.Blob | BLOB | SQL BLOB will be mapped to Java java.sql.Blob |
JDK-related Types
Now we will see the JDK related mapping types.
Sr. No. | Mapping Type | Java Type | ANSI SQL Type | Description |
---|---|---|---|---|
1 | class | java.lang.Class | VARCHAR | SQL VARCHAR should be mapped to java.lang.Class |
2 | locale | java.util.Locale | VARCHAR | VARCHAR can also be mapped to java.util.Locale |
3 | timezone | java.util.TimeZone | VARCHAR | VARCHAR column field can be mapped to java.util.TimeZone if the data is TimeZone data |
4 | currency | java.util.Currency | VARCHAR | If you have currency data in table then you can map it to java.util.Currency |
5 | blob | java.sql.Blob | BLOB | SQL BLOB should be mapped to java.sql.Blob |
Summary
Hibernate comes with the predefined mapping types that you can use in your
hibernate application to map your database column types to the Java Types.
Hibernate also provides support for creating user-defined mapping types.
In this tutorial we have learned Hibernate Mapping Types in detail. You can
check more tutorials at: