{"record":{"id":"3ac9f86f4948f499","repo":"hibernate/hibernate-orm","slug":"illegal-attempt-to-use-interval-second-type-with-s","errorCode":null,"errorMessage":"Illegal attempt to use interval second type with scale > 6","messagePattern":"Illegal attempt to use interval second type with scale > 6","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/internal/Scale6IntervalSecondDdlType.java","lineNumber":28,"sourceCode":"import org.hibernate.type.Type;\nimport org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry;\n\npublic class Scale6IntervalSecondDdlType extends DdlTypeImpl {\n\n\tpublic Scale6IntervalSecondDdlType(Dialect dialect) {\n\t\tthis( \"interval second($s)\", dialect );\n\t}\n\n\tpublic Scale6IntervalSecondDdlType(String typeNamePattern, Dialect dialect) {\n\t\tsuper( SqlTypes.INTERVAL_SECOND, typeNamePattern, dialect );\n\t}\n\n\t@Override\n\tpublic String getTypeName(Size columnSize, Type type, DdlTypeRegistry ddlTypeRegistry) {\n\t\tfinal Integer scale = columnSize.getScale();\n\t\t// The maximum scale for `interval second` is 6 unfortunately\n\t\tif ( scale == null || scale > 6 ) {\n\t\t\tthrow new IllegalStateException( \"Illegal attempt to use interval second type with scale > 6\" );\n\t\t}\n\t\treturn formatTypeName( columnSize.getLength(), columnSize.getPrecision(), scale );\n\t}\n}\n","sourceCodeStart":10,"sourceCodeEnd":33,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/internal/Scale6IntervalSecondDdlType.java#L10-L33","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set an explicit scale between 0 and 6: @Column(scale = 6) together with @JdbcTypeCode(SqlTypes.INTERVAL_SECOND)","If you need sub-microsecond precision, map Duration to a numeric/bigint column instead of INTERVAL_SECOND","For schema validation, align the expected scale with the existing column (must be <= 6)"],"exampleFix":"// before\n@JdbcTypeCode(SqlTypes.INTERVAL_SECOND)\n@Column(scale = 9) // nanoseconds -> 'Illegal attempt to use interval second type with scale > 6'\nprivate Duration elapsed;\n\n// after\n@JdbcTypeCode(SqlTypes.INTERVAL_SECOND)\n@Column(scale = 6) // microseconds, the PostgreSQL maximum\nprivate Duration elapsed;","handlingStrategy":"validation","validationCode":"// Build-time check: INTERVAL_SECOND mappings must declare an explicit scale 0..6\nColumn col = field.getAnnotation(Column.class);\nif (field.isAnnotationPresent(JdbcTypeCode.class)\n        && ((JdbcTypeCode) field.getAnnotation(JdbcTypeCode.class)).value() == SqlTypes.INTERVAL_SECOND) {\n    if (col == null || col.scale() < 0 || col.scale() > 6) {\n        throw new IllegalStateException(\"Duration field \" + field.getName() + \" needs @Column(scale = 0..6) for INTERVAL_SECOND\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pair @JdbcTypeCode(SqlTypes.INTERVAL_SECOND) with an explicit @Column(scale <= 6)","Run SchemaExport in CI so DDL failures surface at build time","Map sub-microsecond durations to numeric columns instead"],"tags":["hibernate","postgresql","ddl","duration","interval","schema-export"],"backgroundTag":"interval-second-scale-limit","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}