Updated April 4, 2023
Introduction to Hibernate Generator
Generator is a type of class in the hibernate framework. It is written under braces (<>) like <generator>. This class is used as a sub-element of ID. The main use of this class is to mark any object of persistent class as unique. This class is used to make a hibernate framework understand the requirement and the strategy to get the primary key generated. This class helps enable the use of primary key functionality to make a record unique in the database. These classes operate upon interface called: org.hibernate.id.IdentifierGenerator. We need to have this interface implemented for generator classes to function. This class is embedded in ID by default if we do not have it inserted voluntarily. One can create one’s own generator class with customized functionalities involved as per business requirements. The general structure of generator class looks like below:
<generator class=""> // Any code or tags can be included under these generator tags, </generator>
Hibernate Generator Classes
There are different hibernate generator classes built-in already in the framework. These built-in classes are explained below. Different generator classes used in hibernate framework:
It is s default generator class used in case we do not define generator class specifically to Id. This class supports all databases. If generator class is assigned, then the application coder/ programmer will be responsible for assigning primary key value to the object of the persistent class. The same value will be stored in the database.
<hibernate-mapping> <class> <id name="testID" column="tid"> <generator /> // or we can also use <generator class="assigned"></generator> // instead of above default generator tag. </id> </class> </hibernate-mapping>
It is used to generator unique vale of int, short or long type. If a table already has an identifies then it considers its maximum value otherwise it considers first generated identifies as “1” and then keeps on adding one to the previous identifier. It can be used in any database making this class as database independent. It is calculated using formula. In case we manually try to assign value to the primary key in database with this generator class implemented then the manual entry is ignored and only the value assigned using the formula of “increment” generator is used.
<hibernate-mapping> <class> <id name="testID" column="tid"> <generator class="assigned"></generator> </id> </class> </hibernate-mapping>
This class is used to create a primary key using sequences from the database. Since these sequences are already defined in databases like oracle, DB2, SAP DB, Postgre, etc this generator becomes database dependent. In case the programmer has created its own sequence instead of picking up directly from the database then he can pass his sequence’s name as a parameter name in the generator class. This generator class is used in all databases except MySQL.
<hibernate-mapping> <class> <id name="testID" column="tid"> <generator class="sequence"> <param name="own_sequence"> CSTOMIZED_SQ_NAME </param> </generator> </id> </class> </hibernate-mapping>
This generator is database dependent and used only in Sybase, My SQL, MS SQL Server, DB2, and HypersonicSQL databases. This is used to support the “id” column which is generated internally by database only by calling the auto-incremented column algorithm of respective database. In this case, hibernate will not generate the primary key and so no parameters for this class to be passed.
<hibernate-mapping> <class> <id name="testID" column="tid"> <generator class="Identity"> </generator> </id> </class> </hibernate-mapping>
This comes under the TableHiloGenerator class. It generates a unique key using a high and low algorithm for short, long, and int type. The first value will be 1 and other subsequent values will be incremented by 32767 as per algorithm’s logic. There are max three parameters which can be passed as a parameter to this class. In case we do not pass any of these parameters then default parameters will be picked up as per default data maintained. Formula used to calculate key values in this also is: max_lo * next_hi + next_hi.
<hibernate-mapping> <class> <id name="testID" column="tid"> <generator class="hilo"> <param name="table">test_table</param> <param name="column">test_col1</param> <param name="max_lo">100</param> </generator> </id> </class> </hibernate-mapping>
This is a database-independent generator.
Native generator is used to provide a list of generators in priority sequence depending upon database vendor. There are three generators involved in this generator listed below in sequence: identity, sequence, or hilo. If any database does not the previous model implemented, then it will pass another generator as per the list. The final “hilo” generator is independent of the database.
<hibernate-mapping> <class> <id name="testID" column="tid"> <generator class="native"> </generator> </id> </class> </hibernate-mapping>
This generator class used high and low algorithms on sequence names. The primary key, in this case, is of short, int or long data type. It works like the way “hilo” generator class works but with a difference that it works on sequence created. It is database dependent and so you may not find it working with databases that do not have their own sequence setup.
<hibernate-mapping> <class> <id name="testID" column="tid"> <generator class="seqhilo"> </generator> </id> </class> </hibernate-mapping>
This generator class uses an external object as a reference to create the primary key. This class returns id using built-in the algorithm which uses an external foreign object as a reference to create a key. It uses only one to one mapping in this class. The full form of this class is the ForeignGenerator class.
<hibernate-mapping> <class> <id name="testID" column="tid"> <generator class="foreign"> </generator> </id> </class> </hibernate-mapping>
All the mentioned above generator classes return short, int, or long data type as a primary key but what in case we need a string as a primary key? To have this issue resolved to hibernate has a “uuid” generator class. This class returns an alphanumeric string as a primary key. It is a short form of AbstractUUIDGenerator class. This class uses an algorithm to generate a string of 32 characters. This length is based out of 4 settings in the system: IP Address of the machine, Start-up time of JVM, System time, Counter value in JVM. There are other functions involved like get() or load() methods in hibernate. To understand the full process, we need to understand uuid generator class whole as a separate topic. It is a system and database dependent generator class as the requirements of the generator algorithm depend upon the local system and database used.
<hibernate-mapping> <class> <id name="testID" column="tid"> <generator class="uuid"> </generator> </id> </class> </hibernate-mapping>
This generator class is again database dependent. It works on MS SQL Server and MySQL databases. It gives out the string as a primary key. It uses GUID generated by a database of string data type making it dependent on the database.
<hibernate-mapping> <class> <id name="testID" column="tid"> <generator class="guid"> </generator> </id> </class> </hibernate-mapping>
This generator class uses a primary key that is returned by a database trigger in the system. It is database dependent generator class. In case the trigger is not maintained in the database then this generator class may not work.
This generator class is used by oracle 10g driver only. It uses its own special algorithm based on sequence generation strategy. This is dependent on the database as in case there is no sequence maintained in the database then the use of this generator class will not be meaningful.
This is one of the most important class in hibernate as the primary key becomes the base in setting up any database. When using hibernate for inserting, extracting, or updating data using hibernate it becomes very crucial to know the use of primary key construction strategies. In hibernate, the primary key is controlled using generator classes. These generator classes use different algorithms based on or independent of database properties with some databases having it implemented while some not. This is a very flexible way of generating relevant and safe databases behind any front-end project.
This is a guide to Hibernate Generator. Here we discuss the Introduction and Different generator classes used in hibernate framework along with its syntax. You can also go through our other suggested articles to learn more –