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.
来源:https://stackoverflow.com/questions/1814013/grails-many-to-many-without-hasmany-belongsto-instead-using-native-3nf-sear