hibernate/hibernate-orm · error · IllegalArgumentException

Cannot parse given string into array of strings. First and l

Error message

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

What it means

ArrayJavaType.fromString parses the PostgreSQL-style array literal that some drivers/dialects hand back for array columns when no native array object is available. It requires the text to start with '{' and end with '}'; anything else is rejected with this IllegalArgumentException.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ArrayJavaType.java:194

			}
			sb.append( '"' );
			glue = ",";
		}
		sb.append( '}' );
		return sb.toString();
	}

	@Override
	public T[] fromString(CharSequence charSequence) {
		if ( charSequence == null ) {
			return null;
		}
		final var lst = new java.util.ArrayList<String>();
		StringBuilder sb = null;
		char lastChar = charSequence.charAt( charSequence.length() - 1 );
		char firstChar = charSequence.charAt( 0 );
		if ( firstChar != '{' || lastChar != '}' ) {
			throw new IllegalArgumentException( "Cannot parse given string into array of strings. First and last character must be { and }" );
		}
		int len = charSequence.length();
		boolean inquote = false;
		for ( int i = 1; i < len; i ++ ) {
			char c = charSequence.charAt( i );
			if ( c == '"' ) {
				if (inquote) {
					lst.add( sb.toString() );
				}
				else {
					sb = new StringBuilder();
				}
				inquote = !inquote;
				continue;
			}
			else if ( !inquote ) {
				if ( Character.isWhitespace( c ) ) {
					continue;

View on GitHub (pinned to fad1729dce)

Solutions

  1. Store/produce values in PostgreSQL array literal form: '{elem,elem}'
  2. Quote elements containing commas, braces or quotes: '{"a,b",c}' and escape embedded quotes/backslashes
  3. Convert the varchar column to a real array column (or map it as String and split in a converter)
  4. If a converter formats the relational form, emit '{' + joined + '}'

Example fix

// before
converter writes: "ONE,TWO,THREE"        // fromString -> IllegalArgumentException
stub value:     "[ONE, TWO, THREE]"       // Java Arrays.toString format
// after
converter writes: "{ONE,TWO,THREE}"      // valid PG array literal
stub value:     "{ONE,TWO,THREE}"
Defensive patterns

Strategy: validation

Validate before calling

// validate/normalize stored text before Hibernate parses it
static String toPgArrayLiteral(String raw) {
    String s = raw == null ? "{}" : raw.trim();
    if (!s.startsWith("{") || !s.endsWith("}")) {
        s = '{' + Arrays.stream(s.split(",")).map(String::trim)
                           .map(e -> e.matches("[A-Za-z0-9_]*") ? e : '"' + e + '"')
                           .collect(Collectors.joining(",")) + '}';
    }
    return s;
}

Type guard

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

Try / catch

try {
    return session.find(Document.class, id);
} catch (IllegalArgumentException e) {
    if (String.valueOf(e.getMessage()).contains("First and last character must be { and }")) {
        // column holds non-literal text: repair row or remap column as String + converter
        log.error("Malformed array literal in row {}", id);
    } else throw e;
}

Prevention

When it happens

Trigger: Reading an array-typed attribute where the JDBC driver returns the value as a String whose format is not '{...}' - e.g. application code or a converter stored 'a,b,c' instead of '{a,b,c}', or a raw VARCHAR column was mapped as an array type.

Common situations: Hand-written data or ETL loading arrays as delimited text; migrating a varchar column to an array mapping without converting data; custom converters producing non-PG-literal output; unit tests stubbing array values with plain comma-separated strings.

Related errors


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