【问题标题】:Grails Criteria Projections - Distinct Projection by two propertiesGrails Criteria Projections - 通过两个属性进行不同的投影
【发布时间】:2016-12-19 15:52:25
【问题描述】:

我想使用 Grails Criteria Projections 获得两列的所有可能组合。

我试试这个:

def criteria = {
   projections{
        property('colA')
        property('colB')
   }
}

def allCombinations = MyDomainEntity.createCriteria().list(criteria)

但是,该代码返回 colA 和 colB 的所有重复组合。

我也尝试使用distinct,但仅适用于单列。

有解决这个问题的想法吗?

谢谢!

【问题讨论】:

  • 我只想获取此类组合的计数。有什么解决办法吗?

标签: grails grails-orm criteria


【解决方案1】:

我不知道你试图解决的真正问题,但根据你所说的你应该使用groupProperty

示例如下:

// Your domain class
class TestEntity {
   String field1
   String field2
}

// test distinct projection
class TestEntityTest extends GroovyTestCase {

    void testDistinctByTwoColumns() {
        new TestEntity(field1: 'test1', field2: 'test2').save()
        new TestEntity(field1: 'test1', field2: 'test2').save() // duplicate
        new TestEntity(field1: 'test1', field2: 'test2').save() // duplicate
        new TestEntity(field1: 'test3', field2: 'test4').save()
        final result = TestEntity.withCriteria {
            projections {
                groupProperty('field1')
                groupProperty('field2')
            }
        }
        assertEquals(2, result.size())
    }
}

附:我使用 Grails 2.5.5 进行测试。也许你的版本有点不同,但我希望这个想法很清楚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-11
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2011-01-03
    • 1970-01-01
    • 2011-08-11
    相关资源
    最近更新 更多