The @Convert annotation in JPA applies an AttributeConverter to one entity attribute, so the persistence provider converts the value between its Java type and the database column type. Use it when a field has no natural column mapping, such as a List<String>, an enum stored as a code, or a value object.
The converter itself is a class that implements AttributeConverter<X, Y>. @Convert selects which converter handles a given field, while @Converter declares the class as a converter.
@Converter marks a class as a JPA converter. @Convert attaches a converter to a specific attribute, or disables one with disableConversion = true. If a converter is declared with @Converter(autoApply = true), it applies to every attribute of the matching type and you do not need @Convert at all.
Both annotations live in the jakarta.persistence package in Jakarta Persistence 3.x (Spring Boot 3, Hibernate 6). Older projects on JPA 2.x use javax.persistence with the same API.
The converter below stores a List<String> as a comma-separated column.
Both methods must handle null. This simple format breaks if an element contains a comma, so use JSON or a dedicated child table when values are free text.
Hibernate calls convertToDatabaseColumn on insert and update, and convertToEntityAttribute when it loads the row.
When the same type is used in many entities, declare the converter once and let the provider apply it everywhere.
Any Year attribute in any entity now maps to an integer column. An individual field can opt out with @Convert(disableConversion = true).
| Use | Example |
|---|---|
| Field or getter | @Convert(converter = X.class) on the attribute |
| Collection element | @Convert(attributeName = "...") through @Converts on the entity or mapped superclass |
| Embedded attribute | @Convert(attributeName = "address.zip", converter = X.class) on the entity |
| Map key or value | attributeName = "key" or "value" |
Some attributes cannot use converters: identifiers (@Id), version fields (@Version), and relationship attributes. Also remember that queries compare the database value, so a converted column used in a JPQL condition behaves according to the converter. For enums, you can often use @Enumerated instead, but a converter is the better choice when you need stable custom codes that do not change with enum order or name.
@Converter marks the converter class. @Convert selects it for a particular attribute. With autoApply = true you do not need @Convert.
Yes. Write an AttributeConverter<Status, String> that maps each constant to its code, then apply it with @Convert or autoApply.
Yes. Spring Data uses the underlying provider, so converters on entities apply to all repository operations.
You can, using a converter that serialises to a JSON string. Hibernate 6 also has native JSON mapping with @JdbcTypeCode(SqlTypes.JSON), which is often simpler.
@Convert binds an AttributeConverter to an entity attribute so custom Java types can be persisted in ordinary columns. Declare the converter with @Converter, handle null values, use autoApply for types reused across entities, and prefer native mappings when the provider already supports them.