hibernate/hibernate-orm · error · IllegalArgumentException

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

Error message

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

What it means

FloatPrimitiveArrayJavaType.fromString reconstructs a float[] from the array-literal string form Hibernate uses for array-valued columns ('{1.0,2.0}'). It throws IllegalArgumentException when the first and last characters are not '{' and '}' — the stored text is not in the expected array-literal shape.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/java/FloatPrimitiveArrayJavaType.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 float[] fromString(CharSequence charSequence) {
		if ( charSequence == null ) {
			return null;
		}
		final List<Float> 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 Floats. 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( Float.parseFloat( charSequence.subSequence( elementStart, i ).toString() ) );
				elementStart = i + 1;
			}
		}
		final float[] result = new float[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. Normalize stored data to the '{e1,e2}' form Hibernate emits
  2. Add an AttributeConverter<float[], String> that tolerates other formats on read
  3. Use a native ARRAY column type with @JdbcTypeCode(SqlTypes.ARRAY) where the dialect supports it
  4. Ensure a single writer for array-as-text columns

Example fix

// before: column text is '[1.5, 2.5]' -> IllegalArgumentException

// after: normalize before Hibernate parses, or fix the data
UPDATE mytable SET vals = '{' || substring(vals from 2 for length(vals) - 2) || '}' WHERE vals LIKE '[%]';
Defensive patterns

Strategy: validation

Validate before calling

static boolean isArrayLiteral(CharSequence s) {
    return s != null && s.length() >= 2
            && s.charAt(0) == '{' && s.charAt(s.length() - 1) == '}';
}

Prevention

When it happens

Trigger: Reading a float[] attribute whose column text was written by another producer in a different format ('[1.0, 2.0]', '1.0;2.0'); hand-migrated data; dialect or mapping changes that altered the array rendering.

Common situations: Databases storing arrays as text without native ARRAY support; ETL imports; switching mappings between SqlTypes.ARRAY and varchar-backed storage.

Related errors


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