Skip to content

Instantly share code, notes, and snippets.

@deigote
Last active August 29, 2015 14:26
Show Gist options
  • Save deigote/549dcecdbb2a6ba80074 to your computer and use it in GitHub Desktop.
Save deigote/549dcecdbb2a6ba80074 to your computer and use it in GitHub Desktop.
Hibernate criteria: Wrong results when join + pagination
class Car {
static hasMany = [brands: Brand]
}
class Brand { }
class CarService {
def searchCars(int firstResult, int maxResults) {
Car.createCriteria().list {
createAlias('brands', 'brands', CriteriaSpecification.INNER_JOIN)
'in'('id', [1, 2].collect {it.toLong() } ) // Make sure Car(1) and Car(2) have at least 2 brands
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
setFirstResult(firstResult)
setMaxResults(maxResults)
}
}
// This assert fails
def proveSearchIsBroken() {
assert searchCars(0, 1).first() != searchCars(1, 1).first()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment