Learnitweb

Mapping Types in Hibernate

1. Introduction

Hibernate is an ORM solution. It maps object model to a relational data model (and vice-versa). Usually the object model is a Plain Old Java Object (POJO), but it is not a mandatory requirement to use Hibernate. Hibernate can easily work with other objects like java.util.Map and can map to the relational model.

To make this mapping of object model to relational model happen, Hibernate has its own type. This type is an implementation of org.hibernate.Type interface. A Hibernate type is not a Java type nor a SQL type. A Hibernate type provides information about the mapping of Java type to a SQL type. It also provides information about saving and retrieving a Java type to and from a relational database.

2. org.hibernate.Type interface

This interface defines a mapping from a Java type to an JDBC type. If you want to create your own custom type, then you need to implement org.hibernate.Type interface. All classes which implement this interface should be immutable and thread-safe. Some classes which implement this interface are ArrayType, BagType, BigIntegerType, BlobType, BooleanType, CalendarType, CharacterArrayType, CollectionType, CurrencyType, DateType, DoubleType, EntityType, ManyToOneType, MapType, OneToOneType, SetType, StringType and many more.

3. Category of Hibernate types

There are two categories of Hibernate types:

  • Value types
  • Entity types

3.1. Value types

A value type is owned by an entity and does not define its own lifecycle. Its lifecycle is defined by the entity. All fields of an entity which represent state of the entity and to be persisted in a relational database are called persistent attributes of the entity. In other words we can say that an entity is made up of value types. There are three categories of value types:

  • Basic types: Basic types usually map a single Java type to a single database column. Some in-built basic types are StringType, TextType, CharacterType, IntegerType, LongType.
  • Embeddable types: An embeddable type is a composition of properties. For example, Address is a composition of country, state, city, street.
  • Collection types: A collection type can contain other Hibernate type.

3.2. Entity types

Entity type represents an entity or domain model which is mapped to a row in database table. Each entity has a unique identifier using which it can be distinguished from other entity objects. Each entity defines its own lifecycle.

4. Example

In this example, id, name are basic value types and address is the embeddable type. Employee is the entity type and it contains value types of id, name and address.

@Entity(name = "Employee")
public class Employee {

	@Id
	private Integer id;

	private String name;

	private Address address;

	//Getters and setters are omitted for brevity
}

@Embeddable
public class Address {

	private String Country;

	private String State;

	private String street;

	//Getters and setters are omitted for brevity
}

5. @Basic annotation

The @Basic (javax.persistence.Basic) annotation is used to denote a basic type. This annotation is assumed by default so it is not mandatory to use this annotation with a basic type. So the following are same:

@Entity(name = "Employee")
public class Employee {
	@Id
	@Basic
	private Integer id;

	@Basic
	private String name;
} 
@Entity(name = "Employee")
public class Employee {
	@Id
	private Integer id;

	private String name;
} 

The @Basic annotation defines 2 attributes:

  • optional: This attribute defines whether this attribute allows nulls. The default value is true.
  • fetch: This attribute defines whether this attribute should be fetched eagerly or lazily. The default value is EAGER.

6. BasicTypeRegistry

The org.hibernate.type.BasicTypeRegistry service in Hibernate maintains a mapping of basic type (org.hibernate.type.BasicType) and the corresponding Java type. For example, org.hibernate.type.StringType mapping with java.lang.String.

7. Specify BasicType explicitly

You can also specify the BasicType explicitly for a particular attribute. You can used the org.hibernate.annotations.Type for this like the following:

@Entity(name = "Post")
public class Post {
	@Id
	private Integer id;

	@org.hibernate.annotations.Type( type = "materialized_nclob" )
	private String comments;
}

This will handle comments attribute as LOB.