hibernate/hibernate-orm · error · IllegalArgumentException

Cannot parse given string into array of Shorts. First and la

Error message

Cannot parse given string into array of Shorts. First and last character must be { and }

What it means

ShortPrimitiveArrayJavaType is Hibernate's JavaType descriptor for primitive short[] mapped to array columns. Its fromString() rebuilds a short[] from the string representation of the array, which must be the PostgreSQL-style brace literal that toString() produces, e.g. {1,2,3}. If the first character is not '{' or the last is not '}', parsing aborts immediately with this IllegalArgumentException.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ShortPrimitiveArrayJavaType.java:89

		sb.append( value[0] );
		for ( int i = 1; i < value.length; i++ ) {
			sb.append( value[i] );
			sb.append( ',' );
		}
		sb.append( '}' );
		return sb.toString();
	}

	@Override
	public short[] fromString(CharSequence charSequence) {
		if ( charSequence == null ) {
			return null;
		}
		final List<Short> list = new ArrayList<>();
		final char lastChar = charSequence.charAt( charSequence.length() - 1 );
		final char firstChar = charSequence.charAt( 0 );
		if ( firstChar != '{' || lastChar != '}' ) {
			throw new IllegalArgumentException( "Cannot parse given string into array of Shorts. First and last character must be { and }" );
		}
		final int len = charSequence.length();
		int elementStart = 1;
		for ( int i = elementStart; i < len; i ++ ) {
			final char c = charSequence.charAt( i );
			if ( c == ',' ) {
				list.add( Short.parseShort( charSequence.subSequence( elementStart, i ).toString(), 10 ) );
				elementStart = i + 1;
			}
		}
		final short[] result = new short[list.size()];
		for ( int i = 0; i < result.length; i ++ ) {
			result[ i ] = list.get( i );
		}
		return result;
	}

	@Override

View on GitHub (pinned to fad1729dce)

Solutions

  1. Store or pass the value exactly in brace format, e.g. "{1,2,3}" — leading '{' and trailing '}' are mandatory
  2. Remove native-SQL transformations such as array_to_string()/array_agg() so the array literal reaches Hibernate unchanged
  3. Map the column with a native array JdbcType (@JdbcTypeCode(SqlTypes.ARRAY) on a dialect like PostgreSQL) so the driver returns a java.sql.Array instead of a String
  4. Pass null rather than an empty or malformed string when there is no value

Example fix

// before
String raw = "1,2,3"; // plain CSV, no braces
short[] arr = ShortPrimitiveArrayJavaType.INSTANCE.fromString(raw); // throws

// after
String raw = "{1,2,3}"; // brace-delimited literal, matching toString() output
short[] arr = ShortPrimitiveArrayJavaType.INSTANCE.fromString(raw);
Defensive patterns

Strategy: validation

Validate before calling

static boolean isParsableShortArrayLiteral(String s) {
    return s == null
        || (s.length() >= 2 && s.charAt(0) == '{' && s.charAt(s.length() - 1) == '}');
}

// use before any string -> short[] conversion
if (!isParsableShortArrayLiteral(raw)) throw new IllegalArgumentException("expected {..} literal: " + raw);

Try / catch

try {
    short[] v = shortArrayJavaType.fromString(raw);
} catch (IllegalArgumentException e) {
    // bad literal: log the raw value, fall back to empty array or fail the row explicitly
    return new short[0];
}

Prevention

When it happens

Trigger: Any string-based path that materializes a short[] attribute: a varchar/text column mapped to short[], a native query returning array_to_string(...) output or another tool's plain '1,2,3' string, second-level cache round-trips through the string form, or calling the descriptor's fromString() directly with a non-braced value.

Common situations: Data files or ETL jobs loading comma-joined values into an array-typed column; native SQL that strips the braces before the value reaches Hibernate; databases without native array support where the driver hands back plain strings; hand-written fixtures using '1,2,3' notation.

Related errors


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