Generator classes in Hibernate

The <generator> class is a sub-component of id. It is utilized to create the one of a kind identifier for the objects of tenacious class. There are numerous generator classes characterized in the Hibernate Framework.


All the generator classes implements the org.hibernate.id.IdentifierGenerator interface. The application software programmer may make one's very own generator classes by executing the IdentifierGenerator interface. Hibernate framework gives many built-in generator classes:

  1. assigned
  2. increment
  3. sequence
  4. hilo
  5. native
  6. identity
  7. seqhilo
  8. uuid
  9. guid
  10. select
  11. foreign
  12. sequence-identity

1) assigned

It is the default generator strategy if there is no <generator> element . In this case, application assigns the id. For example:

  1. ....  
  2.  <hibernate-mapping>  
  3.   <class ...>  
  4.     <id ...>  
  5.      <generator class="assigned"></generator>  
  6.     </id>  
  7.             
  8.     .....  
  9.             
  10.   </class>  
  11.  </hibernate-mapping>  

2) increment

It generates the unique id only if no other process is inserting data into this table. It generates short, int or long type identifier. If a table contains an identifier then the application considers its maximum value else the application consider that the first generated identifier is 1. For each attribute value, the hibernate increment the identifier by 1. Syntax:

  1. ....  
  2.  <hibernate-mapping>  
  3.   <class ...>  
  4.     <id ...>  
  5.      <generator class="increment"></generator>  
  6.     </id>  
  7.             
  8.     .....  
  9.             
  10.   </class>  
  11.  </hibernate-mapping>  

3) sequence

It utilizes the sequence of the database. in the event that there is no arrangement characterized, it makes a sequence consequently for example if there should be an occurrence of Oracle database, it makes an arrangement named HIBERNATE_SEQUENCE. If there should arise an occurrence of Oracle, DB2, SAP DB, Postgre SQL or McKoi, it utilizes sequence however it utilizes generator in interbase. Syntax:

  1. .....  
  2.  <id ...>  
  3.   <generator class="sequence"></generator>  
  4.  </id>  
  5.  .....  

For characterizing your very own succession, utilize the param subelement of generator.

  1. .....  
  2.  <id ...>  
  3.   <generator class="sequence">  
  4.       <param name="sequence">your_sequence_name</param>  
  5.   </generator>  
  6.  </id>  
  7.  .....  

4) hilo

It utilizes high and low algorithm to create the id of sort short, int and long. Sentence structure:

  1. .....  
  2.  <id ...>  
  3.   <generator class="hilo"></generator>  
  4.  </id>  
  5.  .....  

5) native

It utilizes character, sequence or hilo relying upon the database seller. Sentence structure:

  1. .....  
  2.  <id ...>  
  3.   <generator class="native"></generator>  
  4.  </id>  
  5.  .....  

6) identity

It is utilized in Sybase, My SQL, MS SQL Server, DB2 and HypersonicSQL to help the id column. The returned id is of sort short, int or long. It is duty of database to create special identifier.


7) seqhilo

It utilizes high and low algorithm on the predetermined sequence name. The returned id is of sort short, int or long.


8) uuid

It utilizes 128-piece UUID algorithm to create the id. The returned id is of sort String, one of a kind inside a system (since IP is utilized). The UUID is spoken to in hexadecimal digits, 32 long.


9) guid

It utilizes GUID created by database of sort string. It chips away at MS SQL Server and MySQL.

10) select

It utilizes the essential key returned by the database trigger.

11) foreign

It utilizes the id of another related item, for the most part utilized with <one-to-one> affiliation.

12) arrangement identity

It utilizes an special sequence age strategy. It is upheld in Oracle 10g drivers only.





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition