{"record":{"id":"57fdbdb8f227e5a3","repo":"hibernate/hibernate-orm","slug":"cannot-parse-given-string-into-array-of-shorts-fi","errorCode":null,"errorMessage":"Cannot parse given string into array of Shorts. First and last character must be { and }","messagePattern":"Cannot parse given string into array of Shorts\\. 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/ShortPrimitiveArrayJavaType.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 short[] fromString(CharSequence charSequence) {\n\t\tif ( charSequence == null ) {\n\t\t\treturn null;\n\t\t}\n\t\tfinal List<Short> 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 Shorts. 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( Short.parseShort( charSequence.subSequence( elementStart, i ).toString(), 10 ) );\n\t\t\t\telementStart = i + 1;\n\t\t\t}\n\t\t}\n\t\tfinal short[] result = new short[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/ShortPrimitiveArrayJavaType.java#L71-L107","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Store or pass the value exactly in brace format, e.g. \"{1,2,3}\" — leading '{' and trailing '}' are mandatory","Remove native-SQL transformations such as array_to_string()/array_agg() so the array literal reaches Hibernate unchanged","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","Pass null rather than an empty or malformed string when there is no value"],"exampleFix":"// before\nString raw = \"1,2,3\"; // plain CSV, no braces\nshort[] arr = ShortPrimitiveArrayJavaType.INSTANCE.fromString(raw); // throws\n\n// after\nString raw = \"{1,2,3}\"; // brace-delimited literal, matching toString() output\nshort[] arr = ShortPrimitiveArrayJavaType.INSTANCE.fromString(raw);","handlingStrategy":"validation","validationCode":"static boolean isParsableShortArrayLiteral(String s) {\n    return s == null\n        || (s.length() >= 2 && s.charAt(0) == '{' && s.charAt(s.length() - 1) == '}');\n}\n\n// use before any string -> short[] conversion\nif (!isParsableShortArrayLiteral(raw)) throw new IllegalArgumentException(\"expected {..} literal: \" + raw);","typeGuard":null,"tryCatchPattern":"try {\n    short[] v = shortArrayJavaType.fromString(raw);\n} catch (IllegalArgumentException e) {\n    // bad literal: log the raw value, fall back to empty array or fail the row explicitly\n    return new short[0];\n}","preventionTips":["Always produce array text in PostgreSQL {..} literal form, exactly as toString() emits","Never strip braces when copying array values between columns or systems","Prefer native array types over text round-trips for array columns","Round-trip one write+read per array-mapped entity in tests"],"tags":["hibernate","orm","array","parsing","short","sql-array"],"backgroundTag":"array-string-parse-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}