The @Converter annotation (jakarta.persistence.Converter) marks a class that implements AttributeConverter<X, Y>, which tells JPA how to convert an entity attribute of Java type X into a database column value of type Y and back. Use it when the default mapping does not fit, for example to store a value object as a string, a Set as comma-separated text, or a boolean as Y and N.
Since JPA 3.0 the package is jakarta.persistence; older code uses javax.persistence with the same semantics. The examples below work with Hibernate 6 and Spring Boot 3 or later.
AttributeConverter and @Converter work togetherAttributeConverter and @Converter work togetherAttributeConverter<X, Y> declares two methods. convertToDatabaseColumn receives the attribute value and returns what is written to the column. convertToEntityAttribute receives the column value and returns the attribute value. @Converter registers the class with the persistence provider, so it is found during entity scanning.
Both methods can receive null, so handle it explicitly. The converter should be stateless and thread-safe, because the provider may share one instance across sessions.
There are two ways to connect a converter to an attribute.
@ConvertAnnotate the field with @Convert and name the converter class. Only that attribute is affected.
autoApplySet autoApply = true and the converter is used for every attribute of type X in the persistence unit, with no @Convert needed.
Here EmailAddress could be a Java record. Every entity field of that type is stored as a plain string. To switch off an auto-applied converter for one field, use @Convert(disableConversion = true).
Converters are often used for small collections or JSON-like values that you do not want to model as a separate table.
This only suits values that never contain the separator and that you do not need to query by individual element. For complex structures, a JSON column with Hibernate's JSON support, or a proper child table, is a better fit.
@Id, @Version, relationship attributes (@OneToMany, @ManyToOne and so on) or attributes annotated with @Enumerated or @Temporal.where c.newsletterSubscribed = true works as expected.Y.@Converter and @Convert?@Converter is placed on the converter class to declare it, optionally with autoApply. @Convert is placed on an entity attribute to use a specific converter.
@Converter with Spring Boot?Yes. Spring Boot scans entities and converters from your application packages, but the class still needs @Converter so the provider recognises it.
AttributeConverter the same as Spring's Converter interface?No. AttributeConverter belongs to JPA and maps entity attributes to columns. Spring's Converter<S, T> converts values in the web and core layers, for example request parameters.
@Enumerated field?No. Remove @Enumerated and use a converter instead if you want a custom stored value.
@Converter declares a JPA AttributeConverter that translates between a Java attribute type and a column type. Apply it with @Convert for one field or autoApply = true for all fields of a type, handle null in both methods, and avoid it on identifiers, relationships and enum-annotated fields.