Grails 应用程序在域 class 构造时抛出 ConversionNotSupportedException

Grails app throws ConversionNotSupportedException at domain class construction

我正在构建一个 Grails 3.2.3 应用程序,其域 class 定义如下:

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 是其他域 class 具有 belongsToEntity 的引用,建模一个对多关系。

然后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)只是简单的保存。

运行 我得到以下堆栈跟踪的测试:

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

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

您在某处配置了一个名为 contextAttributes 的 Spring bean,它与您的域 class 中名为 contextAttributes 的 属性 冲突。我建议您在 class 级别禁用自动装配:

static mapping = {
    ...
    autowire false
}

或在 grails-app/conf/application.groovy 全局:

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