{"record":{"id":"88ffd59938e5c192","repo":"hibernate/hibernate-orm","slug":"type-gettypename-does-not-support-conver","errorCode":null,"errorMessage":"Type \" + getTypeName() + \" does not support conversion from String","messagePattern":"Type \" \\+ getTypeName\\(\\) \\+ \" does not support conversion from String","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BasicJavaType.java","lineNumber":37,"sourceCode":"\t * for this Java type. Often, but not always, the source of this\n\t * recommendation is the JDBC specification.\n\t *\n\t * @param indicators Contextual information\n\t *\n\t * @return The recommended SQL type descriptor\n\t */\n\tdefault JdbcType getRecommendedJdbcType(JdbcTypeIndicators indicators) {\n\t\t// match legacy behavior\n\t\tfinal int jdbcTypeCode = JdbcTypeJavaClassMappings.INSTANCE.determineJdbcTypeCodeForJavaClass( getJavaTypeClass() );\n\t\tfinal var descriptor = indicators.getJdbcType( indicators.resolveJdbcTypeCode( jdbcTypeCode ) );\n\t\treturn descriptor instanceof AdjustableJdbcType adjustableJdbcType\n\t\t\t\t? adjustableJdbcType.resolveIndicatedType( indicators, this )\n\t\t\t\t: descriptor;\n\t}\n\n\t@Override\n\tdefault T fromString(CharSequence string) {\n\t\tthrow new UnsupportedOperationException( \"Type \" + getTypeName()\n\t\t\t\t\t\t+ \" does not support conversion from String\");\n\t}\n}\n","sourceCodeStart":19,"sourceCodeEnd":41,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BasicJavaType.java#L19-L41","documentation":"BasicJavaType.fromString has no default string-to-value conversion: any basic JavaType that does not override it throws UnsupportedOperationException when Hibernate must materialize the value from its String form. The message names the JavaType whose string round-trip is missing.","triggerScenarios":"A custom basic JavaType without a fromString override used where Hibernate needs to build values from text: query string literals ('from x where e.code = :p' with a string parameter bound to the typed attribute), IN-expansion literals, string-backed column reads (varchar column mapped to a typed attribute), or cache/replication paths that move basic values through strings.","commonSituations":"Writing a custom BasicJavaType and forgetting fromString; native queries returning VARCHAR that get addScalar'ed to the custom type; criteria literals serialized as strings; Hibernate 6+ stricter about which types can be created from literals.","solutions":["Implement fromString(CharSequence) in the custom JavaType (mirror of toString)","If the type is string-representable, also keep toString consistent so round-trips work","Map string columns to String and convert at the application edge, or use an AttributeConverter<String, CustomType>","Avoid forcing the custom type on native query scalars that return raw strings"],"exampleFix":"// before\npublic class YearMonthJavaType extends AbstractClassJavaType<YearMonth> {\n    // no fromString -> 'Type ... does not support conversion from String'\n}\n// after\npublic class YearMonthJavaType extends AbstractClassJavaType<YearMonth> {\n    public YearMonthJavaType() { super(YearMonth.class); }\n\n    @Override\n    public YearMonth fromString(CharSequence string) {\n        return string == null ? null : YearMonth.parse(string);\n    }\n}","handlingStrategy":"validation","validationCode":"// contract test for any custom basic JavaType: must round-trip through String\nString s = javaType.toString(sample);\nassertNotNull(javaType.fromString(s)); // fails fast at bootstrap instead of at query time","typeGuard":"static boolean supportsFromString(JavaType<?> jt) {\n    try { jt.fromString(jt.toString(sample)); return true; }\n    catch (UnsupportedOperationException e) { return false; }\n}","tryCatchPattern":"try {\n    return query.setParameter(\"code\", \"ABC-1\").getSingleResult();\n} catch (UnsupportedOperationException e) {\n    if (String.valueOf(e.getMessage()).contains(\"does not support conversion from String\")) {\n        // custom JavaType lacks fromString: implement it, or bind the typed value directly\n        query.setParameter(\"code\", new Code(\"ABC-1\"));\n        return query.getSingleResult();\n    }\n    throw e;\n}","preventionTips":["Implement fromString in every custom basic JavaType (mirror toString)","Bind typed values instead of raw strings where possible","Add string round-trip tests for custom types in CI","Use AttributeConverter for custom value classes instead of hand-written JavaTypes"],"tags":["hibernate","orm","custom-type","type-conversion","query-literals","unsupported-operation"],"backgroundTag":"unsupported-java-type-conversion","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}