hibernate/hibernate-orm · error · UnsupportedOperationException
Temporal unit not supported [{}]
Error message
Temporal unit not supported [{}] What it means
DateTruncEmulation (used by TruncFunction when the dialect lacks native date_trunc) formats the datetime with format() and pads the trailing components; it implements only YEAR, MONTH, DAY, HOUR, MINUTE and SECOND. Requesting any other TemporalUnit — WEEK, QUARTER, MILLISECOND, EPOCH, NATIVE... — hits the default branch and throws UnsupportedOperationException at query compilation.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/dialect/function/DateTruncEmulation.java:102
break;
case DAY:
pattern = "yyyy-MM-dd";
literal = " 00:00:00";
break;
case HOUR:
pattern = "yyyy-MM-dd HH";
literal = ":00:00";
break;
case MINUTE:
pattern = "yyyy-MM-dd HH:mm";
literal = ":00";
break;
case SECOND:
pattern = "yyyy-MM-dd HH:mm:ss";
literal = null;
break;
default:
throw new UnsupportedOperationException( "Temporal unit not supported [" + temporalUnit + "]" );
}
final var datetime = arguments.get( 0 );
final var formatExpression =
queryEngine.getSqmFunctionRegistry()
.getFunctionDescriptor( "format" )
.generateSqmExpression(
asList(
datetime,
new SqmFormat( pattern, nodeBuilder.getStringType(), nodeBuilder )
),
null,
queryEngine
);
final SqmExpression<?> formattedDatetime;
if ( literal != null ) {
formattedDatetime =
queryEngine.getSqmFunctionRegistry()
.getFunctionDescriptor( "concat" )View on GitHub (pinned to fad1729dce)
Solutions
- Use one of the supported units (YEAR, MONTH, DAY, HOUR, MINUTE, SECOND)
- Compute week/quarter truncation differently: e.g. arithmetic on extract(year/month) or a native query for that DB
- Extend the dialect: subclass and register a custom date_trunc/trunc descriptor that renders week/quarter SQL for your database
Example fix
// before (HANA / emulated date_trunc)
session.createQuery("select trunc(e.ts, WEEK) from Event e"); // -> UnsupportedOperationException
// after
session.createQuery("select trunc(e.ts, DAY) from Event e"); // supported unit Defensive patterns
Strategy: validation
Validate before calling
Set<TemporalUnit> SUPPORTED = EnumSet.of(YEAR, MONTH, DAY, HOUR, MINUTE, SECOND);
if (!SUPPORTED.contains(unit) && dialectLacksNativeDateTrunc(dialect)) {
throw new IllegalArgumentException("date_trunc unit " + unit + " unsupported on " + dialect);
} Type guard
boolean isEmulationSupportedUnit(TemporalUnit u) {
return u == YEAR || u == MONTH || u == DAY || u == HOUR || u == MINUTE || u == SECOND;
} Try / catch
catch (UnsupportedOperationException e) {
// fall back to a native query for exotic units on this dialect
return runNativeWeekTruncation(session, ts);
} Prevention
- Keep a per-dialect capability matrix for temporal units in the test suite
- Express week/quarter logic via arithmetic on extract() results instead of trunc()
- Wrap trunc() calls behind a repository method so fallback logic lives in one place
When it happens
Trigger: HQL like 'select trunc(e.ts, WEEK)' or date_trunc('week', ts) emulation on a dialect using DateTruncEmulation; extract/trunc with temporal units such as quarter, millisecond, or week on those dialects (commonly SAP HANA, which routes TruncFunction through toDateFunction emulation).
Common situations: Writing portable HQL on PostgreSQL (week works natively) and running the same query on HANA/other DB in CI or production; upgrading Hibernate and having previously-ignored units now validated strictly.
Related errors
- Unrecognized field:
- unrecognized field: {unit}
- Unrecognized field: {unit}
- Emulation of function chr() supports only integer literals,
- NATIVE is not a legal field for extract()
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/c086f4e4ece9abd1.
Report an issue: GitHub.