【问题标题】:How can I execute an specifc SQL in Grails/GORM/Hibernate如何在 Grails/GORM/Hibernate 中执行特定的 SQL
【发布时间】:2016-07-08 02:04:05
【问题描述】:

我在 Grails 中有类似的域:

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]
}

我需要执行这条 SQL:

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 是一个类似于 [5,6] 的数组

如何做到这一点?

【问题讨论】:

    标签: hibernate grails hql grails-orm


    【解决方案1】:

    这是一个如何从服务执行 SQL 的示例。在控制器中可以使用相同的方法:

    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
        }     
    }
    

    【讨论】:

      【解决方案2】:

      也许您有理由自己定义连接表,但我的方法是:

      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' ]
          }
      }
      

      要执行 SQL,我会尝试如下:

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

      或者如果您想订购并限制为 1

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-13
        • 1970-01-01
        • 2012-12-03
        • 2011-09-17
        • 1970-01-01
        • 2011-08-03
        相关资源
        最近更新 更多