{"record":{"id":"40c72133e0a01bda","repo":"hibernate/hibernate-orm","slug":"unrecognized-field-unit-40c721","errorCode":null,"errorMessage":"unrecognized field: {unit}","messagePattern":"unrecognized field: (.+?)","errorType":"exception","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/OracleLegacyDialect.java","lineNumber":753,"sourceCode":"\t\t\t\t\telse {\n\t\t\t\t\t\tpattern.append( \"(\" );\n\t\t\t\t\t\textractField( pattern, DAY, unit );\n\t\t\t\t\t\tpattern.append( \"+\" );\n\t\t\t\t\t\textractField( pattern, HOUR, unit );\n\t\t\t\t\t\tpattern.append( \"+\" );\n\t\t\t\t\t\textractField( pattern, MINUTE, unit );\n\t\t\t\t\t\tpattern.append( \"+\" );\n\t\t\t\t\t\textractField( pattern, SECOND, unit );\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tpattern.append( \"((?3-?2)\" );\n\t\t\t\t\tpattern.append( TemporalUnit.DAY.conversionFactor( unit, this ) );\n\t\t\t\t}\n\t\t\t\tpattern.append( \")\" );\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tthrow new SemanticException( \"unrecognized field: \" + unit );\n\t\t}\n\t\treturn pattern.toString();\n\t}\n\n\tprivate void extractField(StringBuilder pattern, TemporalUnit unit, TemporalUnit toUnit) {\n\t\tpattern.append( \"extract(\" );\n\t\tpattern.append( translateExtractField( unit ) );\n\t\tpattern.append( \" from (?3-?2)\" );\n\t\tswitch ( unit ) {\n\t\t\tcase YEAR:\n\t\t\tcase MONTH:\n\t\t\t\tpattern.append( \" year(9) to month\" );\n\t\t\t\tbreak;\n\t\t\tcase DAY:\n\t\t\tcase HOUR:\n\t\t\tcase MINUTE:\n\t\t\tcase SECOND:\n\t\t\t\tbreak;","sourceCodeStart":735,"sourceCodeEnd":771,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/OracleLegacyDialect.java#L735-L771","documentation":"OracleLegacyDialect.timestampdiffPattern() renders HQL timestampdiff()/datetime subtraction by decomposing an Oracle interval (year(9) to month, day(9) to second) with extract(). Units outside the handled set for this legacy dialect (for example WEEK, QUARTER, or an unhandled composite) hit the default branch and throw SemanticException during query translation, before SQL execution.","triggerScenarios":"HQL like 'timestampdiff(week, o.orderDate, o.shipDate)' or 'o.shipDate - o.orderDate week' on OracleLegacyDialect; Criteria difference expressions built with a unit the switch does not cover.","commonSituations":"Age/duration reports asking for weeks or quarters; queries copied between dialects with richer unit coverage; dynamic unit selection driven by report filters.","solutions":["Ask for day or second and scale in the select expression (e.g. '/ 7' for weeks)","Fetch both timestamps and compute the difference in Java","Move to the modern org.hibernate.dialect.OracleDialect if it supports the unit","Use a native query with extract()/day-to-second arithmetic"],"exampleFix":"-- before\nselect timestampdiff(week, o.orderDate, o.shipDate) from Order o\n\n-- after\nselect timestampdiff(day, o.orderDate, o.shipDate) / 7 from Order o","handlingStrategy":"validation","validationCode":"private static final Set<TemporalUnit> ORACLE_TIMESTAMPTDIFF_UNITS = EnumSet.of(\n        TemporalUnit.YEAR, TemporalUnit.MONTH, TemporalUnit.DAY,\n        TemporalUnit.HOUR, TemporalUnit.MINUTE, TemporalUnit.SECOND );\n\nTemporalUnit effective = ORACLE_TIMESTAMPTDIFF_UNITS.contains( unit )\n        ? unit : TemporalUnit.DAY; // then scale the result manually","typeGuard":"static boolean oracleTimestampDiffSupports(TemporalUnit unit) {\n    return EnumSet.of( TemporalUnit.YEAR, TemporalUnit.MONTH, TemporalUnit.DAY,\n                       TemporalUnit.HOUR, TemporalUnit.MINUTE, TemporalUnit.SECOND ).contains( unit );\n}","tryCatchPattern":"try {\n    return session.createQuery( hql, Long.class ).getSingleResult();\n}\ncatch ( SemanticException e ) {\n    // unsupported diff unit: switch the HQL to day/second and rescale\n    throw new IllegalArgumentException( \"Use day/second and scale\", e );\n}","preventionTips":["Normalize duration requests to days or seconds and convert to weeks in the UI layer","Unit-test every HQL date-math expression against the target Oracle dialect","Avoid week/quarter units in shared query libraries"],"tags":["oracle","hql","temporal-unit","query-translation"],"backgroundTag":"unsupported-temporal-unit","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}