hibernate/hibernate-orm · error · CoercionException

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

Error message

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

What it means

Hibernate throws this CoercionException when an Integer must be narrowed to Short (range -32768..32767) and exceeds 32767. It comes from CoercionHelper.toShort(Integer), called by ShortJavaType.coerce when an Integer value is supplied to a Short-mapped attribute (typical SMALLINT column). Hibernate 6 validates narrowing and reports the offending value instead of silently truncating like a Java `(short)` cast.

Source

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

		return value.byteValue();
	}

	public static Byte toByte(BigInteger value) {
		return coerceWrappingError( value::byteValueExact );
	}

	public static Byte toByte(BigDecimal value) {
		return coerceWrappingError( value::byteValueExact );
	}

	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" );
		}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Widen the mapping if large values are legitimate: attribute Short -> Integer and column SMALLINT -> INT.
  2. Narrow explicitly after validation if the domain guarantees fit: check -32768..32767 then `(short) intValue`.
  3. Fix DTO/producer types so integral fields are Short/Integer consistently end to end.
  4. For unsigned SMALLINT data, map to Integer with an appropriate JdbcType instead of Short.

Example fix

// before
@Entity class PortConfig {
    Short port;                   // smallint column
}
cfg.setPort(80800);              // Integer 80800 -> CoercionException: overflow

// after
@Entity class PortConfig {
    Integer port;                 // int column
}
// or guard:
if (portValue < Short.MIN_VALUE || portValue > Short.MAX_VALUE) throw new IllegalArgumentException("port too large");
cfg.setPort((short) portValue);
Defensive patterns

Strategy: validation

Validate before calling

Integer v = dto.getPort();
if (v < Short.MIN_VALUE || v > Short.MAX_VALUE) throw new IllegalArgumentException("port out of short range: " + v);
cfg.setPort(v.shortValue());

Type guard

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

Try / catch

try { session.merge(cfg); } catch (CoercionException e) { throw new ValidationException("port value not representable as smallint", e); }

Prevention

When it happens

Trigger: `ShortJavaType.coerce(value)` with an Integer > 32767: `entity.setCode(intValue)` on a Short field via a Number/Object-typed setter, `setParameter("s", 100000)` bound to a Short path, or session.merge on a graph where the Short property holds an Integer (e.g. from JSON deserialization).

Common situations: Year 50000-style values, ports, or counters stored in SMALLINT-mapped Short fields; Jackson deserializing JSON ints as Integer into DTOs copied onto Short entities; unsigned SMALLINT columns on MySQL holding 32768..65535; migrating from Hibernate 5 which truncated silently.

Related errors


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