我有2個實體,具有一對多關係。我需要從關係實體獲得來自實體的結果。這是變巫婆之一,我嘗試從第二個實體獲取數據來自一個實體
class Question {
int id
String question
List<Answer> answers
static hasMany = [answers : Answer]
// static mappedby = [ answers: 'QuestionID' ]
static constraints = {
}
}
class Answer {
int id
String answer
boolean isCorrect
static belongsTo = Question
// static belongsTo = [ question: Question]
// Question question
static constraints = {
}
}
查詢
class QuestionController {
def index() {
def questionList = Question.findAll() as JSON
[questionList: questionList]
}
}
我得到了一個結果
[{ 「下課」 之後:「ua.home.testknowledge。 Question「,」id「:6,」answers「:[],」question「:」2 + 2 =?「},...]
但結果我得到的答案與數據從實體回答
在java中很容易做,但如何與grails做我找不到解決方案。
我想你明白我的意思。
Thx!
兩件事情需要澄清 - 1.爲什麼'int id'?如果有的話,那麼'id'的映射在哪裏? 2.爲什麼'靜態belongsTo = [問題:問題]'被評論,並沒有用於一對多? – dmahapatro
在配置中將grails.converters.json.default.deep設置爲true。 –