我的问题应该很明显,但我现在还看不到曙光:-(
我有两个像这样的域类:
class Parent {
String name
static hasMany = [children: Child]
}
class Child {
String name
}
现在我应该能够通过使用父级的动态查找器来找到 child 的父级,例如:
Parent.findByChildren([someChild] as Set)
但是我收到一个休眠错误,指出我的休眠 SQL 中有语法错误:
Caused by: java.sql.SQLException: No value specified for parameter 1
供引用: someChild 是一个具体实例。我想避免 Child 在定义中具有明确的父级。
那么我怎样才能从 child 转到 parent 呢?
最诚挚的问候,
克里斯蒂安·索恩·詹森
最佳答案
为了结束这个问题,我想报告我的“问题”的解决方案。我在父实体(客户或生产者)中使用namedQuery,如下所示:
Class Customer {
static hasMany = [channels: Channel]
static namedQueries = {
findByChannel {
channelId ->
channels {
eq 'id', channelId
}
}
}
}
然后我像这样找到客户:
def customers = Customer.findByChannel(channel.id).list()
通过这种方式, channel 摆脱了了解谁引用它的负担,而且我不必做任何人为的关系表。
我仍然认为我无法使用其中一个动态查找器一定是某种错误:
Customer.findByChannels([channel] as Set)
也许动态查找器没有考虑一对多关系,而只适用于简单属性??? (我使用的是 Grails 1.3.1)
谢谢您的回复!
克里斯蒂安·索恩·詹森
关于Grails 从多方面寻找 parent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3138031/