How to get the name of the table GORM object is mapped to?

拟墨画扇 提交于 2019-12-20 17:38:34

问题


Say I have something like:

class Foo {
    static mapping = {
        table 'foo_table'
    }
}

How can I get the name of foo_table if I have a reference to an instance of this object?


回答1:


Import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.

To get the table name from the domain class:

def tableName = GrailsDomainBinder.getMapping(Foo).table.name 

And to get the table name from an instance of the domain class:

def tableName = GrailsDomainBinder.getMapping(foo.class).table.name



回答2:


JamesA's answer will work, but only if table name if defined explicitly, like in the question.

If you wish to get a table name whether or not it was specified in mapping, it can be done using SessionFactory:

def tableName = sessionFactory.getClassMetadata(Foo).tableName


来源:https://stackoverflow.com/questions/7664845/how-to-get-the-name-of-the-table-gorm-object-is-mapped-to

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!