Hibernate Datatypes

  • Hibernate datatypes acts as a bridge between the java datatype and database datatype.
  • Based on the dialect, hibernate choose the correct datatype while creating the table in database.
  • Hibernate datatypes are used while creating hinernate mapping file (*.hbm.xml).
  • Hibernate's rich range of built-in types and support for components means you will rarely need to use a custom type, it is considered good practice to use custom types for non-entity classes that occur frequently in your application.

Mapping Type Java Type ANSI SQL Type
Primitive types :
integer int or java.lang.Integer INTEGER
long long or java.lang.Long BIGINT
short short or java.lang.Short SMALLEST
float float or java.lang.Float FLOAT
double double or java.lang.Double DOUBLE
big_decimal java.math.BigDecimal NUMERIC
character java.lang.String CHAR(1)
string java.lang.String VARCHAR
byte byte or java.lang.Byte TINYINT
boolean boolean or java.lang.Boolean BIT
yes/no boolean or java.lang.Boolean CHAR(1)('Y' or 'N')
true/false boolean or java.lang.Boolean CHAR(1)('T' or 'F')
Date and time types :
date java.util.Date or java.sql.Date DATE
time java.util.Date or java.sql.Time TIME
timestamp java.util.Date or java.sql.Time TIMESTAMP
calendar java.util.Calendar TIMESTAMP
calendar_date java.util.Calendar Date

Hibernate Datatypes Example

hibernate.cfg.xml file
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 

   <property name="hibernate.dialect"> 
   	org.hibernate.dialect.MySQLDialect </property>
   <property name="hibernate.connection.driver_class">
   	com.mysql.jdbc.Driver </property>

   <!-- Assume hibernatedb as the database name -->
   <property name="hibernate.connection.url"> 
   	jdbc:mysql://localhost/hibernatedb </property>
   <property name="hibernate.connection.username"> 
   	root </property>
   <property name="hibernate.connection.password">
   	root </property>
   <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
   <!-- Drop and re-create the database 
   	schema on startup -->
   <property name="hbm2ddl.auto">create</property>	

   <!-- Use when mapping with XML mapping files -->
   <!-- <mapping resource="Employee.hbm.xml"/> -->
   <!-- Names the annotated entity class -->
    <mapping class="com.tkhts.Employee"/>

package com.tkhts;
import java.util.Currency;
import java.util.Date;
import java.util.Locale;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Type;

@Entity //This annotation create the table with class 
		//name if Table annotation not used
@Table(name = "EMPLOYEE1") //This annotation create 
					//table with table name EMPLOYEE1
public class Employee {
   public boolean isActive() {
		return active;
	public void setActive(boolean active) {
		this.active = active;
@Id @GeneratedValue //This annotation create primary 
				//key and generate value automatically
   @Column(name = "id") 
   	//This annotation create column name "id"
   private int id;

   @Column(name = "first_name") //This annotation create 
   							//column name with first_name
   private String firstName;

   @Column(name = "last_name") //This annotation 
   					//create column name with last_name
   private String lastName;

   @Column(name = "salary") //This annotation 
   					//create column name with salary
   private int salary; 

   public Currency getCurrency() {
	return currency;
public void setCurrency(Currency currency) {
	this.currency = currency;
@Type(type="true_false") //This annotation define 
						//a type of boolean
   private boolean active;
   @Temporal(TemporalType.DATE) //This annotation define 
   							//how date will store in db
   private Date created;
   @Type(type="locale") //This annotation define 
   							//a type to locale
   private Locale locale;
   @Type(type="currency") //This annotation 
   					//define a type to currency 
   private Currency currency;
   public Locale getLocale() {
   	   return locale;
   public void setLocale(Locale locale) {
	   this.locale = locale;
   public Date getCreated() {
	   return created;
   public void setCreated(Date created) {
	   this.created = created;
   public int getId() {
       return id;
   public void setId( int id ) {
      this.id = id;
   public String getFirstName() {
      return firstName;
   public void setFirstName( String first_name ) {
      this.firstName = first_name;
   public String getLastName() {
      return lastName;
   public void setLastName( String last_name ) {
      this.lastName = last_name;
   public int getSalary() {
      return salary;
   public void setSalary( int salary ) {
      this.salary = salary;
ManageEmployee.java file
package com.tkhts
import java.util.Currency;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class ManageEmployee {

	private static SessionFactory factory; 
   public static void main(String[] args) {
           	  factory = new Configuration().configure()
      }catch (Throwable ex) { 
         ("Failed to create sessionFactory object." + ex);
         throw new ExceptionInInitializerError(ex); 
      ManageEmployee ME = new ManageEmployee();

      /* Add few employee records in database */
     Integer empID1 = 
     	ME.addEmployee("Zara", "Ali", 1000);
      Integer empID2 = 
      	ME.addEmployee("Daisy", "Das", 5000);
      Integer empID3 = 
      	ME.addEmployee("John", "Paul", 10000);

      /* List down all the employees */
   /* Method to CREATE an employee in the database */
   public Integer addEmployee
   	(String fname, String lname, int salary){
      Session session = factory.openSession();
      Transaction tx = null;
      Integer employeeID = null;
         tx = session.beginTransaction();
         Employee employee = new Employee();
         employee.setCreated(new Date());
         Currency usd = 
         employee.setLocale(new Locale("HI","IN"));
//         Currency currency = new Currency
//         employee.setCurrency()
         employeeID = (Integer) session.save(employee); 
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
      }finally {
      return employeeID;
Table in Mysql
Hibernate Datatype Output