Grails GORM class with @GrailsCompileStatic annotation shows in the static mapping closure table, version, autoTimestamp as unresolved symbol

烈酒焚心 提交于 2020-07-15 09:26:46

问题


References:

  1. https://youtrack.jetbrains.com/issue/IDEA-206196
  2. https://youtrack.jetbrains.com/issue/IDEA-207415?_ga=2.103698112.1724644326.1581075934-247190714.1566820331
package de.equeo.requestcode

import grails.compiler.GrailsCompileStatic

@GrailsCompileStatic
class Feature {

    String name

    static mapping = {
        table 'rq_feature'
        version false
        autoTimestamp false
    }

}

This results in the error in the IDE only (works fine in grails run-app or while compiling):


回答1:


Instead of doing that you can use the built in type safe dsl added in GORM 6.1

import static grails.gorm.hibernate.mapping.MappingBuilder.*

class Book {
    String title

    static final mapping = orm {



回答2:


I never spent much time before to figure out a workaround for this, but now I have a workaround.

First Workaround (Recommended)

As @JamesKleeh answered, you even don't need to define the above method for type-safe DSL:

package com.wizpanda.hooman

import grails.compiler.GrailsCompileStatic

import static grails.gorm.hibernate.mapping.MappingBuilder.orm

@GrailsCompileStatic
class User {

    String firstName
    String lastName
    String bio
    String email

    static final mapping = orm {
        table "rq_feature"
        version false
        autoTimestamp false
        property("bio", [type: "text"])
        property("firstName", {
            column([name: "fn"])
        })
    }

}

Second Workaround

I used my own logic of this from https://github.com/wizpanda/kernel/blob/v2.1.6/src/main/groovy/com/wizpanda/logging/KernelLogging.groovy#L63 and created a static method applyFooMapping which uses @DelegatesTo annotation to fool the IDE 😁

import grails.compiler.GrailsCompileStatic
import org.grails.orm.hibernate.cfg.HibernateMappingBuilder

@GrailsCompileStatic
class Feature {

    String name

    /**
     * This is to solve the IntelliJ Idea problem as defined
     * @param delegate
     * @param closure
     * @return
     */
    static applyFooMapping(Object delegate, @DelegatesTo(HibernateMappingBuilder) Closure closure) {
        closure.delegate = delegate
        closure.resolveStrategy = Closure.DELEGATE_ONLY
        closure.call()
    }
    
    static mapping = {
        applyFooMapping(delegate) {
            table 'rq_feature'
            version false
            autoTimestamp false
        }
    }

}

Cheers!

Third Workaround (Improvement to 2nd)

To solve this problem for multiple domains, create a groovy class in src/main/groovy/some/package/AbstractFooDomain:

@GrailsCompileStatic
abstract class AbstractFooDomain {

    /**
     * This is to solve the IntelliJ Idea problem as defined
     * @param delegate
     * @param closure
     * @return
     */
    static applyFooMapping(Object delegate, @DelegatesTo(HibernateMappingBuilder) Closure closure) {
        closure.delegate = delegate
        closure.resolveStrategy = Closure.DELEGATE_ONLY
        closure.call()
    }
}

And now, use it in your domain classes:


@GrailsCompileStatic
class Feature extends AbstractFooDomain {

    String name

    static mapping = {
        applyFooMapping(delegate) {
            table 'rq_feature'
            version false
            autoTimestamp false
        }
    }

}

Cheers again!



来源:https://stackoverflow.com/questions/60113220/grails-gorm-class-with-grailscompilestatic-annotation-shows-in-the-static-mappi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!