Validate queries passed to 'with' method belong to the same scope
Description
Attachments
Activity
Alvaro PedrazaAugust 27, 2024 at 4:00 AM
so the idea would be to improve the with method to avoid this kind of misuse or to provide a better error message? or maybe both?
Honestly I’m not so familiar with the Criteria API but I’ll study this topic a little bit to understand what is happening here and how can be improved. Please do share with me any idea you may have about this improvement.
CC:
Marco BelladelliAugust 26, 2024 at 7:09 AM
thanks for the initiative, though this was identified as not a bug - rather a misuse of the with
method. As I mentioned in my previous comment, we might do some sort of validation for root / subqueries to only accept CTEs from the right “context”.
I might have lost track of this issue, since this was assigned a sprint a long time ago I can take care of it in the coming days. It would be nice to have a test in the ORM suite, though I believe we can use 's reproducer and build working examples on top of it. I’ll change the issue type to improvement.

Bartlomiej NiemiecAugust 26, 2024 at 6:00 AM
I didn’t find any version in which this example works correctly
I used JDK 17 and JDK 21
I just stumbled upon it by accident during my daily work.
Alvaro PedrazaAugust 23, 2024 at 4:53 AM
:
I’ll give it a try to this one. I have a couple of questions:
Did you checked if this is a regression issue?
Which Java version did you used to reproduce this issue?
Also, any insight you can share with me with any findings or where to start looking, it will be appreciated.
CC:
Marco BelladelliJune 5, 2024 at 1:24 PM
We should validate that queries passed as CTEs belong in the same “context”, we could check if they have the same query as parent. For example, if using with
for a subquery, allow subqueries from the same parent as CTEs, or if using it for a root query (which has a null
parent) require root queries as CTE as well.
Details
Assignee
Marco BelladelliMarco BelladelliReporter
Bartlomiej NiemiecBartlomiej NiemiecComponents
Sprint
NoneFix versions
Priority
Major
Details
Details
Assignee
Reporter

I have following entities:
when i try to execute query:
I get following error:
This query works correctly if parameter
hibernate.criteria.copy_tree
is set to false.