Skip to content

No support to refer to root criteria alias from subquery #14623

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
snimavat opened this issue Feb 28, 2017 · 4 comments
Open

No support to refer to root criteria alias from subquery #14623

snimavat opened this issue Feb 28, 2017 · 4 comments

Comments

@snimavat
Copy link
Contributor

What i am trying to achieve is almost similar to what discussed here http://grails.1312388.n4.nabble.com/Subqueries-in-criteria-tp1678501p3078371.html --

Given the domain classes

Book {
   Author author
   Date datePublished
}

Author {
   Date dob
}

I would like to find all authors who has published book in 2016 (using criteria and exists)

So i would expect some thing like this to work

Author.withCriteria {
  exists(Book.where({
    datePublished = xxx
   eqProperty("author.id", "this.id") //here i am trying to access the root criteria alias using this.
}).id())
}

However this doesnt give the expected result, and generated sql query looks wrong.

What is expected:
A way to refer to root criteria alias, when doing subqueries

@graemerocher
Copy link
Contributor

The above is already possible with where queries. See commit above.

@snimavat
Copy link
Contributor Author

snimavat commented Mar 1, 2017

@graemerocher - yes, creating alias that way for where queries is already explained on reference documentation... However that does not work when i want to use detached criteria/(or where query) as subquery in Criteria query (withCriteria/createCriteria).

See my example for Author criteria, creating alias as you mentioned wont work there.

@snimavat
Copy link
Contributor Author

snimavat commented Mar 1, 2017

I think the issue here is, the alias for criteria and alias for subquery conflicts as both are using the "this" as alias.

So probably one solution is to do

def sub = Book.where({
    datePublished = xxx
   eqProperty("author.id", "this.id") //here i am trying to access the root criteria alias using this.
}).id()

Author.withCriteria {
  exists(sub.setAlias("book"))
}

@snimavat
Copy link
Contributor Author

snimavat commented Mar 7, 2017

is there a way to use alias as u explained, with detached criteria as subquery for a criteria (see my original example at the top)

Or it must be done as my above post ?

Looks like its possible only with where queries, but not when u use where query as subquery to a criteria

jamesfredley referenced this issue in apache/grails-data-mapping Mar 13, 2025
…plugin to v2.0.2 (#884)

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