Grails-CreateCriteria检索查询中包含任何元素的任何条目

Grails-CreateCriteria检索查询中包含任何元素的任何条目,grails,hql,criteria,Grails,Hql,Criteria,鉴于我拥有此域: class Game { Set<GameType> gameType String name } 我需要检索一个游戏列表,其中至少包含查询中指示的一种游戏类型。我尝试使用以下代码: def retrieveGamesThatMayBeUnderGameTypes( List<GameType> listOfGameTypes) { return Game.createCriteria().list(){ 'in'("ga

鉴于我拥有此域:

class Game {
   Set<GameType> gameType
   String name
}
我需要检索一个游戏列表,其中至少包含查询中指示的一种游戏类型。我尝试使用以下代码:

def retrieveGamesThatMayBeUnderGameTypes( List<GameType> listOfGameTypes) {
   return Game.createCriteria().list(){
      'in'("gameType", listOfGameTypes)
   }
}
def retrievegamesthattaybeundergametypes(类型列表){
return Game.createCriteria().list(){
“in”(“游戏类型”,游戏类型列表)
}
}
但是,它返回一个NullPointerException。有什么想法吗?

只要确保你的枚举(
Gametype
)有一个名为
id
的字段。比如:

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

    String id

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

有关更多信息,请参见此答案:

是否必须是名为
id
的字段?它可以是另一种数据类型吗?它可以是另一个域或枚举吗?需要调用
id
——这是规则,否则hibernate将不知道如何引用bean的值。
id
列可以是任何类型,而不仅仅是
String
。我明白了。但是,当前数据库没有用于枚举的表。添加id是否会导致枚举拥有自己的表?不,您不必在数据库中拥有该表,但没有任何东西可以阻止您添加该表。事实上,如果您直接查询该表或有其他系统使用该数据,最好将该表放在数据库中。它不会抛出NullPointerException,但现在返回一个空列表。
enum Gametype {
    RHYTHM('RHYTHM'),
    ADVENTURE('ADVENTURE'),
    ....

    String id

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