4

在下面的示例中,我希望 Product.searchAll 匹配添加剂和产品,但它似乎忽略了eq('name', taste).

class Additive {
    String flavor
    static belongsTo = [product:Product]
}

class Product {
    String name
    static hasMany = [additives:Additive]

    static constraints = {
            name nullable:true
    }

    static namedQueries = {
        searchAll { taste ->
            or {
                eq('name', taste)
                additives { eq('flavor', taste) }
            }
        }
        searchAdditives { taste ->
            additives { eq('flavor', taste) }
        }
        searchProducts { taste ->
            eq('name', taste)
        }
    }
}

class SearchSpec extends grails.plugin.spock.IntegrationSpec {
    def choc, muff

    def 'searchAll should return products and additives that match - THIS FAILS'() {
        setup:
            createTestProducts()
        expect:
            Product.searchAll("chocolate").list() == [choc, muff]
    }


    def 'searchProducts should return only products that match - THIS PASSES'() {
        setup:
            createTestProducts()
        expect:
            Product.searchProducts("chocolate").list() == [choc]
    }

    def 'searchAdditives should return only additives that match - THIS PASSES'() {
        setup:
            createTestProducts()
        expect:
            Product.searchAdditives("chocolate").list() == [muff]
    }

    private def createTestProducts() {
        // create chocolate
        choc = new Product(name:'chocolate').save(failOnError:true, flush:true)
        // create a chocoloate-flavored muffin
        muff = new Product(name:'muffin').addToAdditives(flavor:'chocolate').save(failOnError:true, flush:true)
    }
}

生成的SQL如下:

select this_.id as id1_1_, this_.version as version1_1_,
this_.name as name1_1_, additives_1_.id as id0_0_,
additives_1_.version as version0_0_, additives_1_.flavor as
flavor0_0_, additives_1_.product_id as product4_0_0_ from product
this_ inner join additive additives_1_ on
this_.id=additives_1_.product_id where (this_.name=? or
(additives_1_.flavor=?))

我的语法有问题,还是 Grails、GORM 或 H2 有问题?

4

1 回答 1

5

我的猜测是,快速查看您的查询,Grails / GORM 正在执行inner join. 仅当表之间存在关系时,内连接才匹配。在上面的示例中,该查询永远不会匹配choc,因为choc没有任何关联的添加剂。

所以,这不是or失败的,而是实际的查询。启动localhost:8080/{yourapp}/dbConsole并运行相同的查询,但没有where语句。您应该看到您只获得含有一种或多种添加剂的产品。

我相信(未经测试)你可以强制LEFT JOIN使用这样的语法:

import org.hibernate.criterion.CriteriaSpecification
...

    searchAll { taste ->
        createAlias("additives", "adds", CriteriaSpecification.LEFT_JOIN)
        or {
            eq('name', taste)
            eq('adds.flavor', taste)
        }
    }

这应该强制左(或外)连接,允许没有匹配添加剂的产品。注意:使用外连接时可能会得到重复的结果,但这取决于您的特定使用场景。

于 2012-05-10T08:44:51.830 回答