Hibernate - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Hibernate Classes

Hibernate Classes

shape Introduction

The chapter explains about Hibernate Classes. The functionality of generator class is providing the primary key for an object while saving in the database. It will be written in the hibernate mapping file, by default assigning is the element will be generated if the developer did not mention. Hibernate has more generator classes which are implemented from the org.hibernate.id.IdentifierGeneratar Interface. Following are the generator Hibernate Classes available in hibernate assigned, increment, sequence are regularly used generator classes, assigned is the by default class.

assigned

shape Description

It is the default class in Hibernate. Internally Hibernate calls Assigned Generator Class. This assigned class returns an Id of a POJO class Primary Column Id. It is Database Independent. So it can use any Database. For Example, [xml]<id name="employeeId" column="eid"> <generator class="assigned"/> </id>[/xml]

Increment

shape Description

Into the Mapping File, generator class name is written by incrementing tag. So, Hibernate is internally called as "IncrementGenerator Class". It selects maximum of existing id from a table, increments the value one by one and then returns the Id to Hibernate. This class follows the one formula like max(Id)+1. Increment Generator class is also Database Independent. For Example, [xml]<id name="employeeId" column="id"> <generator class="increment"/> </id>[/xml]

sequence

shape Description

In hbm file, if generator class name is configured as in the sequence, then Hibernate is internally called as "SequenceGenerator Class". When configuring the sequence Generator, if user defined sequence name is passed as a parameter then Sequence generator class reads next value of that sequence from Database and returns it as an Id to the Hibernate. Sequence Generator Class is Database dependent. For Example, [xml]<id name="employeeId" column="eid"> <generator class="sequence"/> </id>[/xml] If a user does not pass sequence value, then Hibernate will generate predefined sequence "hibernate_sequence".

hilo

shape Description

In a hbm file, if generator class name is configured as Hilo, then Hibernate is internally called as "TableHiLoGenerator Class". It returns Id as 1 for the first time. This generator class stores the number of times id's are generated in a table of a database. This class is configured with three parameters. For generating id for second time, Hibernate will create Id using a formula, like max_lo value*next_lo value+next_lo value. Eg:Id generating second time means 32767*1+1=32768. For Example, [xml]<id name="employeeId" column="eid"> <generator class="sequence"/> </id>[/xml] If generator class is not mapped in mapping file, then Hibernate will take default values.

foreign

shape Description

In hbm file, if generator class name is configured as foreign, then Hibernate is internally called as "ForeignGenerator Class". This is used for creating the relationship between two objects. If a Parent Class Object is mapped to a Child Class Object, then generator class name maps as foreign into the Mapping File. ForeignGenerator is also Database independent.

UUID

shape Description

UUID means Universal Unique Id. When PrimaryKey column is String type then Hibernate will give two generator classes. They are "assigned" and "UUID" generator Hibernate Classes. This is used the create the unique string based on some conditions like It will create a big String. If an object is read from Database table, then it will pass the long String on Object. Hence, this generator class is not useful.

Summary

shape Key Points

  • While passing identify class not needed any arguments.
  • assigned is the only the class for supporting all databases.
  • sequence class does not support for MySQL.
  • Programmers will use mostly assigned class because it supports for all the databases.