Skip to content

GORM: Inconsistent DetachedCriteria composition when using real object instead of closure #14629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
davidkron opened this issue Feb 1, 2017 · 2 comments

Comments

@davidkron
Copy link
Contributor

davidkron commented Feb 1, 2017

I observed inconsistent behavior and API when trying to reuse and compose DetachedCriteria instances together:

  • Criteria composition seems to only work with closure based detached criterias
  • It seems there is no way to compose two detached criteria instances together (DetachedCriteria.where(DetachedCriteria))
  • closure based detached criterias do not work in a type-safe environment @GrailsCompileStatic

Tested with Grails 3.2.4

Some examples to demonstrate

// this is the only variant that works
def myFilter = {} as DetachedCriteria<Person>
Person.where(myFilter)
// this doesn't work, resulting in exception:
// No signature of method: Person.where() is applicable for argument types: (grails.gorm.DetachedCriteria)
def myFilter = new DetachedCriteria(Person).build {}
Person.where(myFilter)
// type-safe variant mit @GrailsCompileStatic doesn't even compile:
// [Static type checking] - Cannot assign value of type groovy.lang.Closure <Person> to variable of type grails.gorm.DetachedCriteria <Person>
DetachedCriteria<Person> myFilter = {} as DetachedCriteria<Person>
Person.where(myFilter)
// DetachedCriteria closure coercion inconsistency:
def myCriteria = {} as DetachedCriteria<Person>
log.debug "${myCriteria instanceof DetachedCriteria}" // -> false

def myPredicate = {} as Predicate
log.debug "${myPredicate instanceof Predicate}" // -> true
@jamesdh
Copy link

jamesdh commented Jan 27, 2021

Just bumped into this, and it's painful because being forced to use a Closure means you lose the ability to define join types, a la:

User.where {
    comments {
        authored > since
    }
}.join('comments', JoinType.INNER)

@davidkron
Copy link
Contributor Author

@jamesdh At some point I was annoyed about this as well, but I think I encountered this in earlier Grails 3.x versions. The current API of detached criteria does seem to support join types:

https://github.com/grails/grails-data-mapping/blob/f1158a73b4127a7aab41608e4ae6e0804079e544/grails-datastore-gorm/src/main/groovy/grails/gorm/DetachedCriteria.groovy#L157-L165

Have you tried this yet?

jamesfredley referenced this issue in apache/grails-data-mapping Mar 13, 2025
…3 (#860)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@jdaugherty jdaugherty transferred this issue from apache/grails-data-mapping Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants