{"id":1551,"date":"2010-09-07T15:37:04","date_gmt":"2010-09-07T10:07:04","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1551"},"modified":"2016-12-19T15:06:06","modified_gmt":"2016-12-19T09:36:06","slug":"how-to-map-database-table-without-id-with-grails-domain","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-map-database-table-without-id-with-grails-domain\/","title":{"rendered":"How to Map database table without id with grails domain"},"content":{"rendered":"<p>Recently I got a query regarding mapping a database table which do not have any id and version. For example the table have two varchar fields username and password nothing more than that.<br \/>\nAlthough it was something strange for me that table doesn&#8217;t have the id field. The good thing is that the username is a primary key in the table and this is not auto incremented user want to create it by his own method.<\/p>\n<p>The good thing about grails is, in most of the cases you get your answer in the docs <a href=\"http:\/\/grails.org\/doc\/latest\/\">http:\/\/grails.org\/doc\/latest\/<\/a> . So in this case we just need to change the id field in grails domain like this<\/p>\n<pre lang=\"groovy\">\r\nclass Test {\r\n    String username\r\n    String password\r\n\r\n    static mapping = {\r\n        id name: 'username'\r\n        version false\r\n        id generator: 'assigned'\r\n    }\r\n    static constraints = {\r\n        username(nullable: true)\r\n    }\r\n}\r\n<\/pre>\n<p>and we are done :).<br \/>\n<\/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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I got a query regarding mapping a database table which do not have any id and version. For example the table have two varchar fields username and password nothing more than that. Although it was something strange for me that table doesn&#8217;t have the id field. The good thing is that the username is [&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":15},"categories":[7],"tags":[382,380,381],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1551"}],"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=1551"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1551\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}