Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

What is the difference between an entity and a value object in domain-driven design? Give Java examples and explain how equals and hashCode differ for each.

An entity is an object with an identity that stays the same while its attributes change, such as an Order with order number 1001, so two entities are equal when they have the same identity. A value object has no identity of its own and is defined only by its attributes, such as Money or Address; it should be immutable, and two value objects are equal when all their attributes are equal. In Java, entities implement equals and hashCode on their id, value objects on all fields, which records give us for free, and in JPA value objects usually map as @Embeddable types.

Why this question is asked

The difference between entities and value objects sounds academic until we see what goes wrong without it: objects that cannot be compared reliably, shared mutable addresses that change on two customers at once, and HashSets that lose their elements. Interviewers ask this question because it is the foundation of domain-driven design, a way of modelling software around the business and its language, and because the equals and hashCode part shows whether we can turn the idea into correct Java. In this answer, we will recap the two building blocks, write an entity and a value object in Java, compare how equality works for each, and see how they map with JPA.

1A Quick Recap of the Two Building Blocks

In domain-driven design, or DDD, we model the business with a small set of building blocks. Two of the most basic are entities and value objects, and the question that separates them is simple: do we care which one it is, or only what it is?

For an order, we care which one it is. Order 1001 is the same order whether it holds two lines or five, and whether its status is NEW or SHIPPED. Its attributes change during its life, but it keeps its identity, and other parts of the system refer to it by that identity. That makes it an entity.

For an amount of money, we only care what it is. A 20.00 EUR note in one wallet is interchangeable with another 20.00 EUR. There is no meaningful question of which twenty euros we hold. So Money is a value object: it is fully described by its amount and currency. An address works the same way: two addresses with the same street, city and postcode are the same address.

Entity or value object?
EntityValue object
Defined byIdentity (an id or business key)All of its attributes
Changes over timeYes, attributes change, identity staysNo, replaced by a new value
equals and hashCodeBased on identity onlyBased on all attributes
Java formClass with an id fieldRecord or final immutable class
JPA mapping@Entity with @Id@Embeddable inside an entity
ExamplesOrder, Customer, AccountMoney, Address, DateRange

2An Entity in Java

An entity keeps its identity fixed and lets its other attributes change through methods that make sense in the business. Its equality looks only at the identity.

Java
public class Order {
private final OrderNumber number; // identity, set once
private OrderStatus status = OrderStatus.NEW;
private final List<OrderLine> lines = new ArrayList<>();
public Order(OrderNumber number) { this.number = Objects.requireNonNull(number); }
public void ship() { status = OrderStatus.SHIPPED; } // state changes, identity does not
@Override public boolean equals(Object o) {
return o instanceof Order other && number.equals(other.number);
}
@Override public int hashCode() { return number.hashCode(); }
}
Order a = new Order(new OrderNumber("1001"));
Order b = new Order(new OrderNumber("1001"));
b.ship();
System.out.println(a.equals(b)); // true: same order, different state

Notice that a and b are equal even though their status differs. That is correct for an entity: they represent the same order seen at different moments. If equals compared every field, the same order would stop being equal to itself as soon as it shipped, which would break any collection or cache that holds it.

3A Value Object in Java

A value object should be immutable, meaning its fields never change after construction. To change it, we create a new value. Java records fit this well, because a record is final, its fields are final, and it generates equals, hashCode and toString from all its components.

Java
public record Address(String street, String city, String postcode) {
public Address { // compact constructor validates
Objects.requireNonNull(street); Objects.requireNonNull(city);
postcode = postcode.strip().toUpperCase(); // normalise so equal addresses compare equal
}
public Address withStreet(String newStreet) { return new Address(newStreet, city, postcode); }
}
Address home = new Address("1 Main St", "Leeds", "ls1 4ap");
Address same = new Address("1 Main St", "Leeds", "LS1 4AP");
System.out.println(home.equals(same)); // true: all attributes equal
Address moved = home.withStreet("2 High St");
System.out.println(home.street()); // 1 Main St: the original never changes

