hibernate/hibernate-orm · error · IllegalArgumentException
Cannot parse given string into array of Long. First and last
Error message
Cannot parse given string into array of Long. First and last character must be { and } What it means
LongPrimitiveArrayJavaType maps long[] attributes (e.g. @JdbcTypeCode(SqlTypes.LONG_ARRAY)). fromString() rebuilds the array from the database text form and requires a SQL array literal bounded by '{' and '}'. Strings in any other shape - JSON style '[1,2,3]', a bare comma list '1,2,3', or a truncated value - throw IllegalArgumentException before any element is parsed.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LongPrimitiveArrayJavaType.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 long[] fromString(CharSequence charSequence) {
if ( charSequence == null ) {
return null;
}
final List<Long> 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 Long. 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( Long.parseLong( charSequence, elementStart, i, 10 ) );
elementStart = i + 1;
}
}
final long[] result = new long[list.size()];
for ( int i = 0; i < result.length; i ++ ) {
result[ i ] = list.get( i );
}
return result;
}
@OverrideView on GitHub (pinned to fad1729dce)
Solutions
- Store the text as a SQL array literal: '{1,2,3}' (empty array as '{}')
- Prefer a native array column type (e.g. PostgreSQL bigint[]) so the driver/JDBC binding is used instead of string parsing
- Add an AttributeConverter or mapping change for JSON-style storage (e.g. hibernate-types JSON type) instead of the primitive-array JavaType
- Fix existing rows with an UPDATE that rewrites '[..]' to '{..}'
Example fix
// before
UPDATE user_tags SET ids = '[1,2,3]';
// loading the long[] attribute -> IllegalArgumentException
// after
UPDATE user_tags SET ids = '{1,2,3}'; Defensive patterns
Strategy: validation
Validate before calling
static boolean isSqlArrayLiteral(CharSequence s) {
return s != null && s.length() >= 2 && s.charAt(0) == '{' && s.charAt(s.length()-1) == '}';
}
if (!isSqlArrayLiteral(text)) throw new IllegalArgumentException("Expected '{1,2,3}': " + text); Type guard
static long[] tryParseLongArray(String s) {
if (!isSqlArrayLiteral(s)) return null;
try { return LongJavaTypeArrayParse(s); } catch (RuntimeException e) { return null; }
} Try / catch
catch (IllegalArgumentException e) {
// message: First and last character must be { and }
throw new IllegalArgumentException("Column must hold a SQL array literal like '{1,2,3}': " + raw, e);
} Prevention
- Write '{1,2,3}' not JSON '[1,2,3]' into columns backing long[] attributes
- Prefer native array columns (PostgreSQL bigint[]) over text storage
- Validate the literal shape at every write path (API, ETL, seed files)
When it happens
Trigger: The column backing a long[] attribute is a varchar holding '[1,2,3]' (written by Jackson/Gson) instead of '{1,2,3}'; data produced by another framework or hand-written SQL inserts without braces; using array mapping on a database without native array support so Hibernate must round-trip through text
Common situations: Migrating array data from PostgreSQL ('{1,2,3}') to a database where the app or driver wrote JSON arrays; REST payloads persisted directly into the column; seed files with the wrong literal style.
Related errors
- Array element type error
- Illegal null value for array index encountered while reading
- Nested arrays (with the exception of byte[][]) are not suppo
- Basic array has element type '" + componentJavaType.getTypeN
- Cannot parse given string into array of strings. First and l
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/73df458bc1b7087a.
Report an issue: GitHub.