gorm CreateCriteria中的笛卡尔积

时间:2017-02-12 17:13:52

标签: hibernate grails orm gorm

我正在尝试使用CreateCriteria执行以下sql语句,但我不知道如何使用具有不同别名的相同表。 在这里我的域名:

class Analisys {

    Integer performanceStar
    Phase phase

    static belongsTo = [instrument: Instrument]

}

class Phase {

    String name
    int showOrder

    static hasMany = [category: Category]
}
class Instrument {

    Long internalId
    String name
    String description

    Date dateCreated
    Date lastUpdated

    static hasMany = [ analisys: Analisys]
}

这里是sql语句:

select *
from
    analisys as a1
inner join phase as p1 on a1.phase_id = p1.id,
 analisys as a2
inner join phase as p2 on a2.phase_id = p2.id,
 analisys as a3
left join phase as p3 on a3.phase_id = p3.id
where
a1.phase_id=100
and a2.phase_id=102
and a3.phase_id=103
and a1.instrument_id = a2.instrument_id
and a1.instrument_id = a3.instrument_id

使用CreateCriteria,我开始写这样的东西,但当然是错的:

def results = Classification.createCriteria().list(max: 100, offset: 0) {

            instrument {
                analisys(org.hibernate.criterion.CriteriaSpecification.LEFT_JOIN) {
                    eq("phase.id", 100)
                }
            }
            instrument {
                analisys(org.hibernate.criterion.CriteriaSpecification.LEFT_JOIN) {
                    eq("phase.id", 102)
                }
            }
            instrument {

                analisys(org.hibernate.criterion.CriteriaSpecification.LEFT_JOIN) {
                    eq("phase.id", 103)
                }
            }

        }

使用HQL,我能够使用我在下面写的内容,但仍然不明白如何通过Criteria,现在甚至不确定它是否可能:

Analisys.executeQuery(" from Analisys as a1, Analisys as a2, Analisys as a3 
where a1.phase.id=100 and a2.phase.id=102 and a3.phase.id=103 
and a1.instrument.id = a2.instrument.id and a1.instrument.id = a3.instrument.id ")

我找到了一篇有趣的文章,可以帮助那些遇到同样问题的人:

How and When to Use Various GORM Querying Options

0 个答案:

没有答案