Hibernate <generator> Element
In this tutorial you will learn about the hibernate's <generator> element , which is used to auto generate the primary key value.
The <generator> tag is an optional child element of the element <id>. In <generator> a class name is to be assigned using which unique identifier is generated for the new record at the time of new record is saved or inserted into table. For example :
<class name="roseindia.Employee" table="employee"> <id name="empId" type="long" column="Id" > <generator class="assigned"/> </id>
A simple interface org.hibernate.id.IdentifierGenerator is implemented by all generators. Hibernate provides a various built-in implementations some of them built-in generators are as follows :
shortcut name for the built-in generators | Description |
increment | For the type long, short or int identifiers are generated these identifiers will not be remain unique if any other process inserts data into the same table. It should not used in the clustered environment. |
identity | In DB2, MySQL,
MS SQL Server, Sybase and HypersonicSQL an identity column is supported. The returned type of an identifier may be long, short or int. |
sequence | In DB2, MySQL,
MS SQL Server, Sybase and HypersonicSQL an identity column is supported. The returned type of an identifier may be long, short or int. |
assigned | This generator lets to assign an identifier to the object by application before calling the save(). A default strategy if generator element is not specified. |
select | This generator retrieves a primary key and primary key value by selecting the row by some unique key that are assigned by a database trigger |
foreign | This generator uses the identifier of another associated object. Commonly used in conjunction with a |
sequence-identity | Only supported by Oracle 10g drivers. It is a specialized sequence generation strategy. For the actual value generation it uses a database sequence. |
NOTE : If you put <generator class="native"/>, Hibernate automatically choose between identity, sequence or hilo algorithm to auto generate the primary key, by calculating the capabilities of the used database.
EXAMPLE
Given below the configuration XML file of table which is involved in one-to-one mapping, whose id/primary key will auto-generate using <generator> tag :
Worker.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="net.roseindia"> <class name="Worker" table="worker"> <id name="workerId" column="worker_id"> <generator class="native" /> </id> <one-to-one name="workerDetail" class="net.roseindia.WorkerDetail" cascade="save-update"></one-to-one> <property name="firstname" column="firstname" /> <property name="lastname" column="lastname" /> <property name="birthDate" type="date" column="birth_date" /> <property name="cellphone" column="cell_phone" /> </class> </hibernate-mapping>
Here, we are using <generator class="native"/>, means Hibernate automatically choose between identity, sequence or hilo algorithm to auto generate the primary key, by calculating the capabilities of the used database.
WorkerDetail.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="net.roseindia"> <class name="WorkerDetail" table="workerdetail"> <id name="workerId" type="java.lang.Long"> <column name="worker_id" /> <generator class="foreign"> <param name="property">worker</param> </generator> </id> <one-to-one name="worker" class="net.roseindia.Worker" constrained="true"></one-to-one> <property name="street" column="street" /> <property name="city" column="city" /> <property name="state" column="state" /> <property name="country" column="country" /> </class> </hibernate-mapping>
In the above XML, we are using <generator class="foreign">, means this generator uses the identifier of another associated object(worker table).
For complete example, click here