场景是这样的:
每个Process
具有多个ProcessingSteps
我编写的代码能够获取所有进程,而无需与ProcessingSteps
对应。
我知道我缺少where子句,我想问一下我们如何在Grails中做到这一点。
我只想为每个Process
提取对应的ProcessingStepUpdate
我有两个域类ProcessingStep
和ProcessingStepUpdate
package a.b.c
public class ProcessingStep {
Process process
}
public class ProcessingStepUpdate{
static belongsTo = [processingStep: ProcessingStep]
ProcessingStep processingStep
}
这是我正在写的脚本
Process.list(max:1).each {
//List<ProcessingStep> test2= ProcessingStep.findAllByProcess(it)
//println it
def test3 = ProcessingStep.createCriteria().list() {
eq("process",it)
}
println it
it.list().each {
//not telling it where to get the list from
ProcessingStep.list().each { pstep ->
def test4 = ProcessingStepUpdate.createCriteria().list() {
eq("processingStep",pstep)
// Projections are aggregating, reporting, and
// filtering functions that can be applied after
// the query has finished.
// A common use for projections is to summarize data
// in a query
/* projections{
groupProperty("processingStep")
}*/
}
println pstep
//List<ProcessingStepUpdate> test = ProcessingStepUpdate.findAllByProcessingStep(it)
//List<ProcessingStepUpdate> test = ProcessingStepUpdate.findWhere()
//println "it"
}
}
}
一天之内,我就一直被这个问题困扰。.OOPS世界的新手!
最佳答案
我将尝试猜测,该任务仅是迭代 child 的 child 。然后是这样的:
public class Process {
static hasMany = [processingSteps: ProcessingStep]
}
public class ProcessingStep {
static belongsTo = [process: Process]
static hasMany = [updates: ProcessingStepUpdate]
}
public class ProcessingStepUpdate {
static belongsTo = [processingStep: ProcessingStep]
}
Process.list().each{ process ->
process.processingSteps.each { step ->
step.updates.each {
println "Process: $process, Update: $it"
}
}
}
甚至
def updates = Process.list()*.processingSteps.flatten()*.updates.flatten()
println updates.join('\n')
看一眼Groovy Collections,尤其是在“星点'*'处。运算符”部分。