Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails:连接不相关表的最佳方法是什么?_Grails_Gorm - Fatal编程技术网

Grails:连接不相关表的最佳方法是什么?

Grails:连接不相关表的最佳方法是什么?,grails,gorm,Grails,Gorm,我知道做这件事一定要有个好办法 class A { def foo C c } class B { def bar C c } class C { def x def y def z } 我想得到所有的C,其中A.foo=='foo',其中B.bar=='bar' 我知道我可以从一个或另一个父类中完成,例如: def my_cs = A.withCriteria { eq('foo', 'foo') }*.c …我可以分别抓取所有的B,其中bar=='bar'

我知道做这件事一定要有个好办法

class A {
  def foo
  C c
}

class B {
  def bar
  C c
}

class C {
  def x
  def y
  def z
}
我想得到所有的C,其中A.foo=='foo',其中B.bar=='bar'

我知道我可以从一个或另一个父类中完成,例如:

def my_cs = A.withCriteria { eq('foo', 'foo') }*.c
…我可以分别抓取所有的B,其中bar=='bar',然后在我的C中循环。。。但这似乎效率低下,我觉得必须有一种合理的方法通过criteria语法来实现

这是可能的,还是有其他可以接受的方法

谢谢

--------解决方案----------

我发现我不需要测试B



如果你的
C
类没有对
a
s of
B
s的引用,那么我认为将
a
s和
B
s分开,然后将它们组合起来可能是你唯一的实际选择。然而,我不认为它真的那么糟糕

如果你想确保你有一个独特的
C
s的集合,那也很容易(不需要循环)。看

e、 g


我很害怕。我也在做类似的事情,但在代码审查中被要求找到一种更有效的方法。这很好,谢谢你的回答。我有点希望比我聪明的人会跳进来说“事实上,这是一个巧妙的技巧…”:)如果你真的想出了一个办法,请确保你回到这里并添加你自己的答案。
A.withCriteria {
  createAlias('c.B', 'cb', CriteriaSpecification.LEFT_JOIN)
  eq('foo', 'foo')
  isNull('cb.c')
  projections {
    property 'c'
  }
}
def my_cs = []
my_cs.addAll(A.withCriteria { eq('foo', 'foo') }*.c)
my_cs.addAll(B.withCriteria { eq('bar', 'bar') }*.c)
my_cs.unique(true) // Removes duplicates from the list