将SQL查询映射到GORM查询

时间:2015-03-27 18:54:15

标签: sql grails gorm

我在GORM查询的SQL查询下面的映射是否正确?我将不胜感激。另外,为什么createAlias函数中没有任何选项来指定我们希望LEFT JOIN发生的条件?

SQL查询:

select tg.geck_id as geck_id, min(Fault.id) AS id, Trace.frame_number,
Module.module, Symbol.symbol, Report.email as email, Fault.version, 
Fault.short_os,Fault.fault_date as fault_date from Report, Trace, Module,  
Symbol, Fault LEFT JOIN TraceGroup16Map tgmap ON tgmap.fault_id = Fault.id 
LEFT JOIN TraceGroup16 tg ON tg.id = tgmap.group_id where Report.id = 
Fault.report_id AND Fault.id = Trace.fault_id AND Trace.symbol_id IN (select 
id from Symbol where symbol like '%mysymbol%') AND Trace.module_id IN 
(select id from Module where module like '%mymodule%') AND Trace.module_id = 
Module.id AND Trace.symbol_id = Symbol.id group by Fault.pid, Report.file 
ORDER BY Fault.fault_date DESC

GORM查询:

def trace = Trace.createCriteria()
    def results = trace.list(max:max, offset:offset) {      
        createAlias('module','mod', CriteriaSpecification.LEFT_JOIN)
        createAlias('symbol','sym', CriteriaSpecification.LEFT_JOIN)
        createAlias('fault', 'fault',CriteriaSpecification.LEFT_JOIN)
        createAlias('fault.report', 'report', CriteriaSpecification.LEFT_JOIN)
        createAlias('fault.tgmap', 'tg', CriteriaSpecification.LEFT_JOIN)
        createAlias('tg.traceGroup16','tr', CriteriaSpecification.LEFT_JOIN)        
        projections
        {
            property('fault.id')
            property('tr.geckId')
            property('report.email')
            property('fault.ver')
            property('fault.shortOs')
            property('fault.faultDate')
            property('frameNumber')
            property('mod.module')
            property('sym.symbol')
            groupProperty 'fault.pid'
            groupProperty 'report.file'
        }
        // Handle Unknown module case
        if (module.length() > 0 && symbol.length() > 0 && module != symbol)
        {
            and
            {
                like('mod.module', '%' + mymodule + '%')
                like('sym.symbol', '%' + mysymbol + '%')
            }

        }
        order("fault.faultDate", "desc")
    }

1 个答案:

答案 0 :(得分:1)

验证这一点的最简单方法是打开Hibernate的SQL日志记录并检查Hibernate吐出的SQL。

Reference

或者,如果您正在使用mysql,我更喜欢的解决方案是打开查询日志记录并拖尾查询日志。这样做的好处是你可以在查询中看到查询参数(而不是寻找?)

Reference