简体   繁体   中英

Grails app throws ConversionNotSupportedException at domain class construction

I'm building a Grails 3.2.3 application with a domain class defined like below:

class Entity {

    String start
    String arrival
    Date date
    String sessionId
    String serviceName
    Integer serviceVersion
    Integer nodeNumber
    Integer execTime
    Integer contextExecTime

    static hasMany = [
        contextAttributes: ContextAttribute,
        conditions: Condition,
        tasks: Task
    ]

    static constraints = {
        contextAttributes nullable: true
        conditions nullable: true
        tasks nullable: true
    }

    static mapping = {
        contextAttributes defaultValue: null
        conditions defaultValue: null
        tasks defaultValue: null
    }
}

ContextAttribute , Condition and Task are other domain classes with a belongsTo reference to Entity , modelling a one-to-many relationship.

Then EntityServiceSpec defined like this:

@TestFor(EntityService)
@Mock(Entity)
class EntityServiceSpec extends Specification {

    void "Create an entity"() {
        service.create(new Entity(
            start: "Start",
            arrival: "Arrival",
            date: new Date(0),
            sessionId: "RANDOM",
            serviceName: "RANDOM",
            serviceVersion: 1,
            nodeNumber: 1,
            execTime: 1,
            contextExecTime: 1))
        expect:
        Entity.count() == 1
    }
}

Method EntityService.create(Entity entity) does simply a save.

Running the test I get following stacktrace:

org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type [java.util.Collections$UnmodifiableMap] to required type [java.util.Set] for property 'contextAttributes'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.util.Collections$UnmodifiableMap] to required type [logmadeeasy.ContextAttribute] for property 'contextAttributes[0]': no matching editors or conversion strategy found

    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:591)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1532)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1491)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1231)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
    at org.grails.plugins.web.controllers.api.ControllersDomainBindingApi.autowire(ControllersDomainBindingApi.java:100)
    at org.grails.plugins.web.controllers.api.ControllersDomainBindingApi.initialize(ControllersDomainBindingApi.java:53)
    at logmadeeasy.EntityServiceSpec.Create an entity(EntityServiceSpec.groovy:15)
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.util.Collections$UnmodifiableMap] to required type [logmadeeasy.ContextAttribute] for property 'contextAttributes[0]': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:306)
    at org.springframework.beans.TypeConverterDelegate.convertToTypedCollection(TypeConverterDelegate.java:574)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:220)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576)
    ... 9 more

What does it mean? I'm wasting hours :(

You have a Spring bean configured somewhere called contextAttributes that is in conflict with a property named contextAttributes of your domain class. I suggest you disable autowiring either at the class level:

static mapping = {
    ...
    autowire false
}

Or in grails-app/conf/application.groovy globally:

grails.gorm.default.mapping = {
    autowire false
}

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