I want to get all possible combinations of two columns using Grails Criteria Projections.
I try this:
def criteria = {
projections{
property('colA')
property('colB')
}
}
def allCombinations = MyDomainEntity.createCriteria().list(criteria)
But, that code returns all the repeated combinations of colA and colB.
I also tried to use distinct
, but only works for a single column.
Any ideas to solve this problem?
Thanks!
I'm not aware of the real problem you try to solve, but according to what you've said you should use groupProperty
.
Here is the example:
// 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())
}
}
P.S. I use Grails 2.5.5 for testing. Maybe your version is a little bit different, but I hope the idea is clear.