Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaHibernate – Generator Classes

Hibernate – Generator Classes

Hibernate is an open-source, non-invasive, lightweight java ORM(Object-relational mapping) framework that is used to develop persistence logic that is independent of Database software. In JDBC to develop persistence logic, we deal with primitive types. Whereas in Hibernate framework we use Objects to develop persistence logic that is independent of database software. We can make independent persistence logic in all JAVA, JEE. Persistence logic means storing and processing the data for long use. 

An ORM(Object-relational mapping) framework simplifies data creation, data manipulation, and data access. It is a programming technique that maps the object to the data stored in the database. It internally uses the JDBC API to interact with the database.

Salient features of Hibernate

  • It is an open-source framework and is available for everyone to use.
  • It can be used alone or we can use Hibernate with other java technology and framework.
  • Hibernate class need not implement Hibernate API interfaces and need not extend from Hibernate API classes.

Hibernate Generator Classes

A generator class is used to generate an ID for an object, which is going to be inserted in the database as a primary key. The ID is a unique identifier for the objects of the persistent class. We can use any generator classes in our application as per our requirements. Hibernate provides a  <generator> tag to represent the generator. However, it is a sub-element of <id>. There is a shortcut name for each predefined generator class so that the <generator> tag has to configure with this shortcut name. 

All hibernate generator classes implements hibernate.id.IdentifierGenerator interface, and overrides the generate(SessionImplementor, Object) method to generate the identifier or primary key value. We can create our own generator classes by implementing the IdentifierGenerator interface and by overriding the generate() method.

Types of Generator Classes

Hibernate provides many predefined generator classes. Some of the important predefined generator classes in hibernate are:

  1. assigned
  2. foreign
  3. hilo
  4. identity
  5. increment
  6. native
  7. sequence
  8. uuid

1. assigned generator

The assigned generator is the default generator. By default hibernate consider assigned as a generator and assigned is a shortcut name given for the Assigned class. This class returns the same id set by us to the hibernate which in turn store an object with that id in the database.

Syntax in hbm.xml file:

 <hibernate-mapping>  

        <class …>  

                 <id name=”studentId” column=”sid”>  

                       <generator class=”assigned”></generator>  

                </id>  

        </class>  

</hibernate-mapping>  

2. foreign generator

foreign is a  shortcut name given for the ForeignGenerator class. The ForeignGenerator is only suitable for one-one relationships. It returns the id of the parent object as the id for the child object.

Syntax in hbm.xml file:

<hibernate-mapping>  

       <class …>  

                <id name=”studentId” column=”sid”>  

                       <generator class=”foreign”></generator>  

               </id>  

       </class>  

</hibernate-mapping>  

3. hilo generator

hilo is a shortcut name for the TableHiloGenerator class. While configuring hilo generator class in the hbm.xml file, we pass “table”, “column”, and “max_lo” as parameters.

Syntax in hbm.xml file:

<hibernate-mapping>  

      <class …>  

               <id name=”studentId” column=”sid”>  

                      <generator class=”hilo”>

                                <param name=”table”>document</param>

                               <param name=”column”>column</param>

                              <param name=”max_lo”>13210</param>

                    </generator>  

              </id>  

      </class>  

</hibernate-mapping>  

The default value of above three parameters given by Hibernate are:

  • table = hibernate_unique_key
  • column =  next_hi
  • max_lo = 32767

4. identity generator

identity is a shortcut name given for the IdentityGenerator class. It reads an auto-incremented column algorithm of the database and takes that value and returns it as id to hibernate. Identity generator supports databases like Sybase, My SQL, MS SQL Server, DB2, and HypersonicSQL but doesn’t support Oracle database because there is no autoincrement functionality in Oracle database. Hence identity generator can be considered as a database-dependent generator.

Syntax in hbm.xml file:

<hibernate-mapping>  

      <class …>  

               <id name=”studentId” column=”sid”>  

                      <generator class=”identity”></generator>  

              </id>  

      </class>  

</hibernate-mapping>  

5. increment generator

An increment is a shortcut name given for the IncrementGenerator class. The IncrementGenerator class reads the max value of the existing ID in the database table and then increments it by one and returns the ID value to hibernate.

Syntax in hbm.xml file:

<hibernate-mapping>  

     <class …>  

              <id name=”studentId” column=”sid”>  

                     <generator class=”increment”></generator>  

             </id>  

     </class>  

</hibernate-mapping>  

6. native generator

native is the shortcut name given for the NativeGenerator class. If the database supports an identity generator, then it acts as identity otherwise it will check for the database support of other generators. It selects identity, sequence, or hilo depending upon the capabilities of the underlying database.

Syntax in hbm.xml file:

<hibernate-mapping>  

    <class …>  

             <id name=”studentId” column=”sid”>  

                    <generator class=”native”></generator>  

            </id>  

    </class>  

</hibernate-mapping>  

7. sequence generator

sequence is a shortcut name given for the SequenceGenerator class. It reads the next value of a database sequence and then returns that value as id to the hibernate.

Syntax to create a sequence in the database:

create sequence <sequence_name> start with <random_number> increment by <random_number>

Syntax in hbm.xml file :

<hibernate-mapping>  

   <class …>  

            <id name=”studentId” column=”sid”>  

                   <generator class=”sequence”>

                          <param name=”sequence”>sequential_datasource</param>

                   </generator>

           </id>  

   </class>  

</hibernate-mapping>  

8. uuid generator

uuid is a shortcut name given for the AbstractUUIDGenerator class. It generates a unique string Identifier and returns to hibernate based on the IP Address of the machine, Start-up time of JVM, System time, and Counter value in JVM. 

Syntax in hbm.xml file:

<hibernate-mapping>  

  <class …>  

           <id name=”studentId” column=”sid”>  

                  <generator class=”uuid”>     </generator>

          </id>  

  </class>  

</hibernate-mapping>  

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments