CSE Roadmap

Core tier · about 2 hours · 7 parts

Part 5 of 7 · 1 min read

Entity or value

Level 0 words, now in Java.

Level 0 module 2 split things into entities and value objects. In code, the split decides how a class is written:

Entity Value
Told apart by An id Its data
Changes over time Yes, usually No
equals By id, or not overridden By all fields
Good Java shape Class Record or final class
Example Booking, User Money, TimeSlot

A made-up check: a Booking with id B7 is cancelled. It is still B7, and any list that holds B7 should find it. A Money of 500 cents never "becomes" 600. Adding gives a new Money. Making values immutable is the next module's topic, and EJ gives rules for it.

Common mistake

Overriding equals on an entity using all its fields. When a field changes, the object is no longer equal to itself as stored in a set, and it seems to vanish.

Check yourself: 1. Is User an entity or a value? 2. Why do values suit records? 3. What can go wrong when an entity's equals uses a changing field?