我有 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]
}
}
我得到了一个结果
[{"class":"ua.home.testknowledge.Question","id":6,"answers":[],"question":"2+2=?"},...]
但结果我得到了来自实体答案的数据的答案
在java中很容易做到,但是如何用grails做我找不到解决方案。
我想你明白我的意思。
谢谢!