I am getting this following exception when trying to use the trunc function:
criteriaBuilder.function("TRUNC", LocalDateTime.class,
subRoot.get("..."), criteriaBuilder.literal(TemporalUnit.DAY))
Caused by: org.hibernate.query.sqm.produce.function.FunctionArgumentException: Parameter 1 of function 'trunc()' has type 'NUMERIC', but argument is of type 'java.time.LocalDateTime'
at org.hibernate.query.sqm.produce.function.ArgumentTypesValidator.throwError(ArgumentTypesValidator.java:303) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
ArgumentTypesValidator.java:303
at org.hibernate.query.sqm.produce.function.ArgumentTypesValidator.checkArgumentType(ArgumentTypesValidator.java:264) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
ArgumentTypesValidator.java:264
at org.hibernate.query.sqm.produce.function.ArgumentTypesValidator.checkArgumentType(ArgumentTypesValidator.java:152) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
ArgumentTypesValidator.java:152
at org.hibernate.query.sqm.produce.function.ArgumentTypesValidator.validate(ArgumentTypesValidator.java:101) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
ArgumentTypesValidator.java:101
at org.hibernate.dialect.function.TruncFunction$TruncArgumentsValidator.validate(TruncFunction.java:225) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
TruncFunction.java:225
at org.hibernate.query.sqm.function.AbstractSqmFunctionDescriptor.generateSqmExpression(AbstractSqmFunctionDescriptor.java:102) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
AbstractSqmFunctionDescriptor.java:102
at org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.function(SqmCriteriaNodeBuilder.java:1705) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
SqmCriteriaNodeBuilder.java:1705
at org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.function(SqmCriteriaNodeBuilder.java:192) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
SqmCriteriaNodeBuilder.java:192
I examined the hibernate code and I can see that probably the function should enter in the first if for dateTime validator:
package org.hibernate.dialect.function;
...
@Override
public void validate(
List<? extends SqmTypedNode<?>> arguments,
String functionName,
TypeConfiguration typeConfiguration) {
if ( arguments.size() == 2 && arguments.get( 1 ) instanceof SqmExtractUnit ) {
DATETIME_VALIDATOR.validate( arguments, functionName, typeConfiguration );
}
else {
NUMERIC_VALIDATOR.validate( arguments, functionName, typeConfiguration );
}
}
But there is no way one can pass an Expression having SqmExtractUnit in the function method right? I found in another forum discussion that hibernate introduced criteriaBuilder.truncate but I am on version 6.4.4FINAL and I cannot find this method. So it seems essentially that there is no way to use the trunc function with dates using criteria builder…?