hibernate/hibernate-orm · error · CoercionException

Cannot coerce Long value `%s` as Short : overflow

Error message

Cannot coerce Long value `%s` as Short : overflow

What it means

Hibernate throws this CoercionException when a Long is narrowed to Short (range -32768..32767) and exceeds 32767. Thrown from CoercionHelper.toShort(Long), which ShortJavaType.coerce calls when a Long value reaches a Short-mapped attribute. Hibernate 6 performs explicit bounds checks on every narrowing coercion, so oversized values fail with the offending number in the message instead of being truncated.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionHelper.java:188

	public static Short toShort(Byte value) {
		return value.shortValue();
	}

	public static Short toShort(Integer value) {
		if ( value > Short.MAX_VALUE ) {
			throw new CoercionException( "Cannot coerce Integer value `" + value + "` as Short : overflow" );
		}

		if ( value < Short.MIN_VALUE ) {
			throw new CoercionException( "Cannot coerce Integer value `" + value + "` as Short : underflow" );
		}

		return value.shortValue();
	}

	public static Short toShort(Long value) {
		if ( value > Short.MAX_VALUE ) {
			throw new CoercionException( "Cannot coerce Long value `" + value + "` as Short : overflow" );
		}

		if ( value < Short.MIN_VALUE ) {
			throw new CoercionException( "Cannot coerce Long value `" + value + "` as Short : underflow" );
		}

		return value.shortValue();
	}

	public static Short toShort(Double doubleValue) {
		if ( ! isWholeNumber( doubleValue ) ) {
			throw new CoercionException( "Cannot coerce Double value `" + doubleValue + "` as Short : not a whole number" );
		}
		return toShort( doubleValue.longValue() );
	}

	public static Short toShort(Float floatValue) {
		if ( ! isWholeNumber( floatValue ) ) {

View on GitHub (pinned to fad1729dce)

Solutions

  1. Widen the attribute type to Long/Integer if the domain legitimately exceeds the short range.
  2. Explicitly range-check (-32768..32767) and cast `(short) longValue` where fit is guaranteed.
  3. Fix producers that use Long for quantities that are actually small integers.
  4. For unsigned SMALLINT data, map the attribute as Integer with an appropriate JDBC type.

Example fix

// before
Long visitCount = counterService.currentCount();  // e.g. 50000
stats.setVisitCount(visitCount);                  // Short field -> overflow

// after
if (visitCount < Short.MIN_VALUE || visitCount > Short.MAX_VALUE) {
    throw new IllegalArgumentException("visit count exceeds short range: " + visitCount);
}
stats.setVisitCount(visitCount.shortValue());
// or change visitCount attribute/column to Long/BIGINT
Defensive patterns

Strategy: validation

Validate before calling

Long v = (Long) raw;
if (v < Short.MIN_VALUE || v > Short.MAX_VALUE) throw new IllegalArgumentException("out of short range: " + v);
stats.setVisitCount(v.shortValue());

Type guard

static boolean fitsInShort(Long v) { return v != null && v >= (long) Short.MIN_VALUE && v <= (long) Short.MAX_VALUE; }

Try / catch

try { session.persist(stats); } catch (CoercionException e) { log and reject with field-level message; }

Prevention

When it happens

Trigger: `ShortJavaType.coerce(value)` with a Long > 32767: assigning a long variable to a Short field via a Number/Object-typed setter, `setParameter("s", 70000L)` on a Short-typed path, or merging a detached entity whose Short property was loaded from a Long-valued JSON field.

Common situations: IDs/counts computed as long feeding SMALLINT-mapped Short fields; Jackson configured to deserialize ints as Long; unsigned SMALLINT columns (values up to 65535) on MySQL/MariaDB mapped as Short; migration from Hibernate 5 where oversized longs truncated silently.

Related errors


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