Immutability matters because value objects are shared freely. If two customers hold the same Address instance and one of them could change its street, the other customer would move house too. With an immutable value, moving means assigning a new Address, and nobody else is affected. Notice also that we normalise the postcode in the constructor; a value object is a good place to put such rules, so equality behaves the way the business expects.

4How equals and hashCode Differ

The contract of equals and hashCode is the same for both: equal objects must have equal hash codes, and the hash code of an object must not change while it sits in a hash-based collection. What differs is which fields we use.

For an entity, we use only the identity. The reason is that other fields change during the object's life, and we want the same order to stay equal to itself and keep the same hash code throughout. For a value object, we use all attributes, because the attributes are the value. Since value objects are immutable, their hash code can never change, so they are always safe as map keys and set members.

There is one trap with entities whose id is generated by the database: before saving, the id is null, so equality and hashing on the id behave differently before and after persisting. A natural key set at construction, like the OrderNumber above, or an application-assigned UUID avoids this.

5Mapping Them With JPA

JPA maps entities with @Entity and an @Id. Value objects normally become @Embeddable types, whose fields are stored as columns in the owning entity's table, so they have no table and no id of their own.

Java
@Embeddable
public record Address(String street, String city, String postcode) { } // records as embeddables: Hibernate 6.2+
@Entity
public class Customer {
@Id private UUID id;
@Embedded private Address billingAddress; // stored as columns street, city, postcode
@AttributeOverrides({
@AttributeOverride(name = "street", column = @Column(name = "ship_street")),
@AttributeOverride(name = "city", column = @Column(name = "ship_city")),
@AttributeOverride(name = "postcode", column = @Column(name = "ship_postcode")) })
@Embedded private Address shippingAddress; // same type, different columns
}

Hibernate 6.2 and later can use records as embeddables, which keeps the value object immutable; with older versions or other providers we write a small final class with a protected no-argument constructor. A collection of value objects can be mapped with @ElementCollection, which stores them in a separate table but still treats them as values owned by the entity.

6Follow-up questions

Can a value object contain an entity?

It should not hold a reference to an entity, because that would give the value a changing, identity-based part, and its equality would become unclear. It can hold the entity's identifier, such as a CustomerId, which is itself a small value object.

Is every object without a database id a value object?

No. The question is about meaning, not storage. A Customer is an entity even before it is saved, because the business tracks it over time. And something stored in its own table, such as a list of price tiers, can still be a set of value objects if we only care about what each tier is.

Why not use Lombok's @Data on entities?

Because @Data generates equals and hashCode from all fields, which is the value-object rule. On an entity, that makes equality change as the state changes and can trigger lazy loading of associations inside equals. Entities need hand-written, identity-based equality.

7Conclusion

In this answer, we saw that an entity has an identity that stays the same while its attributes change, like an Order with its order number, so it compares equal by identity, while a value object such as Money or Address is defined only by its attributes, should be immutable, and compares equal when all attributes are equal. Java records fit value objects well because they generate equals and hashCode from every component, and in JPA value objects usually map as @Embeddable types inside the entity that owns them.

What to say in the interviewAbout 90 seconds aloud

Open with

An entity is something whose identity matters more than its current attributes. An Order with number 1001 stays the same order when its lines or status change, and other parts of the system refer to it by that number.

Then, if they want more

So in Java I implement equals and hashCode on the identity only, for example the order number, because the other fields change during its life and I want the order to stay equal to itself.

A value object is the opposite: it has no identity and is defined entirely by its attributes, like Money with an amount and a currency, or an Address. Two addresses with the same street, city and postcode are simply the same address.

Value objects should be immutable, so to change one we create a new instance, which also makes them safe to share. Their equals and hashCode use all attributes, and because they never change, their hash code is stable.

Java records are a natural fit, since they are final and generate equals, hashCode and toString from all components, and the compact constructor is a good place for validation and normalisation. In JPA, entities get @Entity and an @Id, and value objects usually map as @Embeddable types, stored as columns of the owning entity's table.

One thing I watch is entities with database-generated ids, because the id is null before saving, so a natural key or a UUID is safer for equality.

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 official documentation for the version you use. 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 "What is the difference between an entity and a value object in domain-driven design?...": https://scrivio-interview-library.netlify.app/java/domain-driven-design/ae7fb4d84e/