# How to use 'extract'-Function with CriteriaBuilder question

**URL:** https://discourse.hibernate.org/t/how-to-use-extract-function-with-criteriabuilder-question/10075
**Category:** Hibernate ORM
**Created:** [August 5, 2024, 3:04pm UTC](https://discourse.hibernate.org/t/how-to-use-extract-function-with-criteriabuilder-question/10075 "2024-08-05T15:04:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Martin\_Schoepf](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/martin_schoepf/32/3401_2.png) [@Martin\_Schoepf](https://discourse.hibernate.org/u/Martin_Schoepf)
#### Post date: [August 5, 2024, 3:04pm UTC](https://discourse.hibernate.org/t/how-to-use-extract-function-with-criteriabuilder-question/10075/1 "2024-08-05T15:04:54Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![mbladel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbladel/32/2128_2.png) [@mbladel](https://discourse.hibernate.org/u/mbladel)
#### Post date: [August 5, 2024, 3:11pm UTC](https://discourse.hibernate.org/t/how-to-use-extract-function-with-criteriabuilder-question/10075/2 "2024-08-05T15:11:07Z")

</div>

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 )`

---

<div class="post-metadata">

### Author: ![Martin\_Schoepf](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/martin_schoepf/32/3401_2.png) [@Martin\_Schoepf](https://discourse.hibernate.org/u/Martin_Schoepf)
#### Post date: [August 5, 2024, 4:00pm UTC](https://discourse.hibernate.org/t/how-to-use-extract-function-with-criteriabuilder-question/10075/3 "2024-08-05T16:00:20Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![mbladel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbladel/32/2128_2.png) [@mbladel](https://discourse.hibernate.org/u/mbladel)
#### Post date: [August 13, 2024, 7:50am UTC](https://discourse.hibernate.org/t/how-to-use-extract-function-with-criteriabuilder-question/10075/4 "2024-08-13T07:50:44Z")

</div>

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.
