hibernate/hibernate-orm · error · PersistenceException

Error attempting to apply AttributeConverter

Error message

Error attempting to apply AttributeConverter

What it means

Same read-path wrapping as its AttributeConverterBean counterpart, but thrown from AttributeConverterInstance - the wrapper Hibernate uses when it instantiates the converter itself (no bean manager). Any non-PersistenceException RuntimeException escaping convertToEntityAttribute during entity load becomes this PersistenceException with the original failure as cause.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/internal/AttributeConverterInstance.java:47

	public AttributeConverterInstance(
			AttributeConverter<O, R> converter,
			JavaType<O> domainJavaType,
			JavaType<R> jdbcJavaType) {
		this.converter = converter;
		this.domainJavaType = domainJavaType;
		this.jdbcJavaType = jdbcJavaType;
	}

	@Override
	public O toDomainValue(R relationalForm) {
		try {
			return converter.convertToEntityAttribute( relationalForm );
		}
		catch (PersistenceException pe) {
			throw pe;
		}
		catch (RuntimeException re) {
			throw new PersistenceException( "Error attempting to apply AttributeConverter", re );
		}
	}

	@Override
	public R toRelationalValue(O domainForm) {
		try {
			return converter.convertToDatabaseColumn( domainForm );
		}
		catch (PersistenceException pe) {
			throw pe;
		}
		catch (RuntimeException re) {
			throw new PersistenceException( "Error attempting to apply AttributeConverter: " + re.getMessage(), re );
		}
	}

	@Override
	public JavaType<O> getDomainJavaType() {

View on GitHub (pinned to fad1729dce)

Solutions

  1. Check the cause (getCause()) for the real converter exception
  2. Harden convertToEntityAttribute against null and unknown values
  3. Fix or migrate the stored data / column type so the relational form matches the converter's declared database-column type
  4. Reproduce with a direct unit test of the converter using the exact failing DB value

Example fix

// before
@Override
public MonetaryAmount convertToEntityAttribute(Long cents) {
    return MonetaryAmount.ofCents(cents); // NPE when column is NULL
}
// after
@Override
public MonetaryAmount convertToEntityAttribute(Long cents) {
    return cents == null ? null : MonetaryAmount.ofCents(cents);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-flight the distinct stored values through the converter before deploying
for (String v : rawColumnValues) {
    if (v != null && converter.convertToEntityAttribute(v) == null && !allowsNull) {
        throw new IllegalStateException("Row value not convertible: " + v);
    }
}

Type guard

// expose a non-throwing probe next to the converter
public static boolean canConvert(String dbValue) {
    if (dbValue == null) return true;
    try { convertToEntityAttributeImpl(dbValue); return true; }
    catch (RuntimeException e) { return false; }
}

Try / catch

try {
    return session.createQuery("from Order o where o.customer.id = :cid", Order.class)
                  .setParameter("cid", cid).list();
} catch (PersistenceException e) {
    if (e.getMessage() != null && e.getMessage().contains("apply AttributeConverter")) {
        // inspect e.getCause(): dirty data or type mismatch on a converted column
        throw new DataQualityException("Converted column holds bad data", e.getCause());
    }
    throw e;
}

Prevention

When it happens

Trigger: Loading/querying an entity with a @Convert attribute (or autoApply converter) where Hibernate directly instantiated the converter, and convertToEntityAttribute throws on the stored value - null column, unparsable string, unrecognized enum code.

Common situations: Dirty legacy data; column type change after a dialect or driver upgrade; converters that assume non-null input; native query results fed through a converter with values in an unexpected format.

Related errors


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