I want to query for all InvItems, which has multiple Attributes key/values pairs, that should fullfill my where condition.
class InvItem{
static hasMany = [ attributes : InvAttributes ]
}
class invAttributes{
String key
String valueShort
static belongsTo = [ invItem : InvItem ]
}
Let's say in sql: (this is how I would do it in sql!)
SELECT
id,
FROM inv_item inv
JOIN inv_attribute att1 ON inv.id = att1.inv_item_id
JOIN inv_attribute att2 ON inv.id = att2.inv_item_id
WHERE (att1.key = 'Alias' AND att1.value_short LIKE 'foo%')
AND (att2_.key = 'DatasetId' AND att2.value_short LIKE 'bar%')
I tried a createCriteria for muliple occurence of attributes like this:
ciRequest
is a list of attributes with key
and valueShort
def criteria = InvItem.createCriteria()
def result = criteria.list {
ciRequest.attributes.each { req ->
attributes {
eq('key', "${req.key}")
like('valueShort', '%' + "${req.value.trim().toLowerCase()}" + '%')
}
}
}
but this results in :
SELECT id
FROM inv_item this_
INNER JOIN inv_attribute attributes1_ ON this_.id = attributes1_.inv_item_id
WHERE (attributes1_.key = ? AND attributes1_.value_short LIKE ?) AND
(attributes1_.key = ? AND attributes1_.value_short LIKE ?)
How can I manage it, that there will be a second, third, multiple join on the InvAttribute table, so that it can check all the conditions ?