You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
createQuery(Class, BiConsumer): the entity-class counterpart of augment()#1241
A lot of application code does the same thing: list one kind of entity, let the user filter and sort it, and page through the results. The filter and sort are only known at runtime, so this is a job for the Criteria API.
JPA 4.0 made this much better. getResultCount() gives the total without a second query, and the fluent expressions removed most of the builder noise. What is still missing is a short way to start such a query.
JPA 4.0 already has the pattern I'm after, for named queries. You hand over a function, and it customizes the query:
cb.augment(reference, q -> q.where(...));
When you start from an entity class instead of a named query, which is the usual case for a dynamic list, there is nothing like it. You write the same setup every time, around the one line that actually differs:
varcb = em.getCriteriaBuilder();
varquery = cb.createQuery(Book.class);
varbook = query.from(Book.class);
query.select(book);
customizer.accept(query, book); // the only line that changes from query to queryvartyped = em.createQuery(query);
There is also a smaller annoyance in augment() itself: the function gets the query but not its root, so inside it you end up writing (Root<Book>) q.getRootList().get(0).
Proposal
One default method on CriteriaBuilder, next to augment(). No new types, and nothing for providers to implement.
The first version of this proposal put the method on EntityHandler. It moved to CriteriaBuilder after the feedback in #1242.
What it gives
A typed root. The function gets Root<T> as an argument, with no cast.
A standard shape for a query fragment. A caller can pass in BiConsumer<CriteriaQuery<?>, Root<Book>> without the library defining its own type, as Spring Data (Specification), Micronaut (PredicateSpecification) and FlowLogix (JPAFinder) each do today.
A few lines less per query. This is the smallest of the three benefits.
A filter and an ordering declared separately combine with plain BiConsumer.andThen():
Two fragments that each call where() do not combine this way, because the second call replaces the first.
Why in the spec and not a helper
This is a six-line helper, and anyone can write it.
Every project that builds dynamic queries writes this helper, and every library that accepts query fragments (Spring Data, Micronaut Data, FlowLogix, OmniPersistence) defines its own incompatible type for them. A method in the spec gives those fragments one signature.
Working implementation
Both methods, on Jakarta Persistence 3.2, as static methods: FlowLogix CriteriaExtensions. With Lombok's @ExtensionMethod the call chain above compiles as written, see UserDAO.findByName.
#580 (February 2024) proposed this shape: a function that receives the criteria query and modifies it, cqc -> cqc.query().where(...). FlowLogix has shipped it as a lambda since 2020 and as an overridable hook since 2014. augment() in 4.0 uses the same shape.
#580 asked for findAll(), findRange(), count(), two records and a helper interface. 4.0 made most of that unnecessary: getResultCount() replaced the count variants, and fluent expressions removed most uses of the builder. This one method is what is left.
Open questions
1. CriteriaBuilder access
Typed code rarely needs the builder in 4.0, but code that resolves attributes by name still does. With the method on CriteriaBuilder, the builder is already in scope where the function is written:
A function declared somewhere else still has to get the builder from outside. Adding getCriteriaBuilder() to the criteria query would cover that, and would also help augment(). That can be a separate change.
2. Placement
Settled in #1242: CriteriaBuilder, returning a criteria query, with executeIn() on CriteriaQuery to create the executable query.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
The problem
A lot of application code does the same thing: list one kind of entity, let the user filter and sort it, and page through the results. The filter and sort are only known at runtime, so this is a job for the Criteria API.
JPA 4.0 made this much better.
getResultCount()gives the total without a second query, and the fluent expressions removed most of the builder noise. What is still missing is a short way to start such a query.JPA 4.0 already has the pattern I'm after, for named queries. You hand over a function, and it customizes the query:
When you start from an entity class instead of a named query, which is the usual case for a dynamic list, there is nothing like it. You write the same setup every time, around the one line that actually differs:
There is also a smaller annoyance in
augment()itself: the function gets the query but not its root, so inside it you end up writing(Root<Book>) q.getRootList().get(0).Proposal
One
defaultmethod onCriteriaBuilder, next toaugment(). No new types, and nothing for providers to implement.And, as Gavin King suggested in #1242, one
defaultmethod onCriteriaQueryso the whole thing stays one expression:Usage:
The first version of this proposal put the method on
EntityHandler. It moved toCriteriaBuilderafter the feedback in #1242.What it gives
Root<T>as an argument, with no cast.BiConsumer<CriteriaQuery<?>, Root<Book>>without the library defining its own type, as Spring Data (Specification), Micronaut (PredicateSpecification) and FlowLogix (JPAFinder) each do today.A filter and an ordering declared separately combine with plain
BiConsumer.andThen():Two fragments that each call
where()do not combine this way, because the second call replaces the first.Why in the spec and not a helper
This is a six-line helper, and anyone can write it.
Every project that builds dynamic queries writes this helper, and every library that accepts query fragments (Spring Data, Micronaut Data, FlowLogix, OmniPersistence) defines its own incompatible type for them. A method in the spec gives those fragments one signature.
Working implementation
Both methods, on Jakarta Persistence 3.2, as static methods: FlowLogix
CriteriaExtensions. With Lombok's@ExtensionMethodthe call chain above compiles as written, seeUserDAO.findByName.Relation to #580
#580 (February 2024) proposed this shape: a function that receives the criteria query and modifies it,
cqc -> cqc.query().where(...). FlowLogix has shipped it as a lambda since 2020 and as an overridable hook since 2014.augment()in 4.0 uses the same shape.From the October 2020 FlowLogix commit:
#580 asked for
findAll(),findRange(),count(), two records and a helper interface. 4.0 made most of that unnecessary:getResultCount()replaced the count variants, and fluent expressions removed most uses of the builder. This one method is what is left.Open questions
1.
CriteriaBuilderaccessTyped code rarely needs the builder in 4.0, but code that resolves attributes by name still does. With the method on
CriteriaBuilder, the builder is already in scope where the function is written:A function declared somewhere else still has to get the builder from outside. Adding
getCriteriaBuilder()to the criteria query would cover that, and would also helpaugment(). That can be a separate change.2. Placement
Settled in #1242:
CriteriaBuilder, returning a criteria query, withexecuteIn()onCriteriaQueryto create the executable query.3. Release
4.0 or later?
Issue: #1242
Draft PR: #1243
All reactions