hibernate

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:

  1. Oracle
  2. DB2
  3. Microsoft SQL Server
  4. MySQL
  5. 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 TypeDatabase TypeJDBC TypeType Registry
org.hibernate.type.StringTypestringVARCHARstring, java.lang.String
org.hibernate.type.MaterializedClobstringCLOBmaterialized_clob
org.hibernate.type.TextTypestringLONGVARCHARtext
org.hibernate.type.CharacterTypechar, java.lang.CharacterCHARchar, java.lang.Character
org.hibernate.type.BooleanTypebooleanBITboolean, java.lang.Boolean
org.hibernate.type.NumericBooleanTypebooleanINTEGER, 0 is false, 1 is truenumeric_boolean
org.hibernate.type.YesNoTypebooleanCHAR, ‘N’/’n’ is false, ‘Y’/’y’ is true. The uppercase value is written to the database.yes_no
org.hibernate.type.TrueFalseTypebooleanCHAR, ‘F’/’f’ is false, ‘T’/’t’ is true. The uppercase value is written to the database.true_false
org.hibernate.type.ByteTypebyte, java.lang.ByteTINYINTbyte, java.lang.Byte
org.hibernate.type.ShortTypeshort, java.lang.ShortSMALLINTshort, java.lang.Short
org.hibernate.type.IntegerTypesint, java.lang.IntegerINTEGERint, java.lang.Integer
org.hibernate.type.LongTypelong, java.lang.LongBIGINTlong, java.lang.Long
org.hibernate.type.FloatTypefloat, java.lang.FloatFLOATfloat, java.lang.Float
org.hibernate.type.DoubleTypedouble, java.lang.DoubleDOUBLEdouble, java.lang.Double
org.hibernate.type.BigIntegerTypejava.math.BigIntegerNUMERICbig_integer
org.hibernate.type.BigDecimalTypejava.math.BigDecimalNUMERICbig_decimal, java.math.bigDecimal
org.hibernate.type.TimestampTypejava.sql.TimestampTIMESTAMPtimestamp, java.sql.Timestamp
org.hibernate.type.TimeTypejava.sql.TimeTIMEtime, java.sql.Time
org.hibernate.type.DateTypejava.sql.DateDATEdate, java.sql.Date
org.hibernate.type.CalendarTypejava.util.CalendarTIMESTAMPcalendar, java.util.Calendar
org.hibernate.type.CalendarDateTypejava.util.CalendarDATEcalendar_date
org.hibernate.type.CurrencyTypejava.util.CurrencyVARCHARcurrency, java.util.Currency
org.hibernate.type.LocaleTypejava.util.LocaleVARCHARlocale, java.utility.locale
org.hibernate.type.TimeZoneTypejava.util.TimeZoneVARCHAR (Using the TimeZone ID)timezone, java.util.TimeZone
org.hibernate.type.UrlTypejava.net.URLVARCHARurl, java.net.URL
org.hibernate.type.ClassTypejava.lang.ClassVARCHAR (Using the class name)class, java.lang.Class
org.hibernate.type.BlobTypejava.sql.BlobBLOBblog, java.sql.Blob
org.hibernate.type.ClobTypejava.sql.ClobCLOBclob, java.sql.Clob
org.hibernate.type.BinaryTypeprimitive byte[]VARBINARYbinary, byte[]
org.hibernate.type.MaterializedBlobTypeprimitive byte[]BLOBmaterized_blob
org.hibernate.type.ImageTypeprimitive byte[]LONGVARBINARYimage
org.hibernate.type.BinaryTypejava.lang.Byte[]VARBINARYwrapper-binary
org.hibernate.type.CharArrayTypechar[]VARCHARcharacters, char[]
org.hibernate.type.CharacterArrayTypejava.lang.Character[]VARCHARwrapper-characters, Character[], java.lang.Character[]
org.hibernate.type.UUIDBinaryTypejava.util.UUIDBINARYuuid-binary, java.util.UUID
org.hibernate.type.UUIDCharTypejava.util.UUIDCHAR, VARCHARuuid-char
org.hibernate.type.PostgresUUIDTypejava.util.UUIDPostgreSQL UUIDpg-uuid
org.hibernate.type.SerializableTypeImplementors of java.lang.SerializableVARBINARYUnlike 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.

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button