我正在使用 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']
}
标记类
class Tag {
String name
static constraints = {
name(blank: false, maxSize: 40)
name(unique: true)
}
}
QuestionTag 类
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)
}
Q.1如何加载“问题”及其“标签”集?如果有 5 个标签与问题相关联。
现在我安装了“searchable”插件,我将“static searchable=true”应用于所有三个类。但是当我将该属性添加到 QuestionTag 类时出现编译错误,这与缺少“hasMany”有关,
No converter defined for type [com.app.Question]
Q.2如果我添加“hasMany”,它会在后台生成另一个表,但我已经定义了自己的表。还是它会引用我制作的 QuestionTag 表?
Q.3使用 3NF 无论如何我都可以搜索标签和问题全文,然后返回与匹配标签或文本的搜索关键字相关的问题。