hibernate/hibernate-orm · error · IllegalStateException

Illegal attempt to use interval second type with scale > 6

Error message

Illegal attempt to use interval second type with scale > 6

What it means

Scale6IntervalSecondDdlType is the DDL type the PostgreSQL, CockroachDB and GaussDB dialects register for SqlTypes.INTERVAL_SECOND ('interval second($s)'). PostgreSQL caps interval-second precision at 6 fractional digits (microseconds), so getTypeName refuses to render a type name when the requested scale is null OR greater than 6 - it throws IllegalStateException rather than silently emitting invalid DDL. Note that scale == null also throws: the pattern needs an explicit $s value.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/internal/Scale6IntervalSecondDdlType.java:28

import org.hibernate.type.Type;
import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry;

public class Scale6IntervalSecondDdlType extends DdlTypeImpl {

	public Scale6IntervalSecondDdlType(Dialect dialect) {
		this( "interval second($s)", dialect );
	}

	public Scale6IntervalSecondDdlType(String typeNamePattern, Dialect dialect) {
		super( SqlTypes.INTERVAL_SECOND, typeNamePattern, dialect );
	}

	@Override
	public String getTypeName(Size columnSize, Type type, DdlTypeRegistry ddlTypeRegistry) {
		final Integer scale = columnSize.getScale();
		// The maximum scale for `interval second` is 6 unfortunately
		if ( scale == null || scale > 6 ) {
			throw new IllegalStateException( "Illegal attempt to use interval second type with scale > 6" );
		}
		return formatTypeName( columnSize.getLength(), columnSize.getPrecision(), scale );
	}
}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Set an explicit scale between 0 and 6: @Column(scale = 6) together with @JdbcTypeCode(SqlTypes.INTERVAL_SECOND)
  2. If you need sub-microsecond precision, map Duration to a numeric/bigint column instead of INTERVAL_SECOND
  3. For schema validation, align the expected scale with the existing column (must be <= 6)

Example fix

// before
@JdbcTypeCode(SqlTypes.INTERVAL_SECOND)
@Column(scale = 9) // nanoseconds -> 'Illegal attempt to use interval second type with scale > 6'
private Duration elapsed;

// after
@JdbcTypeCode(SqlTypes.INTERVAL_SECOND)
@Column(scale = 6) // microseconds, the PostgreSQL maximum
private Duration elapsed;
Defensive patterns

Strategy: validation

Validate before calling

// Build-time check: INTERVAL_SECOND mappings must declare an explicit scale 0..6
Column col = field.getAnnotation(Column.class);
if (field.isAnnotationPresent(JdbcTypeCode.class)
        && ((JdbcTypeCode) field.getAnnotation(JdbcTypeCode.class)).value() == SqlTypes.INTERVAL_SECOND) {
    if (col == null || col.scale() < 0 || col.scale() > 6) {
        throw new IllegalStateException("Duration field " + field.getName() + " needs @Column(scale = 0..6) for INTERVAL_SECOND");
    }
}

Prevention

When it happens

Trigger: Schema export/update/validate (hbm2ddl.auto, SchemaExport/SchemaValidator) for a Duration attribute mapped with @JdbcTypeCode(SqlTypes.INTERVAL_SECOND) where scale is unset (null) or explicitly > 6, e.g. @Column(scale = 9) to get nanosecond precision.

Common situations: Porting nanosecond-precision Duration mappings from dialects that allow them; copying precision/scale hints from numeric columns onto Duration fields; upgrading to a Hibernate version where Duration defaults to INTERVAL_SECOND on PostgreSQL; leaving scale unspecified and tripping the scale == null half of the guard.

Related errors


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