Repository navigation
Apply readOnly, fetchSize, timeout and flushMode to criteria queries and type the criteria closures - #16823
Apply readOnly, fetchSize, timeout and flushMode to criteria queries and type the criteria closures#16823borinquenkid wants to merge 15 commits into
Conversation
…and type the criteria closures The Hibernate 7 HibernateQuery never read the query arguments map, so a criteria query dropped readOnly, fetchSize, timeout and flushMode that the HQL path applied. Both paths now read them through one HibernateQuerySettings record, which also rejects a value that is not a whole number by naming the argument. withCriteria(args) and the list(args) builder route pass the whole map to the query. GormEntity.where, whereLazy, whereAny and findAll carry @DelegatesTo (DetachedCriteria) so the criteria closures type-check under CompileStatic. Fixes #16808
…riteria-query-args-8.0.x
…nate 5 does The criteria reference describes get as returning a unique result and says it is not to be confused with a limit to just the first row. Hibernate 5 throws NonUniqueResultException when several rows match; Hibernate 7 caught it and returned the first row, so a criteria that matched more rows than intended went unnoticed after an upgrade. Criteria get now takes its own strict path. find, findBy, findWhere and the get and find of a where query keep returning the first row on both modules. The behaviour is pinned by an integration spec that runs unchanged against the Hibernate 5 and Hibernate 7 grails-hibernate apps; the core SingleResultFirstRowSpec asserted the old first-row result for criteria get and is removed.
…of HibernateQueryExecutor uniqueResult returns the row when one matches, null when none do and fails with the row count when several do; singleResult keeps returning the first row. Both run against a real session.
…riteria-query-args-8.0.x
|
For the team: criteria This PR now fails a criteria The criteria reference describes
Worth sharing with the team
|
…riteria-query-args-8.0.x
…riteria-query-args-8.0.x
…riteria-query-args-8.0.x
…riteria-query-args-8.0.x
…meout and flushMode on H5 and H7 Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
…r of HibernateQueryExecutor Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
…riteria-query-args-8.0.x
…uery-args-8.0.x Takes the merge of fix/h7-subquery-root-alias-nested-paths-8.0.x. The upgrade guide numbers its sections 26.14 (not-equal comparisons), 26.15 (bulk statements) and 26.16 (query arguments), as the sections of the branches below it already took 26.14 and 26.15. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. A get that finds more than one row leaves the session it opened still bound.
| } | ||
| } else { | ||
| result = hibernateQuery.singleResult(); | ||
| result = hibernateQuery.uniqueResult(); |
There was a problem hiding this comment.
createCriteria().get { gt('age', 18) } with two matching rows, and no session already bound. The call throws, and the session it opened stays bound. Close that session even when the call throws.
…ened
createCriteria().get { gt('age', 18) } with two matching rows threw and left the session it had opened bound to
the thread, as did a list with a sort that names no property and a criteria closure that throws. The session the
builder opened is now closed when the call throws, as it is when the call returns. A session of the caller that
the builder only participates in is left as it is.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Reproduced and fixed in 20510a5.
Cases in Not covered: Branch policy: the new types and throws of this PR were already in it; this commit adds one public method of the builder and no throw. Where the PR belongs is still open; I have not moved it. |
matrei
left a comment
There was a problem hiding this comment.
Reviewed at 20510a5 (base fix/h7-bulk-write-pagination-guard-8.0.x, #16822's head branch). This PR's own change is the 18 files GitHub shows, measured from ff0c8c9, the #16822 commit it last merged; #16822's head is 65d597f now. The PR is labelled no ci, so CI has not run on it.
What I ran locally:
-
A throwaway spec with Hibernate 5 on 7.0.x (00f51d4), Hibernate 7 on 8.0.x (9fc7dd3), and Hibernate 5 and 7 at this head. Each call ran in a transaction of its own, except in the last two rows, which run with no session bound:
Call Hibernate 5, 7.0.x Hibernate 7, 8.0.x Hibernate 7, this head where { }.list(readOnly: true)ignored ignored applied where { }.find(readOnly: true)ignored ignored applied findAll(readOnly: true) { }ignored ignored applied findAllBy…(…, [readOnly: true])ignored ignored applied withCriteria(readOnly: true) { }ignored ignored applied createCriteria().list(readOnly: true) { }applied ignored applied list(readOnly: true)applied applied applied flushMode: 'ALWAYS'onwhere { }.list,withCriteriaand a finderignored ignored applied flushMode: 'ALWAYS'oncreateCriteria().listapplied ignored applied where { }.list(fetchSize: 'abc')ignored ignored IllegalArgumentExceptioncriteria getwith three matching rowsNonUniqueResultExceptionthe first row NonUniqueResultExceptionno session bound, a criteria closure that throws nothing left bound its session left bound nothing left bound no session bound, list(max: 10, sort: 'noSuchProperty') { }nothing left bound its session left bound nothing left bound The datastore of the spec does not flush before a query by default, so
ALWAYSis the value that shows whetherflushModearrives. Hibernate 5 at this head gives the same results as on 7.0.x, except thatwithCriteria(args)appliesreadOnlyandflushMode: on 8.0.x,withCriteria(Map, Closure)callscreateCriteria().list(args) { }, and on 7.0.x it sets the map as builder properties. -
The two example-app specs on Hibernate 5 at this head: the 13 features pass.
-
A
@GrailsCompileStaticclass in the Hibernate 7 example app, on 8.0.x and at this head: flat criteria, the where DSL, association blocks (pets { eq('name', 'rex') },owner { eq('name', 'Ned') }), mixed criteria and an association path return the same rows on both. A plain@CompileStaticclass at this head returns the same rows too. -
This PR's diff applied to 8.0.x: the code applies without conflicts; only the upgrade guide conflicts. With it, the full suites of
grails-datamapping-coreandgrails-data-hibernate7-corepass on 8.0.x (1816 and 4716 tests, no failures), the PR's new specs included.
Needs a fix
-
Branch: this can go to 8.0.x on its own. For Hibernate 7, 8.0.x is the right branch: all rows of the table but
list(readOnly: true)are wrong there, and Hibernate 7 starts at 8.0. But nothing in the change needs #16816 to #16822. Only the example-app specs useScenarioItemandScenarioStatus, which come from #16816. Please rebase onto 8.0.x and give the example apps a domain class of their own, as #17051 does withBulkWriteItem. Then this PR does not wait for the chain, andno cican go: a pull request against 8.0.x runs the tests. -
Branch: Hibernate 5 drops the same arguments on 7.0.x, where they are documented.
findAll.adoclistsreadOnly,fetchSize,flushModeandtimeoutforfindAll(Map queryParams, Closure whereCriteria), andfindAllBy.adoclists them as metaParams of the dynamic finders. On 7.0.x, Hibernate 5 ignores them for both, and forwherequeries. The cause is the one this PR fixes for Hibernate 7:DynamicFinder.populateArgumentsForCriteriaandDetachedCriteriahand these arguments only to a query that implementsQueryArgumentsAware, and the Hibernate 5HibernateQuerydoes not. By the rule in the dev@ thread, that part goes to 7.0.x in a pull request of its own for Hibernate 5 and is merged up.QueryArgumentsAwareis the hook both callers already use, so implementing it adds no extension point. The Hibernate 5 example-app spec of this PR belongs there too. It passes at this head, but it covers onlywithCriteriaandcreateCriteria().list, which Hibernate 5 honours on 8.0.x. On 7.0.x itswithCriteria(readOnly: true)feature would fail, sincewithCriteriathere ignoresreadOnly, andwherequeries,findAll(args) { }and the finders need features of their own. -
The strict criteria
getchanges a result on 8.0.x. On 8.0.x,createCriteria().get { gt('amount', 0) }with three matching rows returns the first row; at this head it throwsNonUniqueResultException, as Hibernate 5 does on 7.0.x. I agree with the change: the reference describesgetas returning "a unique result", "not to be confused with a limit to just the first row", and Hibernate 7 starts at 8.0, so 8.0.x is the oldest branch with the bug. But a call that returned a row now throws, so the rule in the dev@ thread asks for a second reviewer, a description of the change, and an upgrade note that says how to keep the first row, for examplemaxResults(1)with anorderin the closure, which you found still returns a row. The note does not mention the change yet, and the description mentions it only in your comment of 11-02. -
New public API on the patch line. The patch rule allows no new public classes or methods on 8.0.x. Here:
HibernateQuerySettingsis a new public record (@since 8.0.1).HqlQueryMethodsandHibernateQuery, which use it, are in its package, so it can be package-private.HibernateCriteriaBuilder.closeSessionAfterFailure()is public.CriteriaMethodInvoker, its only caller, is ingrails.ormtoo, so it can be package-private.HibernateQuery.getFetchSize(),getTimeout(),getReadOnly()andgetFlushMode()are read only byQueryArgumentsPropagationSpec. The spec already testsreadOnlyandflushModeby their effect, andHibernateQueryExecutorSpeccovers what reaches the Hibernate query, so the getters can go.HibernateQuery.uniqueResult()andHibernateQueryExecutor.uniqueResult()serve only the criteriaget. The invoker could get the rows from the existinglist()and check their number itself.
HibernateQuery.setArgumentsimplements the existingQueryArgumentsAware, so that one is fine. -
@DelegatesTobelongs in a pull request of its own, against 8.1.x. It does not fix the arguments: it lets the closures ofwhere,whereLazy,whereAnyandfindAlltype-check under plain@CompileStatic. It is ingrails-datamapping-core, so every datastore and Hibernate 5 get it, and 7.0.x has the same signatures. Code that did not compile now compiles, and I found no result that changes (see above), so it is an improvement rather than a fix, and that goes to the next minor.GormEntityCriteriaTypingSpeccovers flat criteria under plain@CompileStatic.@GrailsCompileStaticcompiledwhereclosures before, throughWhereQueryTypeCheckingExtension, as dynamic calls; with the annotation they are compiled statically. Features for association blocks and for@GrailsCompileStaticwould catch a later change that binds a call in a nested block to the outer criteria. -
The upgrade note.
- 8.0.0 is released, and on 8.0.x the notes for later changes are in the Grails 8.0.1 section. 8.0.x moved "Custom User Types" and "Several Unidirectional hasMany Associations on One Owner" there, which are 26.11 and 26.12 on this branch. Please put this note there, as a
====section, rather than as 26.16 of the 8.0.0 changes. - It should cover the strict
get(item 3), and say that the dynamic finders now apply the arguments too. The@DelegatesToparagraph goes with item 5.
- 8.0.0 is released, and on 8.0.x the notes for later changes are in the Grails 8.0.1 section. 8.0.x moved "Custom User Types" and "Several Unidirectional hasMany Associations on One Owner" there, which are 26.11 and 26.12 on this branch. Please put this note there, as a
-
Description. Please update it to match this head:
- "Stacked on #16822" and the
no ciline change with item 1. - It leaves out the strict
get, the session cleanup of 20510a5 and the dynamic finders. - The test counts (4444 Hibernate 7 tests) are older than your comment of 11-18, which says 4637.
- "Fixes #16808" would close the issue, which also lists
readOnly(true)andcache(true)on aDetachedCriteria, still "no-op for now" inAbstractDetachedCriteria, and the completeness tests of F6. "Part of #16808" fits, unless the issue says those are done or not needed. - What changes for applications, as the rule in the dev@ thread asks: arguments that criteria queries and finders ignored now apply, a criteria
getthat returned the first of several rows now throws, and afetchSizeortimeoutthat is not a whole number now throws instead of being ignored.
- "Stacked on #16822" and the
Suggestions
CriteriaSessionCleanupSpechas@Issue('https://github.com/apache/grails-core/issues/16823'), which is this pull request. #16808, or James's review comment, fits.- In
CriteriaSingleResultScenarioSpec, "find with a closure returns the first row" and "dynamic finder and findWhere return the first row" query without an order, so the expectedn1is the row the database happens to return first. H2 returns the rows in insertion order; another database need not. - The four example-app specs can use a bare
@Integration: each app has oneApplicationclass, and the annotation finds it.
Verified as correct
- At this head, every call in the table applies
readOnlyandflushModeon Hibernate 7, and a criteriagetthrows when several rows match and returns the row ornullotherwise. - James's finding of 11-12 is fixed: a criteria
getthat throws, a closure that throws and a refused sort leave no session bound, andCriteriaSessionCleanupSpecchecks that a session of the caller stays bound and open. - A setting absent from the map keeps its current value, so a later call does not reset an earlier one, and the HQL path reads the same settings through
HibernateQuerySettings.
|
Items 1, 3, 4, 6 and 7 of your review: the Hibernate 7 part is rebased on 8.0.x as #17077, with a domain class of its own, no new public API (package-private settings record and cleanup hook, no getters, the criteria get reads |
|
Item 2 of your review: the Hibernate 5 part is #17078 against 7.0.x ( |
Fixes #16808
Stacked on #16822 (chain: #16816, #16817, #16818, #16820, #16821, #16822).
no ciuntil the chain reaches 8.0.x.Problem
HibernateQuerynever read the query arguments map. A criteria query droppedreadOnly,fetchSize,timeoutandflushMode, which the HQL path applied:Person.where { ... }.list(readOnly: true)loaded modifiable entities andflushMode: 'MANUAL'still flushed first.withCriteria(args) { }read onlymax,offsetandsort.HqlQueryMethodsand by the criteria code, so a setting could be honoured by one path and dropped by the other.GormEntity.where,whereLazy,whereAnyandfindAllhad no@DelegatesTo, soeqandgtdid not type-check under@CompileStatic.Changes
HibernateQuerySettingsrecord: readsfetchSize,timeout,readOnlyandflushModefrom an arguments map once. A value that is not a whole number throwsIllegalArgumentExceptionnaming the argument and the value.HqlQueryMethods.populateQuerySettingsand the newHibernateQuery.setArguments(it now implementsQueryArgumentsAware) both go through it.CriteriaMethodInvokerpasses the whole map forwithCriteria(args).@DelegatesTo(DetachedCriteria)on the fiveGormEntitymethods.Tests
QueryArgumentsPropagationSpec(12):readOnlytrue/false throughwhere().list,findAll(args) { }andwithCriteria(args) { }, a single result,flushModeMANUAL vs ALWAYS with a pending change, parsing, a bad value, absent arguments, and a later call keeping earlier settings. Red before the fix (8 failures).HibernateQuerySettingsSpec(18).SingleResultFirstRowSpec(3): first row on ascending and descending order, andnullwhen nothing matches.GormEntityCriteriaTypingSpec: a@CompileStaticclass calls all five methods. It fails to compile without the annotation.grails-data-hibernate7-coresuite: 4444 tests, 0 failures.grails-datamapping-core: 1813, 0 failures.codeStyle,validateRepositoryConventionsandratpass.HibernateQuerySettings: 9 mutants, 9 killed.Not in this PR
singleResultwith several matching rows keeps returning the first row, which is the Grails convention (finddocuments it).SingleResultFirstRowSpecpins it for criteriaget, andwheregetandfind.HibernateQueryConstantsandGrailsHibernateUtilare already deprecated and are left for removal in the Java-to-Groovy chain.