2015-04-03 120 views
3

下個createCriteria多個項目不用於搜索多個標籤工作:中的hasMany關係

def tags = "1,2,3".split(",") 

def results = Item.createCriteria().list() { 
    itemTags { 
    and { 
     tags.each { tag -> 
     like("name", tag) 
     } 
    } 
    } 
} 

但它似乎工作,如果我改變

編輯:在我調試,我發現的標準是:

(itemTags_alias1.name=1 and itemTags_alias1.name=2 and itemTags_alias1.name=3) 

這是不是我的目標來完成。我想檢查一個項目是否有全部三個標籤。

回答

0

我不知道你是否能有這種情況下的標準,但你應該能夠類似HQL寫下面

Item.executeQuery("select i from Item i join i.itemTags tags where tags.name in (:names) group by i having count(i) >= :count", [names:nameList, count: nameList.size()]) 

看到這個question - 你會得到怎樣可以做到這一點的想法在SQL中,所以你可以將它轉換爲hql

注:以上hql查詢未經測試,但會給你想法