Hibernate Generator

This tutorial is helpful in understanding the concept of hibernate generator.


 

Ads

Tutorials   
Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4.2.8 Maven dependency Hibernate 4.2.7 Maven dependency
Ads

Hibernate Generator

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 table.
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.

Student.hbm.xml

<?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>

Download complete source code

Advertisements

Share on Google+Share on Google+

Hibernate Generator

Posted on: July 5, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Hibernate Generator  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads