Relacionamento oneToMany sendo ignorado
02/06/2012 16:20
class Pesquisa {
String guid
Tenant tenant
String titulo
Relatorio relatorio
String mapeamentos
static constraints = {
}
static hasMany = [
questoes: Questao,
questionarios: Questionario
]
static mapping = {
questoes lazy: false
questoes sort: 'pos', order: 'asc'
mapeamentos type: 'materialized_clob'
}
def novaQuestao(config) {
def questao = new Questao()
questao.pesquisa = this
if (!questoes)
questoes = []
questao.pos = questoes.size()
questoes.add(questao)
config(questao)
return questao
}
}
class Questao {
static constraints = {
}
static hasMany = [
opcoes: Opcao
]
static belongsTo = [
pesquisa: Pesquisa
]
static mapping = {
opcoes sort: 'pos', order: 'asc'
opcoes lazy: false
}
int pos
String tituloRelatorios
String questao
String tituloFormulario
TipoQuestao tipo
def void setOpcoes(list) {
def pos = 0
def questao = this
opcoes = []
list.each { textoOpcao ->
def opcao = new Opcao()
opcao.pos = pos++
opcao.questao = questao
opcao.opcao = textoOpcao
opcoes << opcao
}
}
}
class Opcao {
static constraints = {
}
static belongsTo = [
questao: Questao
]
int pos
String opcao
}
def pesquisa = new Pesquisa(...)
def sexo = pesquisa.novaQuestao {
it.questao = 'Sexo'
....
it.setOpcoes(['Masculino', 'Feminino'])
}
pesquisa.save(failOnError: true)
Para se registrar, clique aqui.