1

学生向けのテストアプリケーションを書きたいです。そのため、クラスには 2 つのタイプがあります。Question多くAnswerの正解が含まれています。だから私は一対多と一対一の双方向を持っています。

class Question extends Entity {

    static hasOne   = [ acceptedAnswer: Answer ]

    static hasMany  = [ answers: Answer ]
    static mappedby = [ answers: 'parentQuestion' ]

    static constraints = {
        acceptedAnswer unique: true
    }
}

class Answer  extends Entity {

    Question accesptedInQuestion

    //one of many answers
    static belongsTo = [ parentQuestion: Question] // when ANSWER bidirectional

    static constraints = {
    }
}

抽象エンティティは次のとおりです。

package com.medreactor.content.model

import org.bson.types.ObjectId

abstract class Entity {

    ObjectId post_id
    String posType  // Question OR ANSWER

    static mapping = {
        id column: 'post_id'
    }
}

エラーが発生し続けます:

grails> run-app 
| Running Grails application
| Error 2013-09-05 10:30:50,805 [localhost-startStop-1] ERROR context.ContextLoader  - Context initialization failed
Message: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [answers] in class [class com.medreactor.content.model.Question] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [question] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [answers:'myprop']

なにが問題ですか?答えをマッピングしているのに、コンパイラがこれを認識しないのはなぜですか? 何か不足していますか?

4

2 に答える 2

0

Try to like this

Question.groovy

    class Question extends Entity{
         Answer acceptedAnswer
         static hasMany = [answers: Answer]
         static mappedBy = [answers: "parentQuestion"]
         static mapping = {
            answers cascade: 'all-delete-orphan'
         }
         static constraints = {
            acceptedAnswer unique: true
         }
    }

Answers.groovy

class Answer extends Entity {
    Question acceptedInQuestion
    Question parentQuestion

    static constraints = {
        acceptedInQuestion unique: true
    }
}
于 2013-09-05T10:14:03.300 に答える
0

1. 修正する最初のケースでは、質問インスタンスとインスタンスに同じ名前を使用したため、名前を変更する必要がありますか? 一対多では、読みやすさのためにも質問を参照する必要があります

class Answer extends Entity{

        Question question   

        //one of many answers
        static belongsTo = [ questions:Question] // when ANSWER bidirectional // renamed

        static constraints = {
        }
    }

2番目

class Question extends Entity {

    /*static hasOne   = [ acceptedAnswer: Answer ] you dont need this , you already said belongs to */
    Answer acceptedAnswer;

    static hasMany  = [ answers: Answer ]
    static mappedby = [ answers: 'parentQuestion' ] //parentQuestion Table

    static constraints = {
        acceptedAnswer unique: true
    }
}

3rd .これを試して、いくつかの信用を与えてください、ありがとう!

于 2013-09-05T12:16:07.550 に答える