简体   繁体   中英

Grails - CreateCriteria to Retrieve any entry that contains any element in query

Given I have this domain:

class Game {
   Set<GameType> gameType
   String name
}

And this enum:

enum Gametype {
    RHYTHM, ADVENTURE, PUZZLE, RPG, HORROR, FIGHTING, MOBA, MMO
}

I need to retrieve a list of games that contains at least one of the game types indicated in the query. I tried using this code:

def retrieveGamesThatMayBeUnderGameTypes( List<GameType> listOfGameTypes) {
   return Game.createCriteria().list(){
      'in'("gameType", listOfGameTypes)
   }
}

However, it returns a NullPointerException. Any ideas?

Just make sure your enum ( Gametype ) has a field called id . Something like:

enum Gametype {
    RHYTHM('RHYTHM'),
    ADVENTURE('ADVENTURE'),
    ....

    String id

    Gametype(String id) {
        this.id = id
    }
}

See this answer for more: Grails Enum Mapping

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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