Grails 2.5.1(hibernate 3)标准多个连接到同一个表 [英] Grails 2.5.1 (hibernate 3) criteria multiple joins to same table

查看:222
本文介绍了Grails 2.5.1(hibernate 3)标准多个连接到同一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class SomeDomain {
static hasMany = [productData :ProductData]
}

ProductData是简单的类型/值对

我试图找到具有某种类型的多个产品的所有SomeDomains(在一个循环中)。目前标准的相关部分如下所示:

  SomeDomain.createCriteria.list {
somedata.each {type,价值 - >
productData {
eq(type,type)
eq(value,value)
}
}
}

但是,这只会产生一个SQL连接:



<$来自some_domain的内部加入product_data productdata_a1_ this_.id = productdata_a1_.some_domain_id
其中(productdata_a1_.type_id = 4和productdata_a1_.value ='GC')
和(productdata_a1_.type_id = 5和productdata_a1_.value ='P1')

显然type_id永远不会成功和检查= 4和= 5 ...

我真的很喜欢两个内部联接到product_data ...无法弄清楚我尝试了
createAlias(productData,product - $ {index})
这给了
org.hibernate.QueryException:重复关联路径:productData

解决方案

不确定为什么需要多个连接到同一个表?如果问题被正确理解,那么

  String query =from someDomain sd join productData pd其中pd.type in(:types)和pd .value in(:values)
def inputParams = [:]
inputParams.values = ['GC','P1']
inputParams.types = [4,5]
List resultsList = SomeDomain.executeQuery(query,inputParams,[readOnly:true,timeout:15])

pd.type可能必须是另一个连接,因为它在调试时会尝试获取.id所以添加另一个连接

 字符串query =来自someDomain sd join productData pd join pd.types tp其中tp.id in(:types)和pd.value in(:values)

如果您想按照问题中的建议进行多次连接,请按照以下步骤操作:

 字符串查询=从someDomain sd加入productData pd加入pd.types tp,ProductData pd2其中tp.id in(:types)和pd.value in(:values)and pd2.something = pd.something

接下来就是了f并查找链接到someDomain的productData,然后再次查找整个 ProductData作为pd2 ,然后确认 pd2.something = pd.something

在HQL中使用逗号变成与现有查询无关的新查找。


I've found similar questions, but no answers.

class SomeDomain {
    static hasMany= [productData:ProductData]
}

ProductData is simple type/value pair

I'm trying to find all SomeDomains that have multiple products of certain type (in a loop). Currently the relevant portion of the criteria looks like:

SomeDomain.createCriteria.list {
  somedata.each { type, value ->
    productData {
      eq("type", type)
      eq("value", value)
    }
  }
}

However, this generates only a single join with the SQL:

from some_domain this_ inner join product_data productdata_a1_ on this_.id=productdata_a1_.some_domain_id 
where (productdata_a1_.type_id=4 and productdata_a1_.value='GC') 
and (productdata_a1_.type_id=5 and productdata_a1_.value='P1') 

obviously type_id is never going to succeed on and'd checks for =4 and =5...

What I'd really like is two inner joins to product_data... can't figure out how to force this, though.

I tried createAlias("productData", "product-${index}") this gave org.hibernate.QueryException: duplicate association path: productData

解决方案

Unsure why you need multiple joins to same table ? if question is understood correctly

String query="from someDomain sd join productData pd where pd.type in (:types) and pd.value in (:values) "
def inputParams=[:]
inputParams.values=['GC','P1']
inputParams.types=[4,5]
List resultsList = SomeDomain.executeQuery(query,inputParams,[readOnly:true,timeout:15])

pd.type may have to be another join since in the debug it attempts to get the .id so add another join

  String query="from someDomain sd join productData pd join pd.types tp where tp.id in (:types) and pd.value in (:values) "

If you wanted to do multiple joins as suggested in the question

  String query="from someDomain sd join productData pd join pd.types tp, ProductData pd2 where tp.id in (:types) and pd.value in (:values) and pd2.something=pd.something"

That is then going off and looking productData that is linked to someDomain then again looking up entire ProductData as pd2 and then confirming where pd2.something = pd.something

Using comma's in HQL becomes a new lookup not related to existing query..

这篇关于Grails 2.5.1(hibernate 3)标准多个连接到同一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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