{"id":7521,"date":"2012-09-14T15:30:54","date_gmt":"2012-09-14T10:00:54","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=7521"},"modified":"2016-12-19T15:16:41","modified_gmt":"2016-12-19T09:46:41","slug":"mapping-duplicate-domain-class-name-having-different-packages-using-autoimport-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/mapping-duplicate-domain-class-name-having-different-packages-using-autoimport-in-grails\/","title":{"rendered":"Mapping duplicate domain class name having different packages using autoImport in Grails"},"content":{"rendered":"<p>Hi,<br \/>\n<br \/>\nRecently one of my friend asked me whether there is a mechanism in grails to handle the use-case where we have different packages but having same domain name. Here we are having duplicate domain class names in different package and by default the domain classes are auto-imported in HQL queries which doesn&#8217;t required to specify whole class name. Hence due to it class name are no longer unique because of this it cause <em><strong>DuplicateMappingException<\/strong><\/em> in our case. For this I refer my friend to set <a href=\"http:\/\/grails.org\/doc\/2.1.0\/ref\/Database%20Mapping\/autoImport.html\">autoImport<\/a> false in mapping.<br \/>\n<br \/>\nGrails provide us a mapping key named &#8216;<a href=\"http:\/\/grails.org\/doc\/2.1.0\/ref\/Database%20Mapping\/autoImport.html\">autoImport<\/a>&#8216;. We can disable the auto-import for any one of the domain or both which will fix our problem to some extent. Lets see how it helps :<br \/>\n<br \/>\nSuppose we have 2 packages named :<br \/>\n1. <em><strong>com.tarun.poc.project <\/strong><\/em> having Domain named <strong>&#8220;Issue&#8221;.<\/strong><br \/>\n[groovy]<br \/>\npackage com.tarun.poc.project<br \/>\nclass Issue {<br \/>\n    String summary<br \/>\n    static constraints = {<br \/>\n    }<br \/>\n}<br \/>\n[\/groovy]<br \/>\n2. <em><strong>com.tarun.poc.company<\/strong><\/em> having Domain named <strong>&#8220;Issue&#8221;.<\/strong><br \/>\n[groovy]<br \/>\npackage com.tarun.poc.company<br \/>\nclass Issue {<br \/>\n    String description<br \/>\n    static constraints = {<br \/>\n    }<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>When you run your app you will recieve the exception some thing like this :<br \/>\n[groovy]<br \/>\nnested exception is org.hibernate.DuplicateMappingException: duplicate import: Issue refers to both com.tarun.poc.project.Issue and com.tarun.poc.company.Issue<br \/>\n[\/groovy]<\/p>\n<p>Now we <strong>add autoImport to the mapping of domain &#8216;Issue&#8217;<\/strong> one or both and <strong>set it to false<\/strong> as specified below :<br \/>\n[groovy]<br \/>\npackage com.tarun.poc.project<br \/>\nclass Issue {<br \/>\n    String summary<br \/>\n    static constraints = {<br \/>\n    }<br \/>\n    static mapping = {<br \/>\n        autoImport false<br \/>\n        table &#8216;project_issue&#8217;<br \/>\n    }<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>It will fix your problem regarding &#8216;DuplicateMappingException&#8217;. If you see above we have also specified <a href=\"http:\/\/grails.org\/doc\/2.1.0\/ref\/Database%20Mapping\/table.html\">table mapping<\/a> because if we didn&#8217;t specify table mapping it will create a single table in database with name &#8216;issue&#8217; and having properties of both the domains into that table.<\/p>\n<p>Hence to create different tables for Domain Class or customizes the name of the database table associated with the domain class we used <a href=\"http:\/\/grails.org\/doc\/2.1.0\/ref\/Database%20Mapping\/table.html\">table mapping<\/a>.<\/p>\n<p>Hope it helps \ud83d\ude42<br \/>\n<br \/>\nThanks,<br \/>\nTarun Pareek<br \/>\ntarun@intelligrape.com<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/author\/tarun\/\">More Blogs by Me<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, Recently one of my friend asked me whether there is a mechanism in grails to handle the use-case where we have different packages but having same domain name. Here we are having duplicate domain class names in different package and by default the domain classes are auto-imported in HQL queries which doesn&#8217;t required to [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[967,965,969,968,4840,966],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7521"}],"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\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=7521"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7521\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=7521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=7521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=7521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}