本文介绍了如何处理Grails中的Many-to-Many而不归属?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Grails中创建一个多对多的关系。



我有一个问题域和一个标记域。
一个问题可以有0个或多个标签。标签可以有0个或更多个问题。



如果我在每边放置了一个hasMany,它会给我一个错误,说我需要某个地方的belongsTo。
然而,添加一个belongsTo意味着所有者必须存在...



就像我说的,标签可以有0个问题,而一个问题可以有0个标签。
没有所有者的概念,它是多对多的!



我该怎么做? 你可以做这(请参阅下面的代码)。但是问问题和标签问题都有问题吗?

 包m2msansbt 

类问题{
字符串toString(){return name}
字符串名称
静态hasMany = [questionTags:QuestionTag]
静态约束= {
}
}
包m2msansbt

类标记{
String toString(){返回名称}
字符串名称
static hasMany = [questionTags:QuestionTag]
static constraints = {
}
}
package m2msansbt

class QuestionTag {
问题
标记标记
static QuestionTag链接(问题问题,标记标记){
QuestionTag questionTag = QuestionTag.findByQuestionAndTag(question,tag)
if(!questionTag){
questionTag = new QuestionTag()
question?.addToQuestionTags(questionTag)
tag?.addToQuestionTags(questionTag )
questionTag.save()

}
return questionTag
}
static void unlink(Question question,Tag tag){
QuestionTag questionTag = QuestionTag.findByQuestionAndTag(question,tag)
if(questionTag) {
question?.removeFromQuestionTags(questionTag)
tag?.removeFromQuestionTags(questionTag)
questionTag.delete()
}
}
static constraints = {
}
}

import m2msansbt。*
class BootStrap {

def init = {servletContext - >
问题q1 =新问题(名称:'q1')
标签t1 =新标签(名称:'t1')
标签t2 =新标签(名称:'t2')
q1.save()
t1.save()
t2.save()
QuestionTag q1t1 = QuestionTag.link(q1,t1)
q1t1.save()
QuestionTag q1t2 = QuestionTag.link(q1,t2)
q1t2.save()
q1.save()
t1.save()
}
def destroy = {
}
}


I need to create a many-to-many relationship in Grails.

I have a "Question" domain and a "Tag" domain. A Question can have 0 or more tags. A Tag can have 0 or more Questions.

If I put a "hasMany" on each sides, it gives me an error saying I need a "belongTo" somewhere. However, adding a belongsTo means that the owner must exist...

Like I said, a Tag could have 0 questions, and a Question could have 0 tags. There is no concept of an owner, it's a many-to-many!

What am I supposed to do?

解决方案

you can do this (please see the code below). but does it make sense to have a question tag with out both a question and a tag?

    package m2msansbt

    class Question {
        String toString() { return name }
        String name
        static hasMany=[questionTags:QuestionTag]
        static constraints = {
        }
    }
    package m2msansbt

    class Tag {
        String toString() { return name }
        String name
        static hasMany=[questionTags:QuestionTag]
        static constraints = {
        }
    }
package m2msansbt

class QuestionTag {
    Question question
    Tag tag
    static QuestionTag link(Question question,Tag tag) {
        QuestionTag questionTag=QuestionTag.findByQuestionAndTag(question,tag)
        if (!questionTag) {
            questionTag = new QuestionTag()
            question?.addToQuestionTags(questionTag)
            tag?.addToQuestionTags(questionTag)
            questionTag.save() 

        }
        return questionTag
    }
    static void unlink(Question question,Tag tag) {
        QuestionTag questionTag=QuestionTag.findByQuestionAndTag(question,tag)
        if (questionTag) {
            question?.removeFromQuestionTags(questionTag)
            tag?.removeFromQuestionTags(questionTag)
            questionTag.delete()
        }
    }
    static constraints = {
    }
}

 import m2msansbt.*
 class BootStrap {

         def init = { servletContext ->
            Question q1=new Question(name:'q1')
            Tag t1=new Tag(name:'t1')
            Tag t2=new Tag(name:'t2')
            q1.save()
            t1.save()
            t2.save()
            QuestionTag q1t1=QuestionTag.link(q1,t1)
            q1t1.save()
            QuestionTag q1t2=QuestionTag.link(q1,t2)
            q1t2.save()
            q1.save()
            t1.save()
         }
         def destroy = {
         }
    }

这篇关于如何处理Grails中的Many-to-Many而不归属?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:13