{"record":{"id":"73df458bc1b7087a","repo":"hibernate/hibernate-orm","slug":"cannot-parse-given-string-into-array-of-long-firs","errorCode":null,"errorMessage":"Cannot parse given string into array of Long. First and last character must be { and }","messagePattern":"Cannot parse given string into array of Long\\. First and last character must be (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LongPrimitiveArrayJavaType.java","lineNumber":89,"sourceCode":"\t\tsb.append( value[0] );\n\t\tfor ( int i = 1; i < value.length; i++ ) {\n\t\t\tsb.append( value[i] );\n\t\t\tsb.append( ',' );\n\t\t}\n\t\tsb.append( '}' );\n\t\treturn sb.toString();\n\t}\n\n\t@Override\n\tpublic long[] fromString(CharSequence charSequence) {\n\t\tif ( charSequence == null ) {\n\t\t\treturn null;\n\t\t}\n\t\tfinal List<Long> list = new ArrayList<>();\n\t\tfinal char lastChar = charSequence.charAt( charSequence.length() - 1 );\n\t\tfinal char firstChar = charSequence.charAt( 0 );\n\t\tif ( firstChar != '{' || lastChar != '}' ) {\n\t\t\tthrow new IllegalArgumentException( \"Cannot parse given string into array of Long. First and last character must be { and }\" );\n\t\t}\n\t\tfinal int len = charSequence.length();\n\t\tint elementStart = 1;\n\t\tfor ( int i = elementStart; i < len; i ++ ) {\n\t\t\tfinal char c = charSequence.charAt( i );\n\t\t\tif ( c == ',' ) {\n\t\t\t\tlist.add( Long.parseLong( charSequence, elementStart, i, 10 ) );\n\t\t\t\telementStart = i + 1;\n\t\t\t}\n\t\t}\n\t\tfinal long[] result = new long[list.size()];\n\t\tfor ( int i = 0; i < result.length; i ++ ) {\n\t\t\tresult[ i ] = list.get( i );\n\t\t}\n\t\treturn result;\n\t}\n\n\t@Override","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LongPrimitiveArrayJavaType.java#L71-L107","documentation":"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.","triggerScenarios":"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","commonSituations":"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.","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 '{..}'"],"exampleFix":"// before\nUPDATE user_tags SET ids = '[1,2,3]';\n// loading the long[] attribute -> IllegalArgumentException\n\n// after\nUPDATE user_tags SET ids = '{1,2,3}';","handlingStrategy":"validation","validationCode":"static boolean isSqlArrayLiteral(CharSequence s) {\n    return s != null && s.length() >= 2 && s.charAt(0) == '{' && s.charAt(s.length()-1) == '}';\n}\n\nif (!isSqlArrayLiteral(text)) throw new IllegalArgumentException(\"Expected '{1,2,3}': \" + text);","typeGuard":"static long[] tryParseLongArray(String s) {\n    if (!isSqlArrayLiteral(s)) return null;\n    try { return LongJavaTypeArrayParse(s); } catch (RuntimeException e) { return null; }\n}","tryCatchPattern":"catch (IllegalArgumentException e) {\n    // message: First and last character must be { and }\n    throw new IllegalArgumentException(\"Column must hold a SQL array literal like '{1,2,3}': \" + raw, e);\n}","preventionTips":["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)"],"tags":["hibernate","array-mapping","long-array","sql-array-literal","string-parsing"],"backgroundTag":"sql-array-parse-error","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}