Master java skills

Hibernate Generator Classes

Generator classes in Hibernate are used to generate Ids for entity classes. These Ids map to primary keys in the database. Below is the list of predefined list of generator classes in Hibernate.

  • assigned
  • increment
  • sequence
  • hilo
  • identity
  • native
  • foreign
  • uuid

Apart from these predefined generators, we can define custom generators as well. We will see that later in this tutorial.

Assigned:

This is the default generator. Even if we don’t specify any generator, Hibernate considers this as default. In this case, we have to set the id explicityly from the code. That’s why it is known as assigned. The programmer has to assign this.

<id name="teacherId" column="teacher_id"/>
<id name="teacherId" column="teacher_id">             
 <generator class="assigned"/> 
</id>

Both of the above examples are same. In both the cases, generator class is ‘assigned’.

Increment: