Grails 2.5.6中IdentityEnumType映射触发NPE问题求助
解决Grails 2.5.6中IdentityEnumType映射枚举的NullPointerException问题
问题原因
Grails 2.5.x配套的Hibernate 3.6.x插件中,IdentityEnumType的初始化逻辑已变更,无法再自动推断关联的枚举类。若未显式指定枚举类信息,会在初始化时因properties实例为null,查找enumClass键时抛出NullPointerException。
解决方法
修改域类的映射配置,为使用IdentityEnumType的字段添加enumClass参数,明确指定对应的枚举类型:
import org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType class Account { Long id AccountType accountType static mapping = { id generator: 'sequence', params: [sequence: 'account_id_seq'] // 新增enumClass参数指定枚举类 accountType(type: IdentityEnumType, length: 1, enumClass: AccountType) } enum AccountType { INTERNAL("I", "Internal") // 其他枚举项... String id String name AccountType(String id, String name) { this.id = id this.name = name } } }
注意事项
- 所有使用
IdentityEnumType映射的枚举字段都需要添加enumClass参数,若枚举是外部类,需填写完整包路径(如enumClass: com.example.enums.AccountType) - 验证枚举类的构造方法和
id属性是否正常,确保与数据库存储的枚举值匹配
内容的提问来源于stack exchange,提问作者David Tinker
相关产品推荐
相关产品推荐

