为什么Grails看到一个多对多的联想应该在哪里看到一对多?

时间:2015-07-28 03:13:12

标签: grails model associations gorm

我正在使用以下环境: - Grails 3.0.3,Groovy 2.4.3,JVM 1.8.0.31 ...全部在OSX 10.10.4上运行

使用调试器进行跟踪后,Grails似乎将一对多自我关联(父级 - >儿童)视为许多对多。结果是,当一个人试图从 children 中删除一个对象时,grails尝试通过调用remove()方法来清理关联的另一端,当然,这不是一个采集。找不到适用的remove()方法会导致Grails抛出MissingMethodException ...我已将案例分离为两级继承模型,其中 abstract 基类定义了父/子自我协会。注意,如果我将两个类折叠为单个 instantiable 类,则不会出现此问题。这是域模型:

// CLASS 1 - Abstract Base Class
package com.sandbox

abstract class AbstractBaseClass {

    static hasMany = [ children: AbstractBaseClass ]

    static belongsTo = [ parent: AbstractBaseClass ]

    static constraints = {
    }
}

// CLASS 2 - Instantiable Sub Class
package com.sandbox

class IllustrationCase extends AbstractBaseClass {

    String name

    static constraints = {
    }
}

然后,我发生异常的测试用例如下:

package com.sandbox

import grails.test.mixin.integration.Integration
import grails.transaction.*
import spock.lang.*

@Integration
@Rollback
class IllustrationCaseSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "Deleting Nodes From a Hierarchy"() {
        given:
        // Create the various nodes
        def theParent = new IllustrationCase(
            name: "Parent Node"
        )
        def theChild1 = new IllustrationCase(
            name: "Child Node 1"
        )
        def theChild2 = new IllustrationCase(
            name: "Child Node 2"
        )
        def theChild3 = new IllustrationCase(
            name: "Child Node 3"
        )

        // Create the hierarchy structure
        theParent.addToChildren(
            theChild1
        ).addToChildren(
            theChild2
        ).addToChildren(
            theChild3
        )

        // Perform a DEEP save
        theParent.save( failOnError: true )

        // Remove a child --- This is where the error occurs...
        IllustrationCase.get( theParent.id ).removeFromChildren( IllustrationCase.get( theChild2.id ) )


        expect:

        // Find the parent nodes...
        def targetNode = IllustrationCase.findByName( "Parent Node" )

        targetNode.children.size() == 2
        !targetNode.children.find { it.name == "Child Node 2" }

        targetNode.children.find { it.name == "Child Node 1" }
        targetNode.children.find { it.name == "Child Node 3" }
    }
}

执行上述测试用例会导致抛出以下异常......

groovy.lang.MissingMethodException: No signature of method: com.sandbox.IllustrationCase.remove() is applicable for argument types: (com.sandbox.IllustrationCase) values: [com.sandbox.IllustrationCase : 1]
Possible solutions: merge(com.sandbox.IllustrationCase), merge(), merge(), create(), delete(), save()
    at org.grails.orm.hibernate.AbstractHibernateGormEnhancer.addRelationshipManagementMethods_closure5(AbstractHibernateGormEnhancer.groovy:183)
    at com.sandbox.IllustrationCaseSpec.$tt__$spock_feature_0_0(IllustrationCaseSpec.groovy:46)
    at com.sandbox.IllustrationCaseSpec.Deleting Nodes From a Hierarchy_closure1(IllustrationCaseSpec.groovy)
    at groovy.lang.Closure.call(Closure.java:423)
    at groovy.lang.Closure.call(Closure.java:439)
    at grails.transaction.GrailsTransactionTemplate$1.doInTransaction(GrailsTransactionTemplate.groovy:67)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
    at grails.transaction.GrailsTransactionTemplate.executeAndRollback(GrailsTransactionTemplate.groovy:64)
    at com.sandbox.IllustrationCaseSpec.Deleting Nodes From a Hierarchy(IllustrationCaseSpec.groovy)

如果我崩溃了两个类(即,从方程中移除抽象类和继承),考虑到错误就消失了。这实际上看起来像是Grails 3中的一个潜在错误所以我打开了一个{ {3}}与Grails团队一起......

0 个答案:

没有答案