开发者

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

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?


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