【问题标题】:MappingException when migrating from Hibernate 3 / Grails 2.2.4 to Hiberate 5 / Grails 3.2.4从 Hibernate 3 / Grails 2.2.4 迁移到 Hiberate 5 / Grails 3.2.4 时出现 MappingException
【发布时间】:2017-05-28 01:31:03
【问题描述】:

我有一个使用 grails 2.2.4 和 hibernate 3 的项目。我正在将它迁移到使用 hibernate 5 的 grails 3.2.4。我从头开始创建一个 grails 3.2.4 项目,现在我是慢慢地从旧项目中复制一小部分代码并使其正常工作。起初,我只复制了我的 User、UserRole 和 Role 类,并让我的整个 Spring Security 登录流程正常工作。然后,我复制了剩余的域对象并尝试启动我的项目。我的域对象使用 GORM 映射,而不是 JPA 注释。该项目现在甚至不会启动,并且在启动时会出现以下异常(我链接到一个公共要点,因为内容对于堆栈溢出允许的内容来说太长了)。

https://gist.github.com/schmickie/10522d3a2b8a66b6fb79f76e2af0fd72

我在网上搜索了一些错误,我能找到的所有内容都表明它与设置复合主键的问题有关。但是,我没有使用任何复合主键。有人知道出了什么问题吗?错误中引用的领域对象,Api 和 Method,如下所示。

class Api implements Serializable {

    String name
    boolean enabled
    String apiVersion
    String swaggerVersion
    String resourcePath
    String jsonData
    boolean https = true
    String regionSource
    String contractName
    String contractBasePath

    Date dateCreated
    Date lastUpdated
    Date deprecationDate

    static hasMany = [methods: Method, models: Model, apiServers: ApiServer]

    static mapping = {
        cache true
    }

    static constraints = {
        name(nullable: false, blank: false)
        enabled()
        apiVersion(nullable: true, blank:  true)
        resourcePath(nullable: true, blank: true)
        swaggerVersion(nullable: true, blank:  true)
        jsonData(type: 'text', nullable: true, blank: true)
        dateCreated()
        lastUpdated()
        deprecationDate(nullable: true)
        regionSource(nullable: true)
        contractName(nullable: true)
        contractBasePath(nullable: true)
    }

    public Method getMethod(String name) {
        for (Method method : methods) {
            if (method.name.equals(name)) {
                return method
            }
        }
        return null
    }
}

class Method implements Serializable, Comparable<Method> {

    String name
    boolean enabled
    String edgePath
    HttpMethodEnum edgeHttpMethod
    String servicePath
    HttpMethodEnum serviceHttpMethod
    String summary
    String notes
    boolean deprecatedMethod
    String responseClass
    SortedSet<EdgeParameter> edgeParameters
    SortedSet<ServiceParameter> serviceParameters
    Date dateCreated
    Date lastUpdated
    String publicResponseTransformScript
    String baseResponseTransformScript
    String regionFieldName
    String platformFieldName
    String apiKeyFieldName
    String uriTransformScript
    long cacheExpiry

    static belongsTo = [api: Api]
    static hasMany = [edgeParameters: EdgeParameter, serviceParameters: ServiceParameter, errorResponses: ApiErrorResponse]


    static mapping = {
        cache true
        errorResponses sort: 'code', order: "asc"
    }

    static constraints = {
        name(blank: false)
        enabled()
        edgePath(nullable: false, blank: false)
        edgeHttpMethod(nullable: false, blank: false)
        servicePath(nullable: false, blank: false)
        serviceHttpMethod(nullable: false, blank: false)
        summary(nullable: false, blank: false)
        notes(nullable: true, blank: true)
        deprecatedMethod()
        responseClass(nullable: true, blank: true)
        publicResponseTransformScript(nullable: true)
        baseResponseTransformScript(nullable: true)
        regionFieldName(nullable: true)
        platformFieldName(nullable: true)
        apiKeyFieldName(nullable: true)
        uriTransformScript(nullable: true)
        cacheExpiry(nullable: false, blank: true)
    }
}

