grails - Grails应用程序在域类构建时抛出ConversionNotSupportedException

标签 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是其他域类,它们具有对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,它与域类的名为contextAttributes的属性冲突。我建议您在类级别禁用 Autowiring :

static mapping = {
    ...
    autowire false
}

或全局grails-app/conf/application.groovy中:
grails.gorm.default.mapping = {
    autowire false
}

关于grails - Grails应用程序在域类构建时抛出ConversionNotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40746006/

相关文章:

spring - @PostConstruct 在 Grails 服务上默默失败

grails - Grails 2.0文档-如何打印出来?

grails - grails executeUpdate集成测试中的刷新错误

mongodb - 尝试使用Grails 2.3.7查询MongoDB域类时出现IllegalStateException

grails - 基于嵌入式日期字段的Grais列表不起作用

grails - 绕过 Grails View 层将二进制内容直接写入客户端

grails - 如何从源代码安装Grails插件?

grails - 向List的元素添加约束

hibernate - 没有正确的引用->删除问题

grails - 将具有预定义值的域类映射到表