使用具有参数的GORM在一对一Grails域上加入查询 [英] join query on one-to-one Grails domain using GORM with parameters

查看:104
本文介绍了使用具有参数的GORM在一对一Grails域上加入查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Grails domain类,可以这样说:

I have a two Grails domain class, let say:

class Hero {
    String name
    Float level

    Familiar familiar
}

class Familiar {
    String name
    Integer raceId
}

现在,我要检索的所有Hero具有Familiar的名称都类似于给定String的名称,例如 example :"hachiko".要使用SQL进行此操作,我需要执行的查询与此类似:

Now, what I want is to retrieve all Hero having Familiar with the name similar to a given String, for example: "hachiko". To do that using SQL, all I need is to perform a query similar to this:

SELECT h.name, h.level
FROM HERO h JOIN FAMILIAR f ON h.id = f.ownerId
WHERE f.name LIKE '%hachiko%'

但是如何在Grails上执行此操作?这是我的代码:

But how can I do that on Grails? This is my code:

String filter = "hachiko"
def hero = Hero.executeQuery(
    "SELECT h.name, h.level FROM HERO h JOIN h.familiar WHERE h.id = " +
    "h.familiar.ownerId and h.familiar.name LIKE :filter",
    [filter, "%$filter%"]
)

这项工作吗?我应该创建那些static属性(hasOnebelongsTo),并将它们放在哪里?因为到目前为止,没有这些static属性,我收到的是error消息:

Would this work? Should I create those static properties (hasOne, belongsTo), and where should I place them? Because so far, without those static properties, I'm receiving an error message:

ORA-00904: "HERO0_"."FAMILIAR_ID": invalid identifier

并添加了这些static属性,我得到了:

And having those static properties added, I got this:

org.hibernate.MappingException: hasOne property [Hero.familiar] is not bidirectional. Specify the other side of the relationship!

他们的关系是一对一的可选. (例如,一个Hero最多可以包含一个Familiar),并且FamiliaridHero相同.

Their relationship is optional one-to-one. (e.g. A Hero can have at most one Familiar), and the id of the Familiar is the same of it's Hero.

推荐答案

您可以使用

这篇关于使用具有参数的GORM在一对一Grails域上加入查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