Grails应用程序在域类构造中抛出ConversionNotSupportedException

时间:2016-11-22 15:40:37

标签: grails gorm

我正在使用如下定义的域类构建Grails 3.2.3应用程序:

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
    }
}

ContextAttributeConditionTask是其他域类,belongsTo引用Entity,建模一对多关系。

然后EntityServiceSpec定义如下:

@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
    }
}

方法EntityService.create(Entity entity)只执行保存。

运行测试我得到以下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

这是什么意思?我浪费时间:(

1 个答案:

答案 0 :(得分:0)

您在某个名为contextAttributes的地方配置了一个与您的域类名为contextAttributes的属性冲突的Spring bean。我建议您在班级禁用自动装配:

static mapping = {
    ...
    autowire false
}

或全球grails-app/conf/application.groovy

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