Can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Generators <generator> In Hibernate

Hibernate » On Jun 26, 2011 By Sivateja

<generator /> is one of main element we are using in the hibernate framework [in the mapping file],  let us see the concept behind this generators.

 

  • Up to now in our hibernate mapping file, we used to write <generator /> in the id element scope, actually this is default like whether you write this assigned generator or not hibernate will takes automatically
  • In fact this assigned means hibernate will understand that, while saving any object hibernate is not responsible to create any primary key value for the current inserting object, user has to take the response
  • The thing is, while saving an object into the database, the generator informs to the hibernate that, how the primary key value for the new record is going to generate
  • hibernate using different primary key generator algorithms, for each algorithm internally a class is created by hibernate for its implementation
  • hibernate provided different primary key generator classes and all these classes are implemented from

org.hibernate.id.IdentifierGeneratar Interface

  • while configuring <generator /> element in mapping file, we need to pass parameters if that generator class need any parameters, actually one sub element of <generator /> element is <param />, will talk more about this

 

Example

 

List of generators

The following are the list of main generators we are using in the hibernate framework

  • assigned
  • increment
  • sequence
  • identity
  • hilo
  • native
  • foregin
  • uuid.hex
  • uuid.string

In the above generators list, the first 7 are used for int,long,short types of primary keys, and last 2 are used when the primary key column type is String type (varchar2)

assigned

  • This generator supports in all the databases
  • This is the default generator class used by the hibernate, if we do not specify <generator –> element under id element then hibernate by default assumes it as “assigned”
  • If generator class is assigned, then the programmer is responsible for assigning the primary key value to object which is going to save into the database

 

Increment

  • This generator supports in all the databases, database independent
  • This generator is used for generating the id value for the new record by using the formula

Max of id value in Database + 1

  • if we manually assigned the value for primary key for an object, then hibernate doesn’t considers that value and uses max value of id in database + 1 concept only :-)
  • If there is no record initially in the database, then for the first time this will saves primary key value as 1, as…

max of id value in database + 1
0 + 1
result -> 1

 

sequence

  • Not has the support with MySql
  • This generator class is database dependent it means, we cannot use this generator class for all the database, we should know whether the database supports sequence or not before we are working with it
  • while inserting a new record in a database, hibernate gets next value from the sequence under assigns that value for the new record
  • If programmer has created a sequence in the database then that sequence name should be passed as the generator

  • If the programmer has not passed any sequence name, then hibernate creates its own sequence with name “Hibernate-Sequence” and gets next value from that sequence, and than assigns that id value for new record
  • But remember, if hibernate want’s to create its own sequence, in hibernate configuration file, hbm2ddl.auto property must be set enabled

sql> create sequence MySequence incremented by 5;

  • first it will starts with 1 by default
  • though you send the primary key value., hibernate uses this sequence concept only
  • But if we not create any sequence, then first 1 and increments by 1..bla bla. in this case hibernate creating right..? so ensure we have hbm2ddl.auto enabled in the configuration file

 

identity

  • This is database dependent, actually its not working in oracle
  • In this case (identity generator) the id value is generated by the database, but not by the hibernate, but in case of increment hibernate will take over this
  • this identity generator doesn’t needs any parameters to pass
  • this identity generator is similar to increment generator, but the difference was increment generator is database independent and hibernate uses a select operation for selecting max of id before inserting new record
  • But in case of identity, no select operation will be generated in order to insert an id value for new record by the hibernate

As this is not working in Oracle, if you would like to check this in MySql you must change the configuration file as…….

class: com.mysql.jdbc.Driver
url: jdbc:mysql://www.java4s.com:3306/test (test is default database)
user: root (default)
pass: (default)
dialet: org.hibernate.dialet.MySQLDialet

Note:

  • jar file required (in class path we must set..
    mysql-connector-java-3.0.8-stable-bin.jar (version number may change)
  • Actually this jar will never come along with mysql database software,  to get this jar file we need to download the following file, and unzip it.mysql-connectar-java-3.0.8-stable.zip

 

hilo

  • This generator is database independent
  • for the first record, the id value will be inserted as 1
  • for the second record the id value will be inserted as 32768
  • for the next records the id value will be incremented by 32768 and will stores into the database (i mean adds to the previous)
  • actually this hibernate stores the count of id values generated in a column of separated table, with name “hibernate_unique_key” by default with the column name “next_hi”
  • if we want to modify the table and column names theen wee need to pass 2 parameter’s for the hilo generators

 

native

when we use this generator class, it first checks whether the database supports identity or not, if not checks for sequence and if not, then hilo will be used finally the order will be..

  • identity
  • sequence
  • hilo

For example, if we are connecting with oracle, if we use generator class as native then it is equal to the generator class sequence.

foreign

we will see about this generator in one-to-one relationship, else you may not understand.







By [ Java/J2EE Developer & Web Designer ]
Founder of Java4s.com - Indian, lives in United States. You can contact me at sivateja@java4s.com


Comments

13 Responses to “Generators <generator> In Hibernate”
  1. Shaik Rasool says:

    Hi java4s team,

    while i am working with “sequence” generator, i used product table for some time after that i changed my table name like pro.

    Now my question is whenever i am using new table the pid column is not starting with 1. It is taking “previous table’s max value +1″.

    How can i get the pid value from 1.

    Please reply ASAP.

  2. rahul says:

    if i want to generate the identity values as FF001,FF002,FF003,….. with the help of SequnceGenerator then how we generate pls help me…….

  3. srinivas says:

    Hi Shaik Rasool,
    This is not hibernate problem. Problem with table schema find the table increment start make it as 1.

  4. Hi srinivas Can you please explain it clearly i am also facing the same problem,mentioned by Shaik Rasool .

  5. mintu says:

    good post
    thanks

  6. Shaik Rasool says:

    Hi java4s team,

    plz provide the code to generate alphanumeric primary keys by using “uuid.hex” and “uuid.string”

  7. chittibabu says:

    Hi to all,
    this is site is very useful.
    thank q java4s team..

  8. bhagat says:

    Thank you sivateja ..

    really appreciate it yaar ..

  9. jafar says:

    In the list of generators class list the identity class is misspelled. “identify”.

  10. Java4s says:

    @jafar

    Updated, thank you :-)

  11. rajasekhar says:

    what is the difference between hibernate load and get methods

  12. narayana says:

    it is not clear to me, you have not consolidated the concepts properly in this post. Sorry to mention that, but your effort is too good and thanks for that..

  13. dilip says:

    if table is blank … then generator class increment is not generate primary key as 1… its show hibernate mapping problem..
    please help asap

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
JSP, Web Services [ In Progress ]
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.