Sponsored Links
Ad by Google
In hibernate generator classes are used to generate unique identifiers for instances of the persistence class. Hibernate provides the list of built in generator classes to generate unique identifiers, all the generator classes implements the org.hibernate.id.IdentifierGenerator interface, and if your needs of unique identifiers is not solved by using built in generator classes, then you can create your own generator classes by implementing org.hibernate.id.IdentifierGenerator interface. Here is a example of custom generator class via implementing org.hibernate.id.IdentifierGenerator interface.
Here are the list of built in generator classes provided by Hibernate.
How to use:
For example on assigned generator strategy.
How to use:
How to use:
Hibernate: select max(generator_id) from generator
Hibernate: insert into generator (generator_name, description, generator_id) values (?, ?, ?)
How to use:
How to use:
Create sequence in your database, example
How to use:
How to use:
How to use:
How to use:
How to use:
How to use:
References:
Reference 1
Reference 2
Reference 3
Here are the list of built in generator classes provided by Hibernate.
1. org.hibernate.id.Assigned
The default generator class provided by Hibernate, If you explicitly not provide any generator element then, Hibernate will use assigned as a default generator strategy.How to use:
<id name="generatorId" column="generator_id"> <generator /> </id>OR
<id name="generatorId" column="generator_id"> <generator class="assigned"/> </id>Note: If you are using assigned strategy then, you have to provide the identifier value of the object before save() method is called.
For example on assigned generator strategy.
2. org.hibernate.id.IdentityGenerator
A generator class supported by DB2, MySql, MS Sql Server, Sybase and HypersonicSql. The returned identifiers is a type of long, int or short. The identity strategy used for auto increment columns.How to use:
<id name="generatorId" column="generator_id"> <generator class="identity"/> </id>For example on identity generator strategy.
3. org.hibernate.id.IncrementGenerator
An Identifiers that returns a long, int or short unique identifiers, calculation done by selecting the max value at startup. Not safe for use in cluster environment. Before inserting into the table it will execute an additional select query to get the max value, It is a high payee strategy think twice before opting.How to use:
<id name="generatorId" column="generator_id"> <generator class="increment"/> </id>Note:It will generate two query one for select and one for insert for example,
Hibernate: select max(generator_id) from generator
Hibernate: insert into generator (generator_name, description, generator_id) values (?, ?, ?)
4. org.hibernate.id.TableHiLoGenerator
An Identifier uses hi/low algorithm to generate unique identifier of type long, int or short. It uses a separate table for hi value. The default table uses for hi value is hibernate_unique_key with column hi_next as a hi value. Instead of default table you can create your own table and provides the details of the table and column by passing param values of generator class.How to use:
<id name="generatorId" column="generator_id"> <generator class="hilo"/> </id>OR, If you are using your own table then.
<generator class="hilo" > <param name="table">your table name</param> <param name="column">your next_hi value column</param> </generator>
5. org.hibernate.id.SequenceGenerator
An Identifier uses a Oracle style sequence and returns identifier type of long, int or short. SequenceGenerator is not supported by MySql database.How to use:
Create sequence in your database, example
create sequence your_seq increment by 1;then
<generator class="sequence"> <param name="sequence">your_seq</param> </generator>
6. org.hibernate.id.SelectGenerator
A generator class that select the just inserted row to determine the identifier value assigned by the database. The correct row is located using a unique key, must be defined unique key as natural-id.How to use:
<generator class="select"> <param name="key">unique_key_column</param> </generator>
7. org.hibernate.id.GUIDGenerator
A generator class supported by MySql and MS-Sql server only, and return type is a String.How to use:
<id name="generatorId" column="generator_id"> <generator class="guid"/> </id>
8. org.hibernate.id.UUIDHexGenerator
A generator class that returns a unique string of 32 length, This string will consist of only hex digits of 128 bit. This strategy is works in all databases.How to use:
<id name="generatorId" column="generator_id"> <generator class="uuid"/> </id>
9. org.hibernate.id.SequenceHiLoGenerator
An Identifier that uses as a combination of hi/low algorithm with Oracle style sequence strategy, and returns identifier of type long, int or short. You can specify a maximum lo value to determine how often new hi values are fetched.How to use:
<generator class="seqhilo"> <param name="sequence">hi_value</param> <param name="max_lo">10</param> </generator>
10. org.hibernate.id.ForeignGenerator
A generator class that, uses the identifier of another associated object. It is usually used in conjunction with a <one to one> primary key association.How to use:
<generator class="foreign"> <param name="property">associated_object</param> </generator>Here is an actual example of foreign generator strategy.
11. Native
There is no any class for native generator strategy, because native has no any special behavior. It will automatically select the identifier as identity, sequence or hilo based on the capability of underlying database.How to use:
<generator class="native"/>
12. Your own generator class
Yes, you can create your own generator class by implementing org.hibernate.id.IdentifierGenerator interface. Here is an example of Custom-Generator identifier.References:
Reference 1
Reference 2
Reference 3
Sponsored Links
0 comments:
Post a Comment