2

如何使用enumType:'identity'为Set定义映射?

在 Grails 3.3 中 enumType 成为具有 id 的枚举的强制要求(对于 3.2,它无需任何额外定义即可工作)。

使用枚举字段一切正常,但我不知道如何为枚举集编写映射

class Test {
   Set<TestEnum> enums
   static mapping {
     enums  enumType: 'identity' // not works 
   }
}

enum TestEnum {
   final int id
   TestEnum(int value){
   ...
   }
}

有任何想法吗?

我知道,我可以使用 enumType:'string'。不适合我

4

1 回答 1

1

作为一种解决方法,您可以创建一个包装实体:

class EnumWrapper {
    TestEnum testEnum
    static mapping {
        testEnum enumType: 'identity'
    }
    static belongsTo = [test: Test]
}

class Test {
   static hasMany = [enumWrappers: EnumWrapper]
}

enum TestEnum {
    final int id
    TestEnum(int value){
    ...
    }
}
于 2018-05-09T12:57:52.697 回答