简体   繁体   English

Grails查找具有多对多关系的领域对象,并以列表作为参数

[英]Grails find domain objects with many to many relationship, with a list as parameter

I have a simple class Song, to which I can attribute some tags : 我有一个简单的Song类,可以为它添加一些标签:

class Song {
    String title
    static hasMany = [tags:Tag]
}

Then, i would like to be able to find all songs having a given list of tags, for example this would look like : 然后,我希望能够找到所有具有给定标签列表的歌曲,例如:

List<Song> results = Song.findAll("where tags contains ?", [myTagList]);

For example, if I have : 例如,如果我有:

a Song S1 with tags T1
a Song S2 with tags T1 T2 T3
a Song S3 with tags T1 T3
a Song S4 with tags T1 T2 T3 T4

And I execute my query with myTagList containing T1, T2, T3 , then the call will return S2 and S4 . 然后使用myTagList containing T1, T2, T3执行查询,然后调用将返回S2 and S4

Is there any efficient way to perform this ? 有什么有效的方法可以执行此操作吗?

It should be possible to use criteria with an "in" query: 应该可以在“ in”查询中使用条件:

Song.createCriteria().list{
    "in"("tags",myTagList)
}

那动态取景器呢?

List<Song> results = Song.findAllByTagsInList(myTagList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM