hibernate/hibernate-orm · error · UnsupportedOperationException

Type " + getTypeName() + " does not support conversion from

Error message

Type " + getTypeName() + " does not support conversion from String

What it means

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.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BasicJavaType.java:37

	 * for this Java type. Often, but not always, the source of this
	 * recommendation is the JDBC specification.
	 *
	 * @param indicators Contextual information
	 *
	 * @return The recommended SQL type descriptor
	 */
	default JdbcType getRecommendedJdbcType(JdbcTypeIndicators indicators) {
		// match legacy behavior
		final int jdbcTypeCode = JdbcTypeJavaClassMappings.INSTANCE.determineJdbcTypeCodeForJavaClass( getJavaTypeClass() );
		final var descriptor = indicators.getJdbcType( indicators.resolveJdbcTypeCode( jdbcTypeCode ) );
		return descriptor instanceof AdjustableJdbcType adjustableJdbcType
				? adjustableJdbcType.resolveIndicatedType( indicators, this )
				: descriptor;
	}

	@Override
	default T fromString(CharSequence string) {
		throw new UnsupportedOperationException( "Type " + getTypeName()
						+ " does not support conversion from String");
	}
}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Implement fromString(CharSequence) in the custom JavaType (mirror of toString)
  2. If the type is string-representable, also keep toString consistent so round-trips work
  3. Map string columns to String and convert at the application edge, or use an AttributeConverter<String, CustomType>
  4. Avoid forcing the custom type on native query scalars that return raw strings

Example fix

// before
public class YearMonthJavaType extends AbstractClassJavaType<YearMonth> {
    // no fromString -> 'Type ... does not support conversion from String'
}
// after
public class YearMonthJavaType extends AbstractClassJavaType<YearMonth> {
    public YearMonthJavaType() { super(YearMonth.class); }

    @Override
    public YearMonth fromString(CharSequence string) {
        return string == null ? null : YearMonth.parse(string);
    }
}
Defensive patterns

Strategy: validation

Validate before calling

// contract test for any custom basic JavaType: must round-trip through String
String s = javaType.toString(sample);
assertNotNull(javaType.fromString(s)); // fails fast at bootstrap instead of at query time

Type guard

static boolean supportsFromString(JavaType<?> jt) {
    try { jt.fromString(jt.toString(sample)); return true; }
    catch (UnsupportedOperationException e) { return false; }
}

Try / catch

try {
    return query.setParameter("code", "ABC-1").getSingleResult();
} catch (UnsupportedOperationException e) {
    if (String.valueOf(e.getMessage()).contains("does not support conversion from String")) {
        // custom JavaType lacks fromString: implement it, or bind the typed value directly
        query.setParameter("code", new Code("ABC-1"));
        return query.getSingleResult();
    }
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/88ffd59938e5c192. Report an issue: GitHub.