Primary Key Generation Algorithms
- Primary key is single column or Collection of columns in a table to recognize the records individually.
- In Database applications, to perform the database operations like retrieving a record, updating a record, deleting a record etc. we need primary key and its value.
- In Database applications, we are unable to give option to the users to enter primary key values, because, there is no guarantee for the data entered by the users whether it is unique data or not, but, in Database tables only unique values are accepted by primary key columns.
- To give guarantee for uniqueness in primary key values we have to use Primary key generation algorithms.
- Almost all the Persistence mechanisms like Hibernate, JPA, Open JPA, Toplink, etc. are having their own implementation for primary key generation algorithms.
Hibernate has provided support for the following primary key generation algorithms in order to generate primary key values.
Hibernate has represented all the above primary key generation algorithms in the form of a set of predefined classes provided in “org.hiberante.id” package.
If we want to use any of the above algorithms in Hibernate applications then we have to configure that algorithms in hibernate mapping file by using the following tags.
<hibernate-mapping> <class name="--" table="--"> <id name="--" column="--"> <generator class="--"> <param name="--">value</param> ---- </generator> </id> ----- </class> </hibernate-mapping>
- Where tag will represent a particular primary key generation algorithm.
- Where “class” attribute in tag will take either short name or Fully qualified name of Generator class.
- Where tag will provide single input parameter to the Primary key generator in order to generate primary key value.
- Where “name” attribute in tag will take parameter name , here we have to provide parameter value as body to the tag.