Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Map a Customer with a home Address and a billing Address, both using the same @Embeddable class, stored as columns of the customer table.

We write Address once as an @Embeddable class with street, city and postcode, and embed it twice in Customer. The first embedding can use the default column names, and the second must rename every column with @AttributeOverride, for example billing_street, because two attributes cannot map to the same column. Embeddables have no identity of their own, and when all their columns are null Hibernate gives us a null Address.

Why this question is asked

Addresses, money amounts and date ranges appear in almost every domain model, and they are the textbook case for embeddables. Interviewers ask this exercise because it checks that we know how a value type is stored, what happens when the same type appears twice in one entity, and a couple of behaviours that surprise people in production. In this answer, we will recap what an embeddable is, write the Address class, map it twice in Customer, see the table it produces, and then look at null handling and records.

1A Quick Recap of Embeddables

An embeddable is a class whose fields are stored as columns of the entity that contains it. It is not an entity: it has no id, no table of its own, and no life apart from its owner. If we delete the customer, its addresses are gone with it, and two customers with the same street are still two separate values, not one shared row.

This is what people mean by a value object, an object defined only by its values. Two addresses with the same street, city and postcode are interchangeable. Contrast that with an entity such as Customer, where two customers with the same name are still different customers because they have different ids.

2Writing the Address Class

The embeddable is a plain class marked with @Embeddable. JPA needs a no-argument constructor, which can be protected so that our own code uses the full constructor.

Java
@Embeddable
public class Address {
private String street;
private String city;
private String postcode;
protected Address() { } // for JPA only
public Address(String street, String city, String postcode) {
this.street = street;
this.city = city;
this.postcode = postcode;
}
public String getStreet() { return street; }
public String getCity() { return city; }
public String getPostcode() { return postcode; }
// equals and hashCode over all three fields, since this is a value
}

Notice that there are no setters. A value object is easier to reason about when it is immutable: to change the billing address we assign a new Address. Hibernate notices the new value at flush time and updates the three columns, so we lose nothing by giving up setters.

3Mapping Two Addresses in Customer

Now we embed the class twice. If we did nothing else, both attributes would try to use the columns street, city and postcode, and Hibernate would refuse to start with an error about a repeated or duplicated column in the mapping (the exact wording depends on your version). So we keep the defaults for home and override every column for billing.

Java
@Entity
@Table(name = "customer")
public class Customer {
@Id @GeneratedValue
private Long id;
private String name;
@Embedded
private Address home; // columns: street, city, postcode
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "street", column = @Column(name = "billing_street")),
@AttributeOverride(name = "city", column = @Column(name = "billing_city")),
@AttributeOverride(name = "postcode", column = @Column(name = "billing_postcode"))
})
private Address billing; // columns: billing_street, billing_city, billing_postcode
}
The customer table this mapping produces
ColumnComes fromExample value
idCustomer.id42
nameCustomer.nameAsha Rao
streethome.street12 Hill Road
cityhome.cityPune
postcodehome.postcode411001
billing_streetbilling.street (overridden)5 Market Lane
billing_citybilling.city (overridden)Mumbai
billing_postcodebilling.postcode (overridden)400001
Both addresses live in the same row; there is no address table and no join.

The name in each @AttributeOverride is the field name inside Address, and the column is the name we want in the customer table. Since JPA 2.2, @AttributeOverride is repeatable, so we may also write the three annotations without the @AttributeOverrides wrapper. For symmetry, many teams override the home columns too, as home_street and so on, so nobody wonders which address the plain street column belongs to.

4Using It and Seeing the SQL

Saving and reading a customer needs no extra code. Hibernate writes all eight columns in one insert and reads them back in one select.

Java
Customer c = new Customer("Asha Rao",
new Address("12 Hill Road", "Pune", "411001"),
null); // no billing address yet
em.persist(c);
// insert into customer (name, street, city, postcode,
// billing_street, billing_city, billing_postcode, id) values (?, ?, ?, ?, null, null, null, ?)
Customer loaded = em.find(Customer.class, c.getId());
System.out.println(loaded.getHome().getCity()); // Pune
System.out.println(loaded.getBilling()); // null: all three billing columns are null

The last line shows the null rule. When every column of an embedded value is null, Hibernate does not build an Address with three null fields; it sets the attribute to null. That is usually what we want, but it means code must null-check getBilling(), and an Address created with all-null fields will come back as null after a round trip. Hibernate once had a setting to create empty composites instead, which has been deprecated; check your version before relying on it.

5Records as Embeddables

Since an embeddable is a value, a Java record is a natural fit. Hibernate 6.2 and later can map a record as an embeddable, instantiating it through its canonical constructor, and Jakarta Persistence 3.2 made records officially allowed as embeddable classes. With an older stack, use the class shown above; check your version.

Java
@Embeddable
public record Address(String street, String city, String postcode) { } // equals, hashCode, immutability for free

The overrides in Customer stay exactly the same, because they refer to the component names street, city and postcode, which a record has as well.

6Follow-up questions

Can an embeddable contain another embeddable or a relationship?

Yes. An embeddable may nest another embeddable, for example Address containing a GeoPoint, and the overrides then use a dotted name such as geo.latitude. It may also hold a @ManyToOne, such as a reference to a Country entity, in which case the join column is overridden with @AssociationOverride rather than @AttributeOverride.

Is there a way to avoid writing all the overrides?

Hibernate's ImplicitNamingStrategyComponentPathImpl builds column names from the full path, so home.street becomes home_street and billing.street becomes billing_street without any override. In Spring Boot it is set with spring.jpa.hibernate.naming.implicit-strategy. It changes names for every embeddable in the application, so it is a choice to make at the start of a project, not halfway through.

Why should Address implement equals and hashCode?

Because it is a value. Hibernate compares embedded values when checking for changes and when they are used in collections such as an @ElementCollection Set. Equality over all fields, which a record gives automatically, makes two identical addresses equal, as a value should be.

7Conclusion

In this answer, we wrote Address as an @Embeddable value without identity, embedded it twice in Customer and renamed the billing columns with @AttributeOverride so the two attributes do not fight over the same columns. We saw that both addresses are stored in the customer row, that an all-null address comes back as null, and that recent Jakarta Persistence and Hibernate versions accept a record as the embeddable.

What to say in the interviewAbout 80 seconds aloud

Open with

I would write Address as an @Embeddable class with street, city and postcode, a protected no-argument constructor for JPA, a full constructor, no setters, and equals and hashCode over all fields, or simply as a record if the stack is Hibernate 6.2 or later, since Jakarta Persistence 3.2 allows records as embeddables.

Then, if they want more

In Customer I would put @Embedded Address home and @Embedded Address billing. Because both would otherwise map to the same street, city and postcode columns and Hibernate rejects repeated columns, I would add @AttributeOverride for each field of billing, mapping street to billing_street and so on, and often I would override home too so the column names are symmetric.

The result is one customer table with both addresses as columns and no join. I would point out that embeddables have no identity: they live and die with the customer, and to change an address we assign a new value.

And I would mention the null rule: if all three billing columns are null, Hibernate returns a null Address rather than an empty one, so callers have to handle null. If a project embeds many values, the component path naming strategy can generate prefixed names instead of writing overrides.

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.
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 "Map a Customer with a home Address and a billing Address, both using the same...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/8c25acd3c0/