Grails 2.5.1(hibernate 3)标准多个连接到同一个表 [英] Grails 2.5.1 (hibernate 3) criteria multiple joins to same table
问题描述
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,然后再次查找整个 在HQL中使用逗号变成与现有查询无关的新查找。 I've found similar questions, but no answers. 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: However, this generates only a single join with the SQL: 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 pd.type may have to be another join since in the debug it attempts to get the .id so add another join If you wanted to do multiple joins as suggested in the question That is then going off and looking productData that is linked to someDomain then again looking up entire Using comma's in HQL becomes a new lookup not related to existing query.. 这篇关于Grails 2.5.1(hibernate 3)标准多个连接到同一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! ProductData作为pd2
,然后确认 pd2.something = pd.something $
class SomeDomain {
static hasMany= [productData:ProductData]
}
SomeDomain.createCriteria.list {
somedata.each { type, value ->
productData {
eq("type", type)
eq("value", value)
}
}
}
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')
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])
String query="from someDomain sd join productData pd join pd.types tp where tp.id in (:types) and pd.value in (:values) "
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"
ProductData as pd2
and then confirming where pd2.something = pd.something