How to use 'extract'-Function with CriteriaBuilder question

I just tried to call the ‘extract’-Function inside hibernate 6’s CriteriaBuilder like so:

Expression yearExtractParameter = cb.literal(TemporalUnit.YEAR);
Predicate p1 = cb.le(cb.function(“extract”, Integer.class, yearExtractParameter, root.get(“startDate”)), lookupYear);
Getting the below ClassCastException, also tried String-typed literal.
Is there a way to create a time-unit parameter that translates to an SqmExtractUnit?
Help will be greatly appreciated!

java.lang.ClassCastException: class org.hibernate.query.sqm.tree.expression.SqmLiteral cannot be cast to class org.hibernate.query.sqm.tree.expression.SqmExtractUnit (org.hibernate.query.sqm.tree.expression.SqmLiteral and org.hibernate.query.sqm.tree.expression.SqmExtractUnit are in unnamed module of loader ‘app’)
at org.hibernate.dialect.function.ExtractFunction.generateSqmFunctionExpression(ExtractFunction.java:85)
at org.hibernate.query.sqm.function.AbstractSqmFunctionDescriptor.generateSqmExpression(AbstractSqmFunctionDescriptor.java:104)
at org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.function(SqmCriteriaNodeBuilder.java:1705)
at org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.function(SqmCriteriaNodeBuilder.java:192)

You don’t need to wrap TemporalUnit.YEAR into a literal() expression. You can also use the Hibernate-specific API extension HibernateCriteriaBuilder which exposes the short-hand methods:

  • year( expression )
  • month( expression )
  • day( expression )
  • hour( expression )
  • minute( expression )
  • second( expression )

Thanks for your fast reply!
I think moving to ‘straight’ JPA it should be possible to call the ‘extract’-Function from within a CriteriaBuilder.
Don’t you think so?

The Hibernate extension to Criteria API already offers this and many more added functionalities. Feel free to open a discussion on the JPA side to highlight the need for these to be exposed in the default spec.