This tutorial is helpful in understanding the concept of hibernate generator.
Hibernate <generator> element:
<generator/> element is used in hibernate for mapping file. It is optional
and works with <id> element to generate primary key value.
It generate unique identifiers for instances of your persistent class. It automatically increment value of your primary key.
Generator method provides many options to work on different situations-
1.assigned : lets the application to assign an identifier to the object before save() is called. This is the default strategy if no <generator> element is specified.
2.increment: It generates identifiers of type long, short or
int that are unique only when no other process is inserting data into the same
It should not the used in the clustered environment.
3.identify: It supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.
4.hilo: The hilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database. Do not use this generator with connections enlisted with JTA or with a user-supplied connection.
5.sequence: The sequence generator uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int.
6.native: It picks identity, sequence or hilo depending upon the capabilities of the underlying database. It picks identity, sequence or hilo depending upon the capabilities of the underlying database.
7.select: retrieves a primary key assigned by a database trigger by selecting the row by some unique key and retrieving the primary key value.
8.seqhilo: The seqhilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.
9.uuid: The uuid generator uses a 128-bit UUID algorithm to generate identifiers of type string, unique within a network (the IP address is used). The UUID is encoded as a string of hexadecimal digits of length 32.
10.guid: It uses a database-generated GUID string on MS SQL Server and MySQL.
11.foreign: uses the identifier of another associated object. Usually used in conjunction with a <one-to-one> primary key association.
Here we are giving you block of code to show how generator work.
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="net.roseindia.table.Student" table="student"> <id name="roll" type="int" column="roll_no"> <generator class="native" /> </id> <property name="Sname" type="string" column="student_name" /> <property name="course" type="string" column="course" /> <property name="address" type="string" column="address" /> </class> </hibernate-mapping>
Liked it! Share this Tutorial