使用具有参数的GORM在一对一Grails域上加入查询 [英] join query on one-to-one Grails domain using GORM with parameters
问题描述
我有两个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
属性(hasOne
,belongsTo
),并将它们放在哪里?因为到目前为止,没有这些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
),并且Familiar
的id
与Hero
相同.
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屋!