【问题标题】:One-to-many and one-to-one grails 2.2.4一对多和一对一 grails 2.2.4
【发布时间】:2013-09-05 08:32:21
【问题描述】:

我想为学生编写一个测试应用程序。所以有两种类型的类。 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']

出了什么问题?我正在映射答案,为什么编译器看不到这个?我错过了什么吗?

【问题讨论】:

    标签: grails one-to-many one-to-one


    【解决方案1】:

    1.您第一个解决问题的案例和实例使用了相同的名称,所以您必须重命名它?在一对多的情况下,即使为了可读性,它也必须参考问题

    class Answer extends Entity{
    
            Question question   
    
            //one of many answers
            static belongsTo = [ questions:Question] // when ANSWER bidirectional // renamed
    
            static constraints = {
            }
        }
    

    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 .try this 并给予一些信任,谢谢!

    【讨论】:

    • 您的代码中的parentQuestion 是什么? belongsTo 只指向关系的所有者。 hasOne 给了我第二个方向,Answer 给了Question 方向,对吗?
    • parentQuestion 是答案表的多方面参考,但映射到 parentQuestion ?对吗?
    【解决方案2】:

    试着喜欢这个

    问题.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
        }
    }
    

    【讨论】:

    • 您编写的第一个代码不会更改Answer 中的任何内容。第二个采取一对一的双向关系。从Answer 将不会返回到Question,因为现在可以检查此答案是否被接受。
    • 从回答方你想接受问题啊?你能告诉你的要求吗?
    • 我在第一个代码中更改了 Answer 类。出现错误是因为您的答案类维护了两个问题对象,例如 belongsTo 子句中的 parentQuestion 和一对一的接受问题。
    • 在您的 Answer 类中,您已接受 Qustion(一对一关系)和 parentQuestion(belongsTo 子句)。所以这个错误来了。在为问题添加答案时,它会混淆要引用存储问题 ID 的列。所以我只维护了一个问题列,即问题对象(一对一和belongsTo)
    • 我已经添加了一对一来强制只接受一个接受的问题答案。布尔值将强制搜索所有答案以找到接受的答案。好的,现在我看到您在第一个代码中进行了哪些更改,但是如何解释它?我在Answer 中有两个qustions,但我在Quesion 中放置了映射
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    相关资源
    最近更新 更多