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.
Hibernate Framework is given some Generator Hibernate Classes to generate the Id.
Generator class is used to map a file which gives information of Generator Class type used and Id to generate it.
Generator class maps into the Mapping File using generator tag. It contain Generator Class Names.
Generator class tag is used under the Id tag in Mapping file.
Following are the generator Hibernate Classes available in hibernate assigned, increment, sequence are regularly used generator classes, assigned is the by default class.
assigned
increment
sequence
identify
hilo
native
foreign
uuid.hex
uuid.string
assigned
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
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
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
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.
table: It contains hibernate_unique_key by default.
column: It contain a next_hi by the default value.
max_lo: It contains 32767 by the default value.
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
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
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
1. Ip Address of a Machine
2. JVM startup time
3. System time
4. The counter value of JVM
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
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.