hibernate/hibernate-orm · error · SemanticException

Illegal unit conversion " + this + " to " + unit

Error message

Illegal unit conversion " + this + " to " + unit

What it means

Thrown by TemporalUnit.conversionFactor(TemporalUnit, Dialect) when asked for a conversion factor between two units whose normalized families differ — e.g. converting a month-based unit (YEAR/QUARTER/MONTH) to a nanosecond-based unit. Conversion between duration-based and calendar-based unit families is not mathematically well defined, so a SemanticException is raised. The faulty inputs are the source unit (this) and the requested target unit.

Solutions

  1. Convert only between compatible temporal units (e.g. not months to seconds).
  2. Perform the conversion in application code with explicit logic.
Defensive patterns

Strategy: validation

When it happens

Trigger: A runtime precondition of the Hibernate API is violated.

Common situations: Occurs when application code or configuration does not satisfy the documented contract of the API being called.


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/ad02e9a418bd6b40. Report an issue: GitHub.

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/query/common/TemporalUnit.java:181

	 * the smallest unit of fractional seconds, either milliseconds
	 * or microseconds. We define this value in order to avoid
	 * repeatedly converting between {@link #NANOSECOND}s and a
	 * unit that the database understands. On some platforms this
	 * is also used to avoid numeric overflow.
	 *
	 * @see Dialect#getFractionalSecondPrecisionInNanos()
	 */
	NATIVE;

	public String conversionFactor(TemporalUnit unit, Dialect dialect) {

		if ( unit == this ) {
			//same unit, nothing to do
			return "";
		}

		if ( unit.normalized() != normalized() ) {
			throw new SemanticException("Illegal unit conversion " + this + " to " + unit);
		}

		long from = normalizationFactor( dialect );
		long to = unit.normalizationFactor( dialect );
		if ( from == to ) {
			// the units represent the same amount of time
			return "";
		}
		else {
			// if from < to, then this unit represents a
			// smaller amount of time than the given unit
			// we are converting to (so we're going to
			// need to use division)
			return (from < to ? "/" : "*")
					+ factorAsString(from < to ? to / from : from / to);
		}
	}

View on GitHub (pinned to fad1729dce)