Gorm 返回值类型

发布于 2024-10-03 06:51:40 字数 475 浏览 7 评论 0原文

我有一个与 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 技术交流群。

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

发布评论

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

评论(1

浪荡不羁 2024-10-10 06:51:40

因为第二个查询不仅选择 Foo 的属性,还选择 Bar 的属性。如果您 println foo,第二个 foo 的输出如下所示:

[Foo : 3, Bar : 2]

如果您将 loggingSql = true 添加到您的 dataSource< /code> 定义,Hibernate 会将其使用的实际 SQL 输出到 STDOUT - 类似于:

select
    foo0_.id as id0_0_,
    bar2_.id as id2_1_,
    foo0_.version as version0_0_,
    foo0_.value as value0_0_,
    bar2_.version as version2_1_,
    bar2_.value as value2_1_ 
from
    foo foo0_ 
left outer join
    foo_bar bars1_ 
        on foo0_.id=bars1_.foo_bars_id 
left outer join
    bar bar2_ 
        on bars1_.bar_id=bar2_.id 
where
    bar2_.value=?

OK。但是如何避免从查询中返回 Bar 呢? - 我目前没有好的解决方案。

我投票支持使用 select 子句使用 HQL,但实际上任何这些语法都会在 GORM 中产生错误。可以通过使用 Hibernate Criteria,但我目前不知道如何。

因此,首先,您可能只想声明:

def fooBar = Foo.find("from Foo f left join f.bars b where b.value=:value",
   [value:value])
def foo = fooBar[0]
assert foo instanceof Foo

编辑:请注意,对于一对一关联,可以使用“dotty”语法,即

from Foo foo where foo.baz.value=:value

Because the second query selects properties of not just Foo but also Bar. If you println foo, the output for the second foo is something like this:

[Foo : 3, Bar : 2]

If you add loggingSql = true to your dataSource definition, Hibernate will output the actual SQL it's using to STDOUT - something like:

select
    foo0_.id as id0_0_,
    bar2_.id as id2_1_,
    foo0_.version as version0_0_,
    foo0_.value as value0_0_,
    bar2_.version as version2_1_,
    bar2_.value as value2_1_ 
from
    foo foo0_ 
left outer join
    foo_bar bars1_ 
        on foo0_.id=bars1_.foo_bars_id 
left outer join
    bar bar2_ 
        on bars1_.bar_id=bar2_.id 
where
    bar2_.value=?

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:

def fooBar = Foo.find("from Foo f left join f.bars b where b.value=:value",
   [value:value])
def foo = fooBar[0]
assert foo instanceof Foo

EDIT: Note that for one-to-one associations the "dotty" syntax can be used, i.e.,

from Foo foo where foo.baz.value=:value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文