在hasMany上使用不同条件的grails createCriteria()

时间:2015-04-10 12:53:27

标签: grails gorm createcriteria

我想查询所有InvItems,它有多个属性键/值对,应该满足我的where条件。

class InvItem{
  static hasMany = [ attributes : InvAttributes ]
}

class invAttributes{
  String key
  String valueShort

  static belongsTo = [ invItem : InvItem ]
}

让我们在sql中说:(这就是我在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%')

我尝试了一个createCriteria来实现多个属性的出现:

ciRequest是包含keyvalueShort

的属性列表
    def criteria = InvItem.createCriteria()
    def result = criteria.list {
        ciRequest.attributes.each { req ->
             attributes {
                eq('key', "${req.key}")
                like('valueShort', '%' + "${req.value.trim().toLowerCase()}" + '%')
            }
        }
    }

但结果是:

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 ?)

如何管理它,InvAttribute表上会有第二个,第三个,多个连接,以便它可以检查所有条件?

0 个答案:

没有答案