默认情况下,条件使用“内连接”而不是“左连接”方法,使我的查询工作不符合我的计划

时间:2013-06-13 09:07:34

标签: grails gorm

问题是:在这个特定的例子中,如何使GORM生成左连接而不是内连接?

试验台:

给出A,B和C类:

class A{
    B someObject
}

class B{
    C importantObject
}

class C{
    boolean interestingFlag
}

我想列出A类的所有元素:

  • 他们的B.C对象为空或
  • 他们的B.C对象interestingFlag值为false

到目前为止我尝试了什么:

这种方法产生正确的A列表,其中B.C为空(条件2注释掉)或正确的A列表,其中B.C.interestingFlag = false(无论条件1是否被注释掉)。当两个条件都取消注释时,它只返回一个元素列表,其中A.B.C.interestingFlag = false(A.B.C = null条件被忽略)

// approach 1 (conditional 1 is ignored)
def result = A.withCriteria{
    someObject{
        or{
            isNull('importantObject') // conditional 1, works well when conditional 2 is commented out
            importantObject{
                eq('interestingFlag', false) // conditional 2, works well alone, discards conditional 1 when both of them are uncommented
            }
        }  
    } 
}

编辑: 正如评论中所要求的那样,我正在粘贴一个hibernate生成的sql:

Hibernate: select this_.id as id1_2_, this_.version as version1_2_, 
this_.some_object_id as some3_1_2_, someobject1_.id as id2_0_, 
someobject1_.version as version2_0_, someobject1_.important_object_id as 
important3_2_0_, importanto2_.id as id0_1_, importanto2_.version as version0_1_, 
importanto2_.interesting_flag as interest3_0_1_ from a this_ 
inner join b someobject1_ on this_.some_object_id=someobject1_.id 
inner join c importanto2_ on someobject1_.important_object_id=importanto2_.id 
where ((someobject1_.important_object_id is null or (importanto2_.interesting_flag=?)))

当我直接将它复制并粘贴到pgAdmin查询工具中并更改了一些内容(内部联接更改为左联接,并提供了interestingFlag =“false”参数)时,一切都按照我想要的方式工作(我得到两个ABC = null和ABCimportantFlag =假对象)

Hibernate: select this_.id as id1_2_, this_.version as version1_2_, 
this_.some_object_id as some3_1_2_, someobject1_.id as id2_0_, 
someobject1_.version as version2_0_, someobject1_.important_object_id as 
important3_2_0_, importanto2_.id as id0_1_, importanto2_.version as version0_1_, 
importanto2_.interesting_flag as interest3_0_1_ from a this_ 
left join b someobject1_ on this_.some_object_id=someobject1_.id 
left join c importanto2_ on someobject1_.important_object_id=importanto2_.id 
where ((someobject1_.important_object_id is null or (importanto2_.interesting_flag=false)))

3 个答案:

答案 0 :(得分:40)

经过测试和运作的解决方案:

    def result = A.withCriteria{
        createAlias('someObject', 'so', CriteriaSpecification.LEFT_JOIN)
        createAlias('so.importantObject', 'imp', CriteriaSpecification.LEFT_JOIN)
        or {
            isNull('so.importantObject')
            eq('imp.interestingFlag', false)
        } 

    }

答案 1 :(得分:5)

使用左连接来实现此目的。这应该有用,但我还没有现场测试。

def result = A.withCriteria{
    someObject {
        createAlias("importantObject", "io", CriteriaSpecification.LEFT_JOIN)
        or{
            isNull('importantObject') // conditional 1
            eq('io.interestingFlag', false) // conditional 2            
        }  
    } 
}

Eidt:基于这篇文章:http://grails.1312388.n4.nabble.com/CriteriaBuilder-DSL-Enhancements-td4644831.html这也应该有效:

def result = A.withCriteria{
    someObject {
        isNull('importantObject') // conditional 1
        importantObject(JoinType.LEFT) {
            eq('interestingFlag', false) // conditional 2
        }  
    } 
}

请在两种解决方案上发布您的结果。

编辑2:这是一个与您所描述的情况完全相同的查询,但与您的示例不同。你必须从这里开始,使用showSql来调试生成的SQL并使用左连接。

def result = A.withCriteria{
    or {
        isNull('someObject')
        eq('someObject.importantObject.interestingFlag', false)
    } 
}

答案 2 :(得分:1)

我只能用HQL实现左外连接

Class Transaction {
    String someProperty
    static hasMany = [reviews: Review]
    static hasOne = [reviewQueue: ReviewQueue]
}

Class ReviewQueue {
   Transaction transaction
   Boolean isComplete
   Boolean isReady
}

Class Review {
  Transaction transaction
  String reviewResult
}

def list = Transaction.executeQuery(
    "select  t.id, rq.isReady, rq.isComplete, count(r.transaction.id) " +
    "from Transaction t " +
    "join t.reviewQueue rq " +
    "left outer join t.reviews r " +
    "where rq.isComplete = false " +
    "and rq.isReady = true " +
    "group by t.id " +
    "having count(r.transaction.id) = 0 " +
    "order by rq.transaction.id ",
[max: 10, offset: 0])