Generators In Hibernate
<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
- 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
123<generator class=""> <param name=""> value </param> </generator>
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
-
123<id name="prodId" column="pid"> <generator /> </id>
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 -> 1sequence
- 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
-
12345<id name="productId" column="pid"> <generator> <param name="sequence">MySequence</param> </genetator> </id>
- 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
-
123<id name="productid" column="pid"> <generator class="......."/> </id>
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.MySQLDialetNote:
- 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
-
12345678910<id name="productId" column="pid"> <generator> <param name="table">your table name</param> <param name="column">your column name </param> </generator> </id>
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.
post a comment