{"id":3995,"date":"2011-08-19T00:53:31","date_gmt":"2011-08-18T19:23:31","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=3995"},"modified":"2016-12-19T15:16:39","modified_gmt":"2016-12-19T09:46:39","slug":"grails-get-table-name-mapped-to-a-domain","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-get-table-name-mapped-to-a-domain\/","title":{"rendered":"Grails: Get table name mapped to a domain"},"content":{"rendered":"<p>Hi guys,<\/p>\n<p>Recently while working on a project, I needed to get the table names associated with a particular domain as I needed to perform complex join operations on a particular table. The <a title=\"Grails Developers\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">database that my team was working<\/a> on was a legacy database. The domains that were created were mapped to particular tables to provide backward compatibility with the existing system&#8217;s database and, hence the table names were specified in the mapping closure. Now I needed a way to get the names of the tables that were associated with a particular domain.<\/p>\n<p>The <i>SessionFactoryProxy<\/i> can be directly used to get the table name associated with the domain. So the code is as follows:<\/p>\n<p>[code]<br \/>\nimport org.hibernate.metadata.ClassMetadata<br \/>\nimport org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder<br \/>\nimport org.codehaus.groovy.grails.commons.ApplicationHolder<\/p>\n<p>Class clazz = Class.forName(&amp;quot;com.intelligrape.domain.Album&amp;quot;, true, Thread.currentThread().getContextClassLoader())<\/p>\n<p>String mappedTable <\/p>\n<p>def sessionFactory = ApplicationHolder.application.mainContext.getBean(&amp;quot;sessionFactory&amp;quot;)<br \/>\nClassMetadata hibernateMetaClass = sessionFactory.getClassMetadata(clazz)<br \/>\nmappedTable = hibernateMetaClass.getTableName()<\/p>\n<p>println &amp;quot;Mapped table name: &amp;quot; + mappedTable<br \/>\n[\/code]<\/p>\n<p>Now the above modified code returns the table name associated with a domain.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi guys, Recently while working on a project, I needed to get the table names associated with a particular domain as I needed to perform complex join operations on a particular table. The database that my team was working on was a legacy database. The domains that were created were mapped to particular tables to [&hellip;]<\/p>\n","protected":false},"author":33,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[7],"tags":[620,4840,382,265,621],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3995"}],"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\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=3995"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3995\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=3995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=3995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=3995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}