Hibernate: @Convert maps a type the database does not have

An AttributeConverter turns any Java type into a column and back.

Code
@Converter(autoApply = true)
class MoneyConverter implements AttributeConverter<Money, BigDecimal> {
    public BigDecimal convertToDatabaseColumn(Money m) { return m == null ? null : m.amount(); }
    public Money convertToEntityAttribute(BigDecimal d) { return d == null ? null : new Money(d); }
}

@Entity
class Order {
    Money total;      // stored as decimal(19,2)
}
Output
autoApply = true applies it to every Money field in the model,
so no entity needs to mention the converter at all.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11