【问题讨论】:

  • 该问题与 Error created bean with name 'springSecurityUiInterceptor.如果是这个弹簧安全和弹簧安全 ui 插件,你可以禁用 ui 插件作为测试吗?
  • 是什么让您认为它与 springSecurityUiInterceptor bean 有关系?顺便说一句,在弄清楚之后,我已经在下面发布并接受了我自己的答案。我向grails项目提交了一个PR,让错误信息更清晰,已经合并了。
  • 我认为这是该行的要点链接上的错误日志:01/12/2017 11:17:37 PST WARN main AbstractApplicationContext:550 - 错误从 springSecurityUiInterceptor 开始..但是这显然是一个仓促的答案,可能有点像在安装了 spring security 之后,任何错误返回似乎都与 spring security 相互关联,初学者可能会认为这是 spring security 当它是其他问题但现在嵌入在 spring 中时安全

标签: hibernate grails grails-orm


【解决方案1】:

好吧,我想通了。我在 hibernate/GORM 初始化代码中做了很多单步调试。我发现问题实际上与同一类中的 unrelated 映射有关。在 Method 类中,上面显示了另一个映射,称为 errorResponses。当调用 DefaultGrailsDomainClass.establishRelationshipForCollection 方法建立 Api 和 Method 类之间的关系时,它会调用另一个方法 GrailsClassUtils.getPropertiesOfType。此类迭代 Method 类的所有属性并尝试查找其类型与 api 关联的类型匹配的任何属性。下面是方法实现:

public static PropertyDescriptor[] getPropertiesOfType(Class<?> clazz, Class<?> propertyType) {
    if (clazz == null || propertyType == null) {
        return new PropertyDescriptor[0];
    }

    Set<PropertyDescriptor> properties = new HashSet<PropertyDescriptor>();
    try {
        for (PropertyDescriptor descriptor : BeanUtils.getPropertyDescriptors(clazz)) {
            Class<?> currentPropertyType = descriptor.getPropertyType();
            if (isTypeInstanceOfPropertyType(propertyType, currentPropertyType)) {
                properties.add(descriptor);
            }
        }
    }
    catch (Exception e) {
        // if there are any errors in instantiating just return null for the moment
        return new PropertyDescriptor[0];
    }
    return properties.toArray(new PropertyDescriptor[properties.size()]);
}

它找到的第一个属性是api 属性,它通过isTypeInstanceOfPropertyTypecheck 并被添加到properties 集合中。由于名为getApiErrorResponse 的辅助方法命名不当,对BeanUtils.getPropertyDescriptors(clazz) 的调用在返回的集合中包含了一个名为apiErrorResponse 的字段。但是,我的 Method 类中没有这样的字段(有一个名为 errorResponses 的映射关联,但没有名为 apiErrorResponse 的映射关联)。因此,对于这个不存在的字段,PropertyDescriptor 的propertyType 为空。然后,当使用 null 的 currentPropertyType 值调用 isTypeInstanceOfPropertyType 时,它会抛出 NullPointerException。这导致getPropertiesOfType 方法返回new PropertyDescriptor[0],如catch 子句中所示。稍后,未能返回映射到api 的属性描述符会导致为关联生成不正确的 OneToOne 映射类型,并且该映射也缺少映射字段。

基本上,这是 grails 代码没有提供足够信息来说明真正出错的情况。它默默地抑制了另一个字段上发生的 NullPointerException,而另一个字段上的错误导致了 api 字段的不正确初始化。这意味着后来的错误消息被错误地抱怨,因为假定信息/配置没有丢失,导致工程师非常困惑。

我的最终解决方法是将 getApiErrorResponse 辅助方法从 Method 类中移出到 MethodService 中。请注意,在我从中迁移的旧 Grails 项目(带有 Hiberate 3 的 Grails 2.2.4)中,在我的域对象中使用此辅助方法可以正常工作,因此它可能是最新版本的 Grails/GORM/Spring/Groovy 中的新行为假设所有 get* 方法都映射到字段。我不知道其中哪一个是罪魁祸首,但它们都在比我的旧项目更新的版本上。

我还向 grails-core 提交了一个 PR,以便在发生此类异常时记录有用的错误消息,而不是默默地吞下它们。

https://github.com/grails/grails-core/pull/10400

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 2012-12-04
    • 2016-11-23
    • 1970-01-01
    • 2017-01-08
    相关资源
    最近更新 更多