{"id":828,"date":"2010-06-07T16:15:42","date_gmt":"2010-06-07T10:45:42","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=828"},"modified":"2016-12-19T15:05:48","modified_gmt":"2016-12-19T09:35:48","slug":"how-to-use-custom-method-for-id-generation-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-use-custom-method-for-id-generation-in-grails\/","title":{"rendered":"Use custom method for id generation in grails"},"content":{"rendered":"<p>Hibernate usage in Grails is one of my favorite topic so to learn more about it I just started reading Hibernate. In hibernate I found that user can manually assign the id of object but in grails we need to rely on it and it generates it own sequential ids. Though there is no harm and saves our time as well to generate id, but there may be the case where we need to assign the id of an object manually.<\/p>\n<p>As the grails uses hibernate, the same behavior is available in grails as well. There are different methods to generate id for object by default Grails uses &#8216;native&#8217; method which rely on the id generation capability of database (which is sequential most of the time). So to change this behavior and use the custom id generation we just need to do the changes in static mapping.<\/p>\n<pre> \r\nstatic mapping={\r\n        id generator:'assigned'\r\n    }\r\n<\/pre>\n<p>and now in controller you can write something like<\/p>\n<pre> \r\nobject.id=customMethodToGenerateId()\r\n<\/pre>\n<p>or to make you controllers clean you can do that in beforeInsert<\/p>\n<pre> \r\ndef beforeInsert = {\r\n         id=customMethodToGenerateId()\r\n     }\r\n<\/pre>\n<p>I hope this will be helpful for some people.<\/p>\n<p><\/p>\n<p>Hope it helps<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/uday\/\">Uday Pratap Singh<\/a><br \/>\n<a href=\"mailto:uday@intelligrape.com\">uday@intelligrape.com<\/a><br \/>\n<a href=\"https:\/\/twitter.com\/farjinaarad\">https:\/\/twitter.com\/meudaypratap<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hibernate usage in Grails is one of my favorite topic so to learn more about it I just started reading Hibernate. In hibernate I found that user can manually assign the id of object but in grails we need to rely on it and it generates it own sequential ids. Though there is no harm [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/828"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=828"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/828\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}