Gorm 返回值类型
我有一个与 Bar 对象具有 hasMany 关联的对象 Foo
class Foo {
String value
static hasMany = [
bars: Bar
]
}
class Bar {
String value
}
当我尝试执行以下操作时,
def foo = Foo.find("from Foo f where f.value=:value",[value:value])
,返回值 foo 的类型是 Foo,而当我执行此操作时,
def foo = Foo.find("from Foo f left join f.bars b where b.value=:value",[value:value])
类型是 Object
谁能向我解释为什么?
谢谢,肯。
I have an object Foo with a hasMany association to a Bar object
class Foo {
String value
static hasMany = [
bars: Bar
]
}
class Bar {
String value
}
when i try the follwing
def foo = Foo.find("from Foo f where f.value=:value",[value:value])
the type of the returned value foo is Foo, while when i do this
def foo = Foo.find("from Foo f left join f.bars b where b.value=:value",[value:value])
the type is an Object
Can anyone explain to me why ?
Thx, ken.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为第二个查询不仅选择
Foo
的属性,还选择Bar
的属性。如果您println foo
,第二个foo
的输出如下所示:如果您将
loggingSql = true
添加到您的dataSource< /code> 定义,Hibernate 会将其使用的实际 SQL 输出到 STDOUT - 类似于:
OK。但是如何避免从查询中返回
Bar
呢? - 我目前没有好的解决方案。我投票支持使用 select 子句使用 HQL,但实际上任何这些语法都会在 GORM 中产生错误。可以通过使用 Hibernate
Criteria
,但我目前不知道如何。因此,首先,您可能只想声明:
编辑:请注意,对于一对一关联,可以使用“dotty”语法,即
Because the second query selects properties of not just
Foo
but alsoBar
. If youprintln foo
, the output for the secondfoo
is something like this:If you add
loggingSql = true
to yourdataSource
definition, Hibernate will output the actual SQL it's using to STDOUT - something like:OK. But how to avoid returning the
Bar
from the query? - I currently don't have a good solution.I'd vote for using the select clause with HQL, but in practice any of those syntaxes will produce an error in GORM. It may be possible to solve this by using a Hibernate
Criteria
, but I currently don't know how.So, for a start, you might simply want to state:
EDIT: Note that for one-to-one associations the "dotty" syntax can be used, i.e.,