如何获取GORM对象映射到的表的名称?
假设我有类似的内容:
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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
导入org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder。
从域类获取表名:
从域类的实例获取表名:
Import
org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder
.To get the table name from the domain class:
And to get the table name from an instance of the domain class:
JamesA 的答案将会起作用,但前提是表名明确定义,就像问题中那样。
如果您希望获取表名,无论它是否在映射中指定,可以使用 SessionFactory 来完成:
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
: