Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Understanding Hibernate element 
 

In this lesson you will learn about hibernate method in detail. Hibernate generator element generates the primary key for new record. There are many options provided by the generator method to be used in different situations.

 

Understanding Hibernate <generator> element

                         

In this lesson you will learn about hibernate <generator> method in detail. Hibernate generator element generates the primary key for new record. There are many options provided by the generator method to be used in different situations.

The <generator> element

This is the optional element under <id> element. The <generator> element is used to specify the class name to be used to generate the primary key for new record while saving a new record. The <param> element is used to pass the parameter (s) to the  class. Here is the example of generator element from our first application:
<generator class="assigned"/>
In this case <generator> element do not generate the primary key and it is required to set the primary key value before calling save() method.

Here are the list of some commonly used generators in hibernate:

Generator Description
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.
identity It supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.
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
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.
seqhilo The seqhilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.
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.
guid It uses a database-generated GUID string on MS SQL Server and MySQL.
native It picks identity, sequence or hilo depending upon the capabilities of the underlying database.
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.
select retrieves a primary key assigned by a database trigger by selecting the row by some unique key and retrieving the primary key value.
foreign uses the identifier of another associated object. Usually used in conjunction with a <one-to-one> primary key association.

                         

» View all related tutorials
Related Tags: sql c hibernate api com class ant comparison import io property method sed const operators expression operator port criteria name

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

26 comments so far (
post your own) View All Comments Latest 10 Comments:

Hi All,
I uesd <generator class="increment"/> in hbm.xml file. I am getting "could not fetch initial value for increment generator" Error. Please help me to solve this problem.

Regards,
Sandeep

Posted by Sandeep on Sunday, 12.7.08 @ 22:21pm | #82489

iam doing a project matrmonial iwant to develop REGISTRATION FORM.PLS HELP ME

Posted by chalamreddy on Monday, 07.28.08 @ 18:44pm | #69565

Hi,

I have an entity object which have an id with sequence generator. in some case i want to override this generator if the id is being passed by entity object. Is it possible to achieve this by <generator class="sequence"> itself?

Please help me on this

Posted by Jeya Uma on Tuesday, 06.10.08 @ 18:50pm | #62897

hi

Posted by Raveendra on Monday, 05.19.08 @ 16:50pm | #60451

hello everybody ...
For the single primary key.not at all a problem. we can work it out. But what to do with composit primary key.i.e. in a table primary key is made up of combination of more than one field.


regards
uday bhat

Posted by uday bhat on Tuesday, 05.6.08 @ 12:01pm | #58608

When i tried with the example given, only tables are created. But data is not inserted ...

I tried like this..

create a transaction object(tx) with the session i.e session.beginTransaction(). and then tx.commit() to commit the transaction. Then my data is updated and inserted in the table.

I think by default autocommit may be false.

Enjoy..
Mythili

Posted by Mythili on Wednesday, 04.30.08 @ 15:36pm | #58212

Hi, I'm trying to generate an id in hibernate in a specific format i.e. i want it to appear 0001,0002, etc ...

Here is the code I use in my request.hbm.xml file

<id name="requestId" column="REQUEST_ID">
<generator class="increment"/>
</id>

but this generates the ids 1,2,etc...

Any idea's?

Posted by Amanda Sweeney on Thursday, 03.6.08 @ 15:39pm | #51693

Hi,
I writtenexaple in that in i mentioned generator class="sequence" but its giving fallowing exaception can any help me to solve that problem.

error like this:
org.hibernate.HibernateException: identifier of an instance of com.Honey altered from 2 to 2

if i use class="genarator" its working fine.

Posted by Ravi Kumar on Monday, 02.4.08 @ 22:26pm | #47214

Hi RoseIndiaTeam,

who is the subject matter expert in maintaining this site for java in RoseIndia? really i want to talk to them once .......

very nice site for Java Developers.

the structure, point wise explianation is really heart touching

keep on updating the site ............




stephen
09818827207

Posted by T.Stephen on Wednesday, 01.30.08 @ 19:17pm | #46617

I am new to Hibernate. I have tried with given example source . All the mapped tables are being created but data is not been inserted in table. Even there is no exception. Can you please guide me what could be possible reason for this??

Posted by Shikhar on Tuesday, 01.29.08 @ 17:25pm | #46497

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.