{"record":{"id":"2eb0855d8edfbef6","repo":"hibernate/hibernate-orm","slug":"unable-to-convert-string-s-to-url-s","errorCode":null,"errorMessage":"Unable to convert string [%s] to URL : %s","messagePattern":"Unable to convert string \\[(.+?)\\] to URL : (.+?)","errorType":"exception","errorClass":"CoercionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/UrlJavaType.java","lineNumber":56,"sourceCode":"\tpublic JdbcType getRecommendedJdbcType(JdbcTypeIndicators context) {\n\t\treturn context.getJdbcType( SqlTypes.VARCHAR );\n\t}\n\n\t@Override\n\tpublic boolean useObjectEqualsHashCode() {\n\t\treturn true;\n\t}\n\n\tpublic String toString(URL value) {\n\t\treturn value.toExternalForm();\n\t}\n\n\tpublic URL fromString(CharSequence string) {\n\t\ttry {\n\t\t\treturn new URL( string.toString() );\n\t\t}\n\t\tcatch ( MalformedURLException e ) {\n\t\t\tthrow new CoercionException( \"Unable to convert string [\" + string + \"] to URL : \" + e );\n\t\t}\n\t}\n\n\tpublic <X> X unwrap(URL value, Class<X> type, WrapperOptions options) {\n\t\tif ( value == null ) {\n\t\t\treturn null;\n\t\t}\n\t\tif ( URL.class.isAssignableFrom( type ) ) {\n\t\t\treturn type.cast( value );\n\t\t}\n\t\tif ( String.class.isAssignableFrom( type ) ) {\n\t\t\treturn type.cast( toString( value ) );\n\t\t}\n\t\tthrow unknownUnwrap( type );\n\t}\n\n\tpublic <X> URL wrap(X value, WrapperOptions options) {\n\t\tif ( value == null ) {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/UrlJavaType.java#L38-L74","documentation":"UrlJavaType is the basic type descriptor for java.net.URL attributes. Whenever a URL must be rebuilt from its string form (row load, query-parameter coercion, AttributeConverter input), it calls new URL(String) and wraps any MalformedURLException in a CoercionException. The message shows both the offending string and the underlying JDK parse reason.","triggerScenarios":"Loading an entity whose java.net.URL column contains a non-URL string; binding a String parameter into a URL-typed attribute; criteria/coercion calls that convert arbitrary strings to URL; importing rows where the column holds relative paths or placeholder text.","commonSituations":"Relative URLs like '/docs/page' (no protocol) stored in the column; migrated or user-submitted data containing 'N/A', empty strings or strings with unencoded spaces; switching an attribute from String to URL without cleansing existing rows.","solutions":["Fix the stored data: every row must contain an absolute, well-formed URL including protocol and host","Map the column as String or java.net.URI instead — URI tolerates relative references that URL rejects","Add an AttributeConverter that normalizes values (e.g. prefixes 'http://') before new URL() runs","Validate URLs at write time so malformed values never reach the database"],"exampleFix":"// before\n@Entity class Link {\n    @Column(name = \"target\") URL target; // column holds '/docs/page' -> throws on load\n}\n\n// after\n@Entity class Link {\n    @Column(name = \"target\") String target; // or java.net.URI, which parses relative refs\n}","handlingStrategy":"try-catch","validationCode":"static boolean isAbsoluteUrl(String s) {\n    if (s == null || s.isBlank()) return false;\n    try { new java.net.URL(s); return true; }\n    catch (java.net.MalformedURLException e) { return false; }\n}","typeGuard":null,"tryCatchPattern":"try {\n    Link link = session.find(Link.class, id);\n} catch (org.hibernate.type.descriptor.java.CoercionException e) {\n    java.net.MalformedURLException cause = (java.net.MalformedURLException) e.getCause();\n    // route to data-repair path (skip row, queue for cleanup) instead of failing the whole load\n}","preventionTips":["Prefer String or java.net.URI column types when data may contain relative URLs","Validate URLs at write time (isAbsoluteUrl-style guard) before persisting","Cleanse imported data before it reaches URL-typed columns","Normalize/encode URLs (URI.normalize) before saving"],"tags":["hibernate","orm","url","coercion","data-quality"],"backgroundTag":"malformed-url","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}