{"id":3812,"date":"2020-06-22T19:33:19","date_gmt":"2020-06-22T14:03:19","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3812"},"modified":"2020-12-14T15:58:50","modified_gmt":"2020-12-14T10:28:50","slug":"what-is-hibernate-algorithm-for-the-primary-key","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/what-is-hibernate-algorithm-for-the-primary-key\/","title":{"rendered":"What is Hibernate Algorithm for the primary key?"},"content":{"rendered":"\n<p><a href=\"https:\/\/www.h2kinfosys.com\/blog\/hibernate-overview\/\">Hibernate <\/a>&lt;Generator&gt; classes are used to generate the primary key or an identifier. Multiple algorithms are used to generate the primary key. &lt;generator&gt; tag is a subtag of &lt;id&gt; tag. All the generator classes implement hibernate.id.IdentifierGenerator interface and it overrides the generate(SessionImplementor,Object) method. If you want to define your user-defined generator, you should implement the IdentiferGenerator interface and override the generate().<\/p>\n\n\n\n<p><strong><em>The basic syntax is :<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;generator class=\"\"&gt;\n&lt;param name=\"\"&gt; value &lt;\/param&gt;\n&lt;\/generator&gt;\nExample:\n&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;!DOCTYPE hibernate-mapping PUBLIC \"-\/\/Hibernate\/Hibernate Mapping DTD 3.0\/\/EN\" \"http:\/\/hibernate.sourceforge.net\/hibernate-mapping-3.0.dtd\"&gt;\n&lt;hibernate-mapping&gt;\n&nbsp;&nbsp;&lt;class name=\"hibernate.Student\" table=\"student\"&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;id name=\"hid\" column=\"HID\" type=\"integer\"&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;generator class=\"increment\"&gt;&lt;\/generator&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/id&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property name=\"sfirstname\" column=\"SFIRSTNAME\" type=\"string\"&gt;&lt;\/property&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property name=\"slastname\" column=\"SLASTNAME\" type=\"string\"&gt;&lt;\/property&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property name=\"srollnumber\" column=\"SROLLNUMBER\" type=\"integer\"&gt;&lt;\/property&gt;\n&nbsp;&nbsp;&lt;\/class&gt;\n&lt;\/hibernate-mapping&gt;\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><em>Hiberanate provides multiple algorithms such as :<\/em><\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Increment<\/li><li>Assigned<\/li><li>Sequence<\/li><li>Hilo (High Low)<\/li><li>Select<\/li><li>Native<\/li><li>Identity<\/li><li>Uuid<\/li><li>Seqhilo<\/li><li>Guid<\/li><li>Foreign<\/li><li>Sequence-identity<\/li><\/ul>\n\n\n\n<p><strong>1] assigned:<\/strong> It is the default algorithm. If no &lt;generator&gt; tag is defined then assigned generator class is used. In this algorithm the value assigned to the primary key field will be the primary key value in the database.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;id name=\"hid\" column=\"HID\" type=\"integer\"&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;generator class=\"assigned\"&gt;&lt;\/generator&gt;\n&lt;\/id&gt;<\/pre>\n\n\n\n<p><strong>2] increment:<\/strong> This algorithm works with all types of database software but the data type of primary key should be int, short, byte or long.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;id name=\"hid\" column=\"HID\" type=\"integer\"&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;generator class=\"increment\"&gt;&lt;\/generator&gt;\n&lt;\/id&gt;<\/pre>\n\n\n\n<p><strong>3] sequence:<\/strong> If the sequence is not explicitly defined the database uses default sequence (+1) and every client request is then increment by +1.<\/p>\n\n\n\n<p>You can also create your own sequence by explicitly creating the sequence in the database as follows:<\/p>\n\n\n\n<p>create sequence my-sequence increment by 20<\/p>\n\n\n\n<p>You can also configure the sequence by using &lt;param&gt; tag which is a child tag of &lt;generator&gt; tag.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;id name=\"hid\" column=\"HID\" type=\"integer\"&gt;\n&lt;generator class=\"sequence\"&gt;\n&lt;param name=\"sequence\" value=\"my-sequence\"&gt;&lt;\/param&gt;\n&lt;\/generator&gt;\n&lt;\/id&gt;<\/pre>\n\n\n\n<p>Sequence algorithm can only be applied to int, short, long and byte data types.<\/p>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Sequence_step_algorithm\" rel=\"nofollow noopener\" target=\"_blank\">Sequence algorithm<\/a> is not supported by all the database softwares. For example:<\/p>\n\n\n\n<p>Oracles supports sequence algorithm but MySQL does not supports sequence algorithm. If there is no sequence in the database then default sequence i.e. HIBERNATE_SEQUENCE is used.<\/p>\n\n\n\n<p><strong>4] hilo (high low): <\/strong>It uses high low algorithm and it uses database data column range.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;id name=\"hid\" column=\"HID\" type=\"integer\"&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;generator class=\"hilo\"&gt;&lt;\/generator&gt;\n&lt;\/id&gt;<\/pre>\n\n\n\n<p>Default table is hibernate_unique_key and column is next_hi.<\/p>\n\n\n\n<p><strong>5] native: <\/strong>It uses identity, high low or sequence algorithms depending on the database vendor.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">&lt;id name=\"hid\" column=\"HID\" type=\"integer\"&gt;\n     &lt;generator class=\"native\"&gt;&lt;\/generator&gt;\n&lt;\/id&gt;<\/pre>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<p><strong>6] identity: <\/strong>It is used in MySQL, MS SQL Server, Sybase, DB2 to support the id column. Database is responsible to generate the unique identifier.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;id name=\"hid\" column=\"HID\" type=\"integer\"&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;generator class=\"identity\"&gt;&lt;\/generator&gt;\n&lt;\/id&gt;<\/pre>\n\n\n\n<p><strong>7] seqhilo: <\/strong>It uses sequence and hilo algorithms. Its return data type is short, int , long.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;id name=\"hid\" column=\"HID\" type=\"integer\"&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;generator class=\"seqhilo\"&gt;&lt;\/generator&gt;\n&lt;\/id&gt;<\/pre>\n\n\n\n<p><strong>8] uuid: <\/strong>For the generation of id it uses 128-bit UUID algorithm. The UUID is represented as hexadecimal digits having length of 32 characters. It generates identifier using IP Address. It is also used to generate password.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;id name=\"hid\" column=\"HID\" type=\"integer\"&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;generator class=\"uuid\"&gt;&lt;\/generator&gt;\n&lt;\/id&gt;<\/pre>\n\n\n\n<p><strong>9] guid: <\/strong>It only works on MySQL and MS SQL Server.<\/p>\n\n\n\n<p><strong>10] select: <\/strong>It uses the primary key value returned through database trigger.<\/p>\n\n\n\n<p><strong>11] foreign: <\/strong>It is mostly used in one-to-one association and uses id of another associated object.<\/p>\n\n\n\n<p><strong>12] sequence-identity: <\/strong>It is only supported in Oracle 10g drivers and uses a special sequence generation strategy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hibernate &lt;Generator&gt; classes are used to generate the primary key or an identifier. Multiple algorithms are used to generate the primary key. &lt;generator&gt; tag is a subtag of &lt;id&gt; tag. All the generator classes implement hibernate.id.IdentifierGenerator interface and it overrides the generate(SessionImplementor,Object) method. If you want to define your user-defined generator, you should implement the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3823,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[1020,1021],"class_list":["post-3812","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-hibernate-algorithm","tag-primary-key"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3812"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3812\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3823"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}