Quick revision
The short answer
We implement jakarta.persistence.AttributeConverter<X, Y>, whose convertToDatabaseColumn turns our Java type into a column value and convertToEntityAttribute turns it back, and apply it with @Convert(converter = ...) on the attribute or @Converter(autoApply = true) for every attribute of that type. If the value needs several columns, such as an amount and a currency, an @Embeddable is the right tool instead. Converted values take part in dirty checking through equals, so they should be immutable, and for JSON columns Hibernate has its own mapping, depending on version.
What to remember
In this answer, we mapped custom value types to single columns with jakarta.persistence.AttributeConverter, implementing convertToDatabaseColumn and convertToEntityAttribute, and applied converters with @Convert on an attribute or @Converter(autoApply = true) for a whole type. We saw that values spanning several columns belong in an @Embeddable, that converted values take part in dirty checking through equals and are safest as immutable types replaced as a whole, and that Hibernate 6 offers built-in JSON mapping as an alternative for structured data.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We implement jakarta.persistence.AttributeConverter<X, Y>, whose convertToDatabaseColumn turns our Java type into a column value and convertToEntityAttribute turns it back, and apply it with @Convert(converter = ...) on the attribute or @Converter(autoApply = true) for every attribute of that type. If the value needs several columns, such as an amount and a currency, an @Embeddable is the right tool instead. Converted values take part in dirty checking through equals, so they should be immutable, and for JSON columns Hibernate has its own mapping, depending on version.
Why this question is asked
Real domain models are full of small value types: money, email addresses, tags, percentages. Storing them as raw strings and numbers in entities spreads conversion code across the application, while mapping each to its own table is far too heavy. Interviewers ask this question to see whether we know JPA's tool for exactly this gap, and its pitfalls. In this answer, we will recap what JPA can map on its own, write converters for a list of tags and for Money, look at how to apply them, see when an @Embeddable is better, and finish with the dirty checking trap and JSON mapping.
1A Quick Recap: What JPA Maps on Its Own
JPA knows how to store basic types such as String, numbers, BigDecimal, booleans, enums and java.time types in a column. For any other class used as an attribute, it does not know what to do. Our two examples are a List<String> of tags that we want in a single text column as "java,spring,jpa", and a Money class that we want in one column.
An attribute converter is a small class that sits between the entity attribute and the column. On write, it turns the Java value into a basic type the database understands; on read, it turns the column value back into the Java type.
2Writing a Converter for Tags
The converter implements AttributeConverter<X, Y>, where X is the entity attribute type and Y is the column type.
Notice that the converter must handle null in both directions, because a column can be NULL and an attribute can be unset. The format also needs a rule for tags that contain a comma; here we would reject them in the domain, or choose a different separator. A comma-separated column cannot be searched or indexed well, so if we often query by tag, a separate table is still the better model.
3A Converter for Money and autoApply
Money is a value type we use in many entities. Repeating @Convert on every attribute is noisy, so we mark the converter with autoApply, and JPA applies it to every attribute of type Money automatically.
A converter can also be used in JPQL comparisons: when we write WHERE i.total = :total and bind a Money, the provider converts the parameter with the same converter. Converters do not apply to @Id attributes or to version attributes; check the specification and your provider if you need that.
4When a Converter Is the Wrong Tool: @Embeddable
A converter maps one attribute to exactly one column. But real money usually has two parts, an amount and a currency, and squashing them into a string such as "19.99 EUR" makes them impossible to sum or filter in SQL. For values that need several columns, JPA has @Embeddable: a class whose fields become columns of the owning entity's table.
| Need | Tool | Example |
|---|---|---|
| One Java value to one column | AttributeConverter | Money as NUMERIC, tags as text |
| Same converter everywhere | @Converter(autoApply = true) | Every Money attribute |
| One value to several columns | @Embeddable | amount and currency |
| Structured document in one column | Hibernate JSON mapping | preferences as jsonb |
So the rule is simple: one column means a converter, several columns mean an embeddable.
5The Dirty Checking Trap
When a transaction flushes, Hibernate compares each managed entity's current attribute values with a snapshot taken when it was loaded, and writes an UPDATE for anything that changed. For converted attributes, that comparison relies on the Java type's equals, and the snapshot must be a separate copy of the value.
This is where mutable value objects cause trouble. If tags were a mutable ArrayList and we called article.getTags().add("new"), the change might not be detected reliably, depending on how the snapshot was taken; and a value class without a proper equals may be seen as changed on every flush, causing needless UPDATEs. The safe habit is to make value types immutable, use records or List.of(), and change them by replacing the whole value.
Hibernate also offers controls for how converted values are treated, such as marking them immutable; the details differ between Hibernate 6 versions, so check your version if you need to tune it.
6JSON Columns in Hibernate
For structured data that does not fit a single scalar column, Hibernate 6 can map an attribute to a JSON column directly, with @JdbcTypeCode(SqlTypes.JSON), serialising it with Jackson or another available JSON library. On PostgreSQL this maps to json or jsonb. This is often a better choice than a hand-written converter that calls a JSON library, because Hibernate knows the column type. The exact support depends on the database and the Hibernate version, so check your version.
7Follow-up questions
Can a converter be applied to an enum?
Yes, and it is a good way to store a stable code instead of the enum name or ordinal, for example storing 'A' for ACTIVE. The converter maps the enum to its code and back, so renaming or reordering constants never changes stored data. Do not combine it with @Enumerated on the same attribute.
Where must the converter class live to be found?
In Spring Boot, converters annotated with @Converter are found by entity scanning in the same packages as the entities, or they can be listed explicitly. If autoApply seems not to work, the usual cause is that the converter is outside the scanned packages.
Can a converter call a Spring bean?
Hibernate can let Spring create converters as beans when Spring is the bean container, which Spring Boot configures by default, so constructor injection works. It is still best to keep converters simple and free of database calls, since they run for every load and flush.
8Conclusion
In this answer, we mapped custom value types to single columns with jakarta.persistence.AttributeConverter, implementing convertToDatabaseColumn and convertToEntityAttribute, and applied converters with @Convert on an attribute or @Converter(autoApply = true) for a whole type. We saw that values spanning several columns belong in an @Embeddable, that converted values take part in dirty checking through equals and are safest as immutable types replaced as a whole, and that Hibernate 6 offers built-in JSON mapping as an alternative for structured data.
Open with
For a value that fits in one column, I write a class that implements jakarta.persistence.AttributeConverter with two type parameters, the Java type and the column type. convertToDatabaseColumn turns a list of tags into a comma-separated string, and convertToEntityAttribute splits it back; both handle null.
Then, if they want more
I apply it with @Convert converter equals TagsConverter class on the attribute, or, for a type like Money used across many entities, I annotate the converter with @Converter autoApply true so every Money attribute is converted automatically, and JPQL parameters of that type are converted too.
If the value really needs several columns, like amount and currency, a converter is the wrong tool and I use an @Embeddable, which maps each field to its own column in the owning table.
One thing I watch is dirty checking: Hibernate compares the current value with a snapshot using equals, so value types should be immutable, records or List.of, and changed by replacing the whole value. A mutable list modified in place, or a class without equals, gives missed or needless updates.
For structured data, Hibernate 6 can also map an attribute straight to a JSON column with @JdbcTypeCode SqlTypes JSON, depending on version.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How do you map a custom value type, such as a Money class or a list of tags stored as a...": https://scrivio-interview-library.netlify.app/databases/jpa-fundamentals/c6ccc93d9e/