2016-07-08 13 views
1

Ich habe Domains wie in Grails:Wie kann ich eine specifc SQL in Grails/GORM/Hibernate ausführen

class Questao { 

    static transients = [ "tags" ] 

    String enunciado 
    Double valorQuestao 
    byte[] imagem 
    public enum Tipo { 
     ALTERNATIVAS, 
     VF, 
     SUBJETIVA 
    } 

    Tipo tipoQuestao 

    static hasMany = [alternativas:Alternativas, assuntos: QuestaoAssunto, provas: Prova] 

    static belongsTo = [Prova] 

    static mapping = { enunciado type: 'text'} 

    static constraints = { 
     imagem nullable: true, maxSize: 160384 
     alternativas nullable: true 
    } 
} 

class QuestaoAssunto { 

    Questao questao 
    Assunto assunto 

    static belongsTo = [Questao,Assunto] 

} 
class Assunto { 

    String titulo 

    static hasMany = [questoes:QuestaoAssunto] 

    static belongsTo = [Questao] 
} 

und ich brauche diese SQL auszuführen:

select q.* from questao_assunto qa join questao q on q.id=qa.questao_id where assunto_id in (:assuntos_id) and q.tipo_questao = 'SUBJETIVA' GROUP BY q.id order by rand() limit 1; 

: assuntos_id eine ist Array wie [5,6]

wie kann man das machen ??

Antwort

1

Hier ist ein Beispiel für die Ausführung von SQL von einem Dienst. Der gleiche Ansatz kann in einem Controller verwendet werden:

import groovy.sql.Sql 
import groovy.sql.GroovyRowResult 

class SomeService { 

    // Reference to default datasource. 
    def dataSource 

    List<GroovyRowResult> executeQuery(assuntosId) { 

     final String query = '''\ 
       select q.* 
       from questao_assunto qa 
       join questao q on q.id=qa.questao_id 
       where assunto_id in (:assuntos_id) 
       and q.tipo_questao = 'SUBJETIVA' 
       group by q.id 
       order by rand() 
       limit 1 
     ''' 

     // Create new Groovy SQL instance with injected DataSource. 
     final Sql sql = new Sql(dataSource) 

     final results = sql.rows(query, assuntos_id: assuntosId) 
     results 
    }  
} 
1

Vielleicht haben Sie einen Grund haben die Join-Tabelle selbst zu definieren, aber mein Ansatz wäre:

class Questao { 

    static transients = [ "tags" ] 

    String enunciado 
    Double valorQuestao 
    byte[] imagem 
    public enum Tipo { ALTERNATIVAS, VF, SUBJETIVA } 

    Tipo tipoQuestao 

    static hasMany = [assuntos: Assunto] 

    static mapping = { 
     enunciado type: 'text' 
     assuntos joinTable:[name:"questao_assunto", key:'assunto_id' ] 
    } 

    static constraints = { 
     imagem nullable: true, maxSize: 160384 
    } 
} 

class Assunto { 

    String titulo 

    static hasMany = [questoes:Questao] 

    static belongsTo = [Questao] 

    static mapping = { 
     questoes joinTable:[name:"questao_assunto", key:'questao_id' ] 
    } 
} 

und die SQL auszuführen, ich würde es wie die folgenden versuchen:

def records = Questao.findAll { 
    tipoQuestao == Questao.Tipo.SUBJETIVA 
    && assuntos { id in [1 as long,2 as long,3 as long] } 
} 

oder wenn Sie mit dem 1 bestellen und Limit mögen

def records = Questao.find(order: 'xxx') { ... }