Grails:没有 hasMany/belongsTo 的多对多 - 而是使用本机 3NF - 搜索全文

发布于 2024-08-13 05:33:43 字数 1978 浏览 5 评论 0原文

我正在使用 3NF 在 grails 中实现多对多映射, 不使用 hasMany 或belongsTo 属性。

摘自这篇文章,它展示并解释了很多有关其优点的内容。

文章:http://burtbeckwith.com/blog/?p=169

演示文稿说明: http://burtbeckwith.com/blog/files/169/ gorm%20grails%20meetup%20presentation.pdf

我正在尝试在问题上创建一个标签系统,有点像这样(stackoverflow:))

我可以保存问题和标签,然后保存与它们的关联, 但现在我希望能够搜索并提供带有标签的完整问题,

我有 3 个域类 - Question、Tag 和 QuestionTag

class Question {

  String title
  String content
  Date createdAt
  String tags

  static transients = ['tags']

}

Tag Class

class Tag {

    String name

    static constraints = {
        name(blank: false, maxSize: 40)
        name(unique: true)
    }
}

QuestionTag Class

 class QuestionTag implements Serializable{

  Question question
  Tag tag

  static mapping = {
    table 'question_tags'
    version false
    id composite: ['question', 'tag']
  }

这些会生成 3 个表格,以 3 种标准化形式

保存工作、问题和数量标签。

def question = new Question()
question.properties = params

question.save()

def tags = question.tags

tags.split(' ')?.each { tagName ->

   Tag tag = Tag.findByName(tagName) ?: new Tag(name: tagName).save()

   QuestionTag questionTag = new QuestionTag(question: question, tag: tag)
   QuestionTag.save(flush: true) 
}

问题1 如何加载“问题”及其一组“标签”?如果有 5 个与该问题相关的标签。

现在我安装了“searchable”插件,我将“static searchable=true”应用于所有三个类。但是当我将该属性添加到 QuestionTag 类时出现编译错误, 思考与缺少“hasMany”有关,

No converter defined for type [com.app.Question]

Q.2如果我添加“hasMany”,它会在后台生成另一个表,但我已经定义了自己的表。或者它会引用我制作的 QuestionTag 表吗?

Q.3 使用 3NF 无论如何我都可以搜索标签和问题全文,然后返回与匹配标签或文本的搜索关键字关联的问题。

I am implementing a many-to-many mapping in grails using 3NF,
Not using the hasMany or belongsTo property.

Taken from this article it shows and explains quite a lot about its advantages.

Article: http://burtbeckwith.com/blog/?p=169

Presentation notes: http://burtbeckwith.com/blog/files/169/gorm%20grails%20meetup%20presentation.pdf

I'm trying to make a Tag system onto questions, kind of like this(stackoverflow :))

I can save the Question and the Tags, then save the association with them,
but now I want to be able to search and serve up a full Question with Tags,

I have 3 domain classes - Question, Tag and QuestionTag

class Question {

  String title
  String content
  Date createdAt
  String tags

  static transients = ['tags']

}

Tag Class

class Tag {

    String name

    static constraints = {
        name(blank: false, maxSize: 40)
        name(unique: true)
    }
}

QuestionTag Class

 class QuestionTag implements Serializable{

  Question question
  Tag tag

  static mapping = {
    table 'question_tags'
    version false
    id composite: ['question', 'tag']
  }

These produce 3 tables, in 3 normalized form

Saving works, a question and number of tags.

def question = new Question()
question.properties = params

question.save()

def tags = question.tags

tags.split(' ')?.each { tagName ->

   Tag tag = Tag.findByName(tagName) ?: new Tag(name: tagName).save()

   QuestionTag questionTag = new QuestionTag(question: question, tag: tag)
   QuestionTag.save(flush: true) 
}

Q.1 How can I load a "Question" along with its set of "Tags"? if there were 5 tags associated with the Question.

Now I installed the "searchable" plugin, I applied the "static searchable=true" to all three classes. but I get compile errors when I add that property to the QuestionTag class,
thinking is has to do with the lack of "hasMany",

No converter defined for type [com.app.Question]

Q.2 If I add "hasMany" will it generate another table under-the-hood, yet I have defined my own. Or will it reference my QuestionTag table that I made?

Q.3 Using the 3NF is there anyway I can search for Tags AND Question full text, then return the Questions associated with the search keywords that match Tags or text.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文