hibernate/hibernate-orm · error · PersistenceException

Error attempting to apply AttributeConverter: " + re.getMess

Error message

Error attempting to apply AttributeConverter: " + re.getMessage()

What it means

Hibernate wraps any non-PersistenceException RuntimeException thrown by your AttributeConverter.convertToDatabaseColumn while translating a domain value into its relational form for writing. This variant comes from AttributeConverterBean (managed-bean converter wrapper), so it fires on the write path: INSERT/UPDATE statements and converter-bound query parameters. The original exception is attached as the cause.

Source

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

		}
		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 attributeConverterBean.getBeanInstance().convertToDatabaseColumn( domainForm );
		}
		catch (PersistenceException pe) {
			throw pe;
		}
		catch (RuntimeException re) {
			throw new PersistenceException( "Error attempting to apply AttributeConverter: " + re.getMessage(), re );
		}
	}

	@Override
	public JavaType<? extends AttributeConverter<O, R>> getConverterJavaType() {
		return converterJavaType;
	}

	@Override
	public JavaType<O> getDomainJavaType() {
		return domainJavaType;
	}

	@Override
	public JavaType<R> getRelationalJavaType() {
		return jdbcJavaType;
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Inspect the cause via getCause() to locate the failing line in convertToDatabaseColumn
  2. Guard the null case and cover every enum constant (add a default branch)
  3. Bind the parameter with the right API if the failure happens on a query parameter (setParameter with the converter class)
  4. Write a failing unit test that calls the converter directly with null and every domain constant

Example fix

// before
@Override
public String convertToDatabaseColumn(Status status) {
    return switch (status) {
        case ACTIVE -> "A";
        case CLOSED -> "C";
    }; // NPE on null, IllegalArgument on future constants
}
// after
@Override
public String convertToDatabaseColumn(Status status) {
    if (status == null) {
        return null;
    }
    return switch (status) {
        case ACTIVE -> "A";
        case CLOSED -> "C";
    };
}
Defensive patterns

Strategy: try-catch

Validate before calling

// validate in setters/service code before flush can reach the converter
void setStatus(Status s) {
    if (s != null && StatusConverter.codeOf(s) == null) throw new IllegalArgumentException("Unmapped: " + s);
    this.status = s;
}

Type guard

static boolean isConvertible(Status s) {
    return s == null || switch (s) { case ACTIVE, CLOSED -> true; default -> false; };
}

Try / catch

try {
    session.persist(entity);
    session.flush(); // force converter execution here for a precise failure point
} catch (PersistenceException e) {
    Throwable cause = e;
    while (cause.getCause() != null) cause = cause.getCause();
    log.error("Converter failed on attribute value {} of {}", entity.getStatus(), entity.getClass());
    // surface cause to user / abort transaction
}

Prevention

When it happens

Trigger: Persisting or updating an entity whose @Convert attribute's convertToDatabaseColumn(domainForm) throws - e.g. an NPE because the entity value is null, a switch without a default on a newly added enum constant, or formatting code that fails on the value.

Common situations: Null attribute not guarded in the converter (Hibernate does call the converter with null in several paths); a new enum constant added without extending the converter's mapping; a formatter (DateTimeFormatter, DecimalFormat) receiving an unexpected value; stricter Hibernate 6+ converter type resolution after an upgrade.

Related errors


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