如何获取GORM对象映射到的表的名称?

发布于 2024-12-08 04:29:35 字数 154 浏览 1 评论 0原文

假设我有类似的内容:

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

如果我有对此对象实例的引用,如何获取 foo_table 的名称?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬 2024-12-15 04:29:35

导入org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder。

从域类获取表名:

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

从域类的实例获取表名:

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

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
痞味浪人 2024-12-15 04:29:35

JamesA 的答案将会起作用,但前提是表名明确定义,就像问题中那样。

如果您希望获取表名,无论它是否在映射中指定,可以使用 SessionFactory 来完成:

def tableName = sessionFactory.getClassMetadata(Foo).tableName

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文