Quick revision
The short answer
I would define a small set of shared value types, such as Money with an amount and a currency, Address and DateRange, as immutable @Embeddable classes or records that check their own rules in the constructor. Teams would agree on column names through a naming convention with @AttributeOverride where needed, use an AttributeConverter only for values that fit in one column, and share the types through a small library rather than shared entities, so every service still owns its schema.
What to remember
In this answer, we turned scattered money, address and date range modelling into shared, immutable value types that check their own rules, mapped them as embeddables with a prefix-plus-field column convention and fixed precisions, kept AttributeConverter for single-column values, and shared the types through a small library of values rather than entities, so each service keeps its own schema.
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.
I would define a small set of shared value types, such as Money with an amount and a currency, Address and DateRange, as immutable @Embeddable classes or records that check their own rules in the constructor. Teams would agree on column names through a naming convention with @AttributeOverride where needed, use an AttributeConverter only for values that fit in one column, and share the types through a small library rather than shared entities, so every service still owns its schema.
Why this question is asked
When every team models money its own way, the bugs are quiet and expensive: an amount stored as a double loses cents, a currency stored separately is forgotten in one table, and a date range with an end before its start passes every check. Interviewers ask this design question to see whether we can turn a recurring class of bugs into a standard that many teams will actually adopt. In this answer, we will look at what goes wrong today, design the shared value types, decide how they map to columns, choose between embeddables and converters, and plan how to share and roll them out.
1What Inconsistent Value Modelling Costs
Take three services. Orders stores price as a DECIMAL and a currency column. Billing stores amount_cents as a BIGINT and assumes the account currency. Reporting receives prices as strings like 'EUR 12.50' and parses them. Each choice is reasonable alone, but data crossing the services is converted by hand each time, and every conversion is a chance to mix up cents and units or to drop the currency.
Date ranges show the same pattern. One entity has start and end columns where end is exclusive, another has from_date and to_date where the end is inclusive, and a third stores a JSON object. Overlap checks then differ by one day between services, and nobody notices until a contract is billed twice.
2Designing the Shared Value Types
A value type should make the wrong state impossible to build. So the constructor checks the rules, there are no setters, and operations return new values. Here is Money as a record, which is accepted as an embeddable in Hibernate 6.2 and later and in Jakarta Persistence 3.2; on older stacks the same design works as a final-style class with a protected no-argument constructor.
Notice the choices. BigDecimal instead of double, because binary floating point cannot represent 0.10 exactly. The currency travels with the amount, so it cannot be forgotten. The scale check uses the currency's own decimals, two for EUR and zero for JPY. Hibernate maps java.util.Currency to a short text column by default, so no extra code is needed for it.
DateRange follows the same idea: a start and an exclusive end, a constructor that rejects an end before the start, and methods such as overlaps and contains written once and tested once. Address is simpler and mostly about consistent fields and lengths.
3Agreeing on Column Names
An embeddable used in many entities needs predictable column names, or every table ends up slightly different. The convention I would choose is prefix plus field: an attribute called price produces price_amount and price_currency. Teams can get it either from @AttributeOverride on each use, or from Hibernate's component path naming strategy, which builds names from the attribute path automatically.
| Type | Java shape | Columns | Rule checked in constructor |
|---|---|---|---|
| Money | BigDecimal amount, Currency currency | x_amount NUMERIC(19,4), x_currency VARCHAR(3) | non-null, scale fits currency |
DateRange | LocalDate start, LocalDate endExclusive | x_start DATE, x_end DATE | end after start |
| Address | street, city, postcode, country | x_street, x_city, x_postcode, x_country | country is ISO 3166 code |
EmailAddress | String value | one column via AttributeConverter | valid format, lower-cased |
The precision and length are part of the standard too. NUMERIC(19,4) covers large amounts and the currencies with up to four decimals in use, and ISO currency codes are three letters. Writing these numbers once in the guide prevents a team from picking NUMERIC(10,2) and finding out years later that it overflows.
4Embeddable or AttributeConverter?
An AttributeConverter turns one Java value into one database column and back. It is the right tool for a value that really is a single column, such as an EmailAddress wrapping a string or a CountryCode. It is the wrong tool for Money or DateRange, because squeezing two fields into one column, a string like 'EUR 12.50' or a JSON blob, makes the values impossible to sum, index or filter in SQL. So the rule is short: single-column values use a converter, multi-column values are embeddables.
Some teams ask about storing values as JSON columns, which Hibernate 6 supports well. For true document-like data that is fine, but for money and dates that reports must aggregate, real typed columns win.
5Sharing Without Coupling the Services
The value types go into a small library, versioned like any other dependency, containing only the value classes, their tests and perhaps the converters. It deliberately contains no entities. If we shared entities, every service would be tied to one schema and one release cycle, which is the opposite of what separate services are for. Sharing values gives us the same meaning of Money everywhere while each service still owns its tables and migrations.
For the rollout, new code uses the library from day one, and existing tables migrate when a team touches them for other reasons, with expand and contract steps for column renames. A simple architecture test, for example with ArchUnit, can flag entity fields named amount or price that are a raw BigDecimal or double instead of Money, which keeps the standard alive after the initial push.
- Service entities and schemasowned by each team: Order, Invoice, Contract
- Shared value libraryMoney,
DateRange, Address, converters, tests - JDK and Jakarta Persistence
BigDecimal, Currency,LocalDate,@Embeddable
3 layers, from the top down. Step through them one at a time.
6Follow-up questions
Why not store amounts as integer cents everywhere?
Integer minor units are a valid choice and avoid rounding issues, but they need the currency to know the scale, and some currencies have zero or three decimals. If a team prefers it, the Money type can still hide it: the column holds minor units and the type converts. What matters is that one representation is chosen and wrapped in one type, not that cents or decimals win.
Where do validation rules belong, in the type or in Bean Validation annotations?
Invariants that are always true for the value, such as end after start, belong in the constructor, so no invalid instance can exist anywhere. Context rules, such as a maximum order amount for a particular use case, belong in the service or in Bean Validation on request objects, because they differ by situation.
How do you keep JSON and events consistent with the database representation?
The library can also provide Jackson serializers for the value types, so Money always travels as an object with amount as a string and currency as a code. Using a string for the amount avoids JSON number precision loss in other languages.
7Conclusion
In this answer, we turned scattered money, address and date range modelling into shared, immutable value types that check their own rules, mapped them as embeddables with a prefix-plus-field column convention and fixed precisions, kept AttributeConverter for single-column values, and shared the types through a small library of values rather than entities, so each service keeps its own schema.
Open with
I would start by defining a few shared value types, Money, DateRange and Address, as immutable embeddables, as records where the stack is Hibernate 6.2 or later, otherwise as classes without setters.
Then, if they want more
Each one checks its invariants in the constructor: Money needs a BigDecimal amount and a Currency, and the amount's scale must fit the currency; DateRange needs the end after the start, with the end exclusive. Then no invalid value can exist anywhere in the code.
For mapping, I would agree on a naming convention, attribute name plus field, so price becomes price_amount and price_currency, applied with @AttributeOverride or Hibernate's component path naming strategy, and I would fix the precisions in the guide, for example NUMERIC(19,4) and a three-letter currency.
I would use AttributeConverter only for single-column values like an email address, never to pack money into a string or JSON, because then SQL cannot sum or filter it. The types go into a small versioned library that contains values, tests and maybe Jackson serializers, but no entities, so services share meaning without sharing schemas.
New code uses it immediately, old tables migrate when touched, and an ArchUnit rule flags raw BigDecimal amounts in entities so the standard does not fade.
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 "Teams model money, addresses and date ranges in different ways across entities (separate...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/c1dd32d8c6/