Generators in Hibernate
<generator /> is one of main element we are using in the hibernate framework [in the mapping file], let us learn and understand the concept behind this generators.
Up to now in our hibernate mapping file, we used to write <generator /> in the id element scope, actually this is default like whether you write this assigned generator or not hibernate will takes automatically
In fact this assigned means hibernate will understand that, while saving any object hibernate is not responsible to create any primary key value for the current inserting object, user has to take the responsibility for it. User has to set the id.
The thing is, while saving an object into the database, the generator informs to the hibernate
that, how the primary key value for the new record is going to be generate hibernate uses different primary key generator algorithms, for each algorithm internally a class is created by hibernate for its implementation
Hibernate provided different primary key generator classes and all these classes are implemented from “org.hibernate.id.IdentifierGeneratar” Interface
while configuring <generator /> element in mapping file, we need to pass parameters if that
generator class need any parameters, actually one sub element of <generator /> element is
<param />, we will learn more about this.
List of generators
The following are the list of main generators we are using in the hibernate framework
- assigned
- increment
- sequence
- identify
- hilo
- native
- foregin
- uuid.hex
- uuid.string
In the above generators list, the first 7 are used for int, long, short types of primary keys,
and last 2 are used when the primary key column type is String type (varchar2)
Assigned
This generator supports in all the databases
This is the default generator class used by the hibernate, if we do not specify <generator/>
element under id element then hibernate by default assumes it as “assigned”
<id name="id" column="ID" >
</id>
If generator class is assigned, then the programmer is responsible for assigning the primary
key value to object which is going to save into the database
<id name="id" column="ID" >
<generator class="assigned" />
</id>
Read me : Here “id” is a variable of a class and ”ID” is a columns of a table
Increment
This generator supports in all the databases, database independent
This generator is used for generating the id value for the new record by using the formula
Max of id value in Database + 1
If we manually assigned the value for primary key for an object, then hibernate doesn’t considers that value and it uses max value of id in database + 1 concept only
If there is no record initially in the database, then for the first time this will saves primary key value as 1, as max of id value in database + 1 means 0 + 1 , result id will be 1
<id name="id" column="ID" >
<generator class="increment" />
</id>
Read me: Here “id” is a variable of a class and ”ID” is a columns of a table
Sequence
Not has the support with MySql
This generator class is database dependent it means, we cannot use this generator class for all.
The database, we should know whether the database supports sequence or not before we are working with it while inserting a new record in a database, hibernate gets next value from the sequence used under assigns and stores that value for the new record
If programmer has created a sequence in the database then that sequence name should be
passed as the generator
<id name="id" column="ID">
<generator>
<param name="sequence">MyEntitySequence</param>
</genetator>
</id>
Read me: Here “id” is a variable of a class and ”ID” is a columns of a table
If the programmer has not passed any sequence name, then hibernate creates its own
sequence with name “Hibernate-Sequence” and gets next value from that sequence, and
then assigns that id value for new record
But remember, if hibernate want’s to create its own sequence, in hibernate configuration
file,hbm2ddl.auto property must be set enabled
Identity
This is database dependent, actually its not working in oracle
In this case (identity generator) the id value is generated by the database, but not by the
hibernate, but in case of increment hibernate will take over this this identity generator doesn’t needs any parameters to pass this identity generator is similar to increment generator, but the difference was increment generator is database independent and hibernate uses a select operation for selecting max of id before inserting new record
But in case of identity, no select operation will be generated in order to insert an id value
for new record by the hibernate
<id name="entityId" column="ID">
<generator class="identity"/>
</id>
Hilo
This generator is database independent
for the first record, the id value will be inserted as 1
for the second record the id value will be inserted as 32768
for the next records the id value will be incremented by 32768 and will stores into the
database (i mean adds to the previous)
actually in this case hibernate stores the count of id values generated in a column of
separated table, with name “hibernate_unique_key” by default with the column name “next_hi”
if we want to modify the table and column names theen wee need to pass 2 parameter’s
for the hilo generators
<id name=" entityId " column="ID">
<generator class=””>
<param name="table">your table name</param>
<param name="column">your column name </param>
</generator>
</id>
Native
When we “native” this generator class, it first checks whether the database
supports identify or not, if not checks for sequence and if not, then hilo will be used finally the order will be
- identify
- sequence
- hilo
For example, if we are connecting with oracle, if we use generator class as native then it is equal to the generator class sequence.