简体   繁体   中英

GORM IdentityEnumType mapping NPE with Grails 2.5.6

I have an application that I am trying to upgrade from Grails 2.0.3 to Grails 2.5.6. Many of the domain classes map enums using IdentityEnumType:

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']
        accountType(type: IdentityEnumType, length: 1)
        ...
    }

    enum AccountType {
        INTERNAL("I", "Internal"),
        ...
        String id
        String name

        AccountType(String id, String name) { .. }
    }
}

The app throws a NPE on startup:

Caused by: org.hibernate.MappingException: Unable to instantiate custom type: org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType
    at org.hibernate.type.TypeFactory.custom(TypeFactory.java:187) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.type.TypeFactory.custom(TypeFactory.java:174) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.type.TypeFactory.byClass(TypeFactory.java:99) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.type.TypeResolver.heuristicType(TypeResolver.java:170) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:297) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.mapping.Property.isValid(Property.java:243) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:464) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.mapping.RootClass.validate(RootClass.java:235) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1362) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration.buildSessionFactory(GrailsAnnotationConfiguration.java:135) ~[grails-datastore-gorm-hibernate-3.1.4.RELEASE.jar:na]
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:793) ~[spring-orm-4.1.9.RELEASE.jar:4.1.9.RELEASE]
    at org.codehaus.groovy.grails.orm.hibernate.ConfigurableLocalSessionFactoryBean.newSessionFactory(ConfigurableLocalSessionFactoryBean.java:245) ~[grails-datastore-gorm-hibernate-3.1.4.RELEASE.jar:na]
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:715) ~[spring-orm-4.1.9.RELEASE.jar:4.1.9.RELEASE]
    at org.codehaus.groovy.grails.orm.hibernate.ConfigurableLocalSessionFactoryBean.buildSessionFactory(ConfigurableLocalSessionFactoryBean.java:191) ~[grails-datastore-gorm-hibernate-3.1.4.RELEASE.jar:na]
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:189) ~[spring-orm-4.1.9.RELEASE.jar:4.1.9.RELEASE]
    at org.codehaus.groovy.grails.orm.hibernate.ConfigurableLocalSessionFactoryBean.afterPropertiesSet(ConfigurableLocalSessionFactoryBean.java:235) ~[grails-datastore-gorm-hibernate-3.1.4.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1631) ~[spring-beans-4.1.9.RELEASE.jar:4.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568) ~[spring-beans-4.1.9.RELEASE.jar:4.1.9.RELEASE]
    ... 45 common frames omitted
Caused by: org.hibernate.MappingException: Error mapping Enum Class using IdentifierEnumType
    at org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType.setParameterValues(IdentityEnumType.java:116) ~[grails-datastore-gorm-hibernate-3.1.4.RELEASE.jar:na]
    at org.hibernate.type.TypeFactory.injectParameters(TypeFactory.java:127) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.type.TypeFactory.custom(TypeFactory.java:183) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    ... 64 common frames omitted
Caused by: java.lang.NullPointerException: null
    at org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType.setParameterValues(IdentityEnumType.java:103) ~[grails-datastore-gorm-hibernate-3.1.4.RELEASE.jar:na]
    ... 66 common frames omitted

The code us looking for an enumClass key in a null properties instance. Anyone know how to get this to work?

I am using:

plugins {... compile ":hibernate:3.6.10.19"

Thanks David

You have to specify the enum class name in a params map:

static mapping = {
    accountType(type: IdentityEnumType, length: 1, params: [enumClass: AccountType.name])
}

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