Home Grails GORM getting people with both tags
Reply: 1

Grails GORM getting people with both tags

Greg Zuber
1#
Greg Zuber Published in 2017-11-28 13:22:23Z

I have the classes:

class Person {
    static hasMany = [tags: Tag]
}

class Tag {
}

To get the people that have at least one of the tags from tagList I could create a criteria

public Criteria hasAtLeastOneOf(List<Tag> tagList) {
    return Person.createCriteria().buildCriteria {
        'tags' {
          'in' 'id', tagList.collect{it.id}
        }
    }
}

What kind of criteria do I need to create to get all the people that have each one of the tags from tagList (eg. tagList[0] and tagList[1], etc.)

Trebla
2#
Trebla Reply to 2017-11-28 17:48:18Z

Similar to this unanswered question:

Grails 2.5.1 (hibernate 3) criteria multiple joins to same table

In short you need multiple joins between Person and Tag, one for each tag in the list that you want to compare. As the comments on the linked issue describe, this can be done in HQL by building the joins manually, but criteria doesn't (seem to?) support this.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.310177 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO