Monday 14 May 2012

Hibernate Identity Generator



Identity value of HB POJO class Object is the criteria value for HB Software to perform synchronization between HB POJO class object and table row.


Hibernate supplies lot of predefined algorithms as identity value generators for pojo class objects. Most of these algorithms can generate dynamic and unique values are identity values of HB POJO class object.
1.Increment
2.Identity
These algorithms are pre-defined class supplied by HB API implementing org.hibernate.id.IdentifierGenerator. All these classes are also having nick names/ shortcut names to utilize..

Nick Names
Algorithm Class Name
Assigned
Org.hibernate.id.Assigned
Increment
Org.hibernate.id.IncrementGenerator
Identity
Org.hibernate.id.IdentityGenerator
Sequence
Org.hibernate.id.SequenceGenerator
Hilo(high&low)
Org.hibernate.id.TablehiloGenerator
Seqhilo
Org.hibernate.id.SequencehiloGenerator
Uuid
Org.hibernate.id.UUIDHexGenerator
Guid
Org.hibernate.id.GUIDGenerator
Native
For this No class
Select
Org.hibernate.id.SelectGenerator
Foreign
Org.hibernate.id.ForeignGenerator





To specify these algorithms <generator>(sub tag of <id> tag) in HB Mapping file.if no algorithm is explicitly specify in mapping file the HB software takes “assigned” as default algorithm
While working with any identity value generator algorithm consider the following two rules:
1.Make sure that the type identity value generated by algorithm is compatable with the data type of identity field member variable of POJO class.
2. Make sure that underlying DB software supports the chosen algorithm

Assigned Algorithm:
This algorithm lets the application developer to assign identity value to HB POJO class object manually before calling session.save().
In mapping file:
<hibernate-mapping>
<class name=“empbean” table=“emp”>
<id name=“no” column=“EID”>
<generator class=“assigned”/>(
</id>
<property …..
</property>

this is default algorithm so if we don't specify <generator class="assigned"> it will take automatically this



Increment:
     This algorithm generates identity value long,short,or int.. This algorithm works with all database software. This algorithm uses max.value+1 formula on identity field.
<id name=“no” column=“eio”>
<generator class=“increment”/>
</id>

When we use this algorthim we should not give id value. Hibernate software generates its own id value by using maxvalue+1. if we give id value that id won’t store in the database. 



Identity algorithm:
     Supports identity columns in db2,mysql,sqlserver,sybase & hypersonic sql database software. This algorithm returns identity value of type long/short/int.
This algorithm doesnot work with oracle database s/w.
This algorithm uses maxvalue+1 formula for an identity.
While working with this algorithm with mysql use
Not null,primarykey,autoincrement otherwise it won’t work(it will give error)
<id name="empno" column="empno">
   <generator class="identity"></generator>
</id>



What is difference between Increment and Identity.
Increment
        Identity
1.Identity field member variable related column in db table need not to have any constrains to work with this algorithm
  1.Identity filed related column in DB table should not be taken as       identity column by applying auto increment constraint to work with this   algorithm
2.When   all   records of table are deleted increment algorithm     generates  1  as  the identity value
     2.If all the records of the table are deleted after applying and working with identity algorithm. This algorithm still considers deleted records values to generate new identity value.
3.This algorithm works with all database softwares        
3.This algorithm works with only some database software







Sequence:
This algorithm uses sequence created in the database.
It works with DB2,POSTGREY,SQL,SAP,Oracle databases.
This algorithm doesnot work with MySql Database Software.
Steps to work with Sequence:
1.Create table in oracle
2.Create sequence for that table (assume that myora_seq1 is name of sequence)
3.Write the following code in mapping file.
 
<id name=“empno” column=“empno”>
<generator class=“sequence”>
<param name=“sequence”>myora_seq1</param>
</generator>
</id>
We can work with sequence algorithm only with those database software which support sequences creation



Hilo:
This algorithm uses high/low algorithm to efficiently generate identity values of type long, short,int by using helper of type long,short,int by using helper table column value and given parameters.
Steps to use Hilo algorithm
1. create anther table
Create table mytable(mycol number);
2. Insert into mytable values(10);
3.That above table is called is helper table. Because of this table this algorithm works..
4.Add following code in mapping file.
<id name="empno" column="empno">
   <generator class="hilo">
   <param name="table">mytable</param>
   <param name="column">mycol</param>
   </generator>
</id>



Native algorithm:
  This algorithm doesnot have its own behavior this algorithm dynamically picks up identity,sequence or hilo algorithm(one of them) depending upon the capabilities of the underlying database software.
This algorithm doesnot have its own classname. Because it doesnot have its own individual behavior.







Seqhilo:
This algorithm is enhancement of hilo algorithm.which internally uses and gives sequence name and parameter values to generate identity values of type long,short,int.
This algorithm doesn’t expect special table name from the programmer. This works with that database software with supports DB sequences
Steps to use Seqhilo:
1.Make sure that myora_seq1 is available in oracle database software.
2.Configure seqhilo algorithm in HB mapping file has shown below.

UUID(Universal Unique ID):

This algorithm uses network IP Address as base value to generated string type hexadecimal notation values as identity value for POJO Objects.
This value contains 32 digits in hexadecimal notation.
This algorithm works with all database software.
Example Code:
1.change “empno” member variable as String and change setter and getter methods
2. <id name="empno" column="empno">
   <generator class="uuid">
 
   </generator>
3.Run the client program

Guid:
This algoritham uses database generated guid string as identity value. This algorithm only works with SQL Server,MySql DataBase Software.
Steps to use Guid algoritham
1.Create table emp1
(empno varchar2(20),
Ename varchar2(20),
Sal int
);
2.Pojo class
public class emp1 {
private int sal;
private String ename,empno;
Getter and setter methods
}
3. Mapping file
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class  name="emp1" table="emp1">
<id name="empno" column="empno">
   <generator class="guid">
 
   </generator>
</id>
<property name="ename" column="ename"></property>
<property name="sal" column="sal"></property>
</class>
</hibernate-mapping>
4.Configuration file same…
5.  Run the Program










10 comments:

  1. second statement is wrong in (identity) differece bw increment and identity.plz check it practically...

    ReplyDelete
  2. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.

    rpa training in bangalore
    best rpa training in bangalore
    RPA training in bangalore
    rpa course in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  3. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
    Python training in bangalore
    Python course in pune
    Python training in bangalore

    ReplyDelete
  4. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    AWS Training in pune

    AWS Online Training

    AWS Training in Bangalore

    ReplyDelete
  5. Thanks For Sharing The InFormation The Information Shared Is Very Valuable Please Keeep updating Us Time Just Went On reading the article Python Online Course Data Science Online Course Data Science Online Course Hadoop Online Course Awsw Online Course

    ReplyDelete
  6. I have really enjoyed sharing your post. thank you so much for your sharing this document. this document more useful and improve our knowledge.

    aws training in chennai | aws training in annanagar | aws training in omr | aws training in porur | aws training in tambaram | aws training in velachery

    ReplyDelete
  7. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject...keep up!!!

    Android Training in Chennai

    Android Online Training in Chennai

    Android Training in Bangalore

    Android Training in Hyderabad

    Android Training in Coimbatore

    Android Training

    Android Online Training

    ReplyDelete
  8. I would really like to come back again right here for like wise good articles or blog posts. Thanks for sharing. It seems you are so busy in last month. The detail you shared about your work and it is really impressive. I have you bookmarked to test out new substance you post.

    Aws Training in Chennai

    Aws Training in Velachery

    Aws Training in Tambaram

    Aws Training in Porur

    Aws Training in Omr

    Aws Training in Annanagar

    ReplyDelete