Quick revision
The short answer
Data-oriented programming fits best for data that crosses boundaries: commands, events, API messages, results and parser output, modelled as sealed interfaces and records, with behaviour in functions that switch over them. It fits poorly for JPA entities, because records cannot be entities, and for hierarchies other teams must extend. Serializing sealed hierarchies needs explicit type information, such as Jackson's @JsonTypeInfo and @JsonSubTypes with an allow-list of names. The practical mix is object-oriented services that orchestrate and data-oriented types that carry the data.
What to remember
In this answer, we saw that data-oriented programming, with sealed interfaces and records and behaviour in functions with pattern switches, fits data crossing boundaries such as commands, events, API messages, results and parser output. It fits poorly for JPA entities, which records cannot be, and for hierarchies others must extend. Sealed hierarchies need explicit type information for serialization, such as @JsonTypeInfo with Id.NAME and an allow-list in @JsonSubTypes. In a Spring service, objects orchestrate and data-oriented types carry the 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.
Data-oriented programming fits best for data that crosses boundaries: commands, events, API messages, results and parser output, modelled as sealed interfaces and records, with behaviour in functions that switch over them. It fits poorly for JPA entities, because records cannot be entities, and for hierarchies other teams must extend. Serializing sealed hierarchies needs explicit type information, such as Jackson's @JsonTypeInfo and @JsonSubTypes with an allow-list of names. The practical mix is object-oriented services that orchestrate and data-oriented types that carry the data.
Why this question is asked
Java's records, sealed types and pattern matching have given us a second way to model a domain: instead of objects that hide state behind methods, plain immutable data with operations written as functions over it. Teams that adopt it enthusiastically sometimes try to use it everywhere, including places where Spring and JPA expect classic objects. Interviewers ask this design question to see whether we can draw the line sensibly. In this answer, we will recap what data-oriented programming means, look at where it fits in a Spring service, where it does not, how to serialize it safely, and how the two styles live together.
1A Quick Recap of Data-Oriented Programming
In classic object-oriented design, an object bundles state and behaviour, hides its fields, and callers ask it to do things. Polymorphism, overriding a method in each subclass, is how we vary behaviour by type.
Data-oriented programming separates the two. Data is modelled as immutable values: records for groups of fields and sealed interfaces for a closed set of alternatives. Behaviour lives in functions, often static methods or service methods, that take the data and use pattern switches to decide what to do for each alternative. The data is transparent, the set of shapes is closed, and the compiler checks that every function handles every shape. Neither style is better in general; they suit different problems.
2Where It Fits: Data Crossing Boundaries
The sweet spot is data whose whole purpose is to travel: an HTTP request body, a Kafka event, a command sent to a handler, the result of a call to another service, the output of a parser. Such data should be immutable, easy to compare in tests, and have a fixed set of shapes that consumers must all handle.
Notice how the Spring part is ordinary: BalanceProjector is a singleton bean with dependencies injected as usual. The events are just values it receives. Adding an event type makes apply fail to compile until it is handled, which is exactly what we want for an event stream. Controllers can accept and return records directly, and Spring's validation annotations work on record components.
3Where It Fits Poorly
JPA entities are the clearest case. An entity must have a no-argument constructor, must not be final, and has mutable state that Hibernate tracks and changes, including lazy-loading proxies created by subclassing. A record has none of these properties, so a record cannot be an entity. Records do work well elsewhere in persistence: as projections returned by queries, as DTOs, and, since Hibernate 6.2, as @Embeddable value types (check your Hibernate version). The entity itself stays a classic mutable class.
Open extension is the second case. A sealed interface lists all its implementations, so other teams or plugins cannot add their own. If a payment platform lets each country team contribute its own payment method, an ordinary interface with polymorphic methods is the right tool, and a sealed hierarchy would force every addition through one team.
The third case is rich behaviour with invariants that must be protected. An Order aggregate that must never ship before payment is often clearer as an object whose methods enforce the rule than as a record plus functions that each have to remember it.
| Kind of type | Data-oriented fit | Why |
|---|---|---|
| Commands, events, API messages | strong | immutable values with a closed set of shapes |
| Results of calls (Approved, Declined) | strong | callers must handle every outcome |
| Parser or AST output | strong | operations added often, node set closed |
| JPA entities | poor | records cannot be entities: final, no no-arg constructor |
| Plugin or extension points | poor | sealed types block other teams' implementations |
| Spring services and controllers | not applicable | they orchestrate; they are not data |
4Serializing Sealed Hierarchies Safely
When a sealed event is sent as JSON, the receiver must know which record to create. JSON has no types of its own, so we must include a type name. With Jackson, which Spring Boot uses by default, we annotate the interface.
Two choices here matter for safety. We use Id.NAME with explicit short names, not Id.CLASS, which would put the Java class name in the JSON and let an attacker ask Jackson to create arbitrary classes, a known deserialization vulnerability. And the @JsonSubTypes list is an allow-list: only these three names can be deserialized. The names are also a contract, so renaming a record does not have to break consumers. Whether newer Jackson versions can discover permitted subtypes automatically is version-dependent, so check your Jackson version; listing them explicitly is clear either way. Spring Boot 4 moves to Jackson 3, where the annotations keep the same names.
5The Practical Mix
In a real Spring service the two styles divide the work cleanly. Services, controllers, repositories and configuration are objects: they have identity, dependencies, lifecycle and often state such as connection pools. They orchestrate. The data that flows between them, requests, commands, events, results, is data-oriented: records and sealed interfaces, immutable, validated at construction, and handled with exhaustive switches. Entities remain classic mutable objects managed by JPA, and we map them to records at the edge of the persistence layer.
- Controllerobject: accepts and returns records
- Application serviceobject: orchestrates, switches over sealed results
- Commands, events, resultsdata-oriented: sealed interfaces and records
- Repository and JPA entitiesobject: mutable entities, mapped to records at the edge
4 layers, from the top down. Step through them one at a time.
6Follow-up questions
Can a record be used as a Spring @ConfigurationProperties class?
Yes. Spring Boot binds configuration to records through their constructor, which makes configuration immutable. This is a good example of data-oriented style fitting naturally into Spring.
Does data-oriented programming mean giving up encapsulation?
Partly, by design. Records expose their components through accessors, so the data is transparent. We protect invariants in the compact constructor instead of hiding fields, and since records are immutable there is no state to protect after construction.
How do we version a sealed event hierarchy shared through Kafka?
Treat each record's components and type name as a contract. Adding optional components is usually safe with Jackson; removing or renaming is breaking. Consumers should convert incoming JSON into their own sealed model and handle unknown type names explicitly at that boundary.
7Conclusion
In this answer, we saw that data-oriented programming, with sealed interfaces and records and behaviour in functions with pattern switches, fits data crossing boundaries such as commands, events, API messages, results and parser output. It fits poorly for JPA entities, which records cannot be, and for hierarchies others must extend. Sealed hierarchies need explicit type information for serialization, such as @JsonTypeInfo with Id.NAME and an allow-list in @JsonSubTypes. In a Spring service, objects orchestrate and data-oriented types carry the data.
Open with
I would use data-oriented programming for data that crosses boundaries: commands, events, API request and response bodies, results of calls to other services, and parser output. Those are immutable values with a fixed set of shapes, so a sealed interface with records fits, and the behaviour lives in service methods that use exhaustive pattern switches, which means the compiler finds every place to update when a new shape is added.
Then, if they want more
Spring is happy with this: controllers bind records, configuration properties can be records, and beans just receive the values. Where it fits poorly is JPA entities, since an entity needs a no-arg constructor, must not be final and is mutable, so a record cannot be an entity, although records work as projections, DTOs and embeddables in recent Hibernate.
It also fits poorly where other teams need to add implementations, because sealed types are closed, and for aggregates with rich invariants enforced by methods. For serialization, a sealed hierarchy needs type information in the JSON, so with Jackson I would use @JsonTypeInfo with Id.NAME and list the subtypes in @JsonSubTypes, which acts as an allow-list, never Id.CLASS.
So the mix I recommend is object-oriented services that orchestrate, and data-oriented types that carry the data between them.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6, Spring Boot 4
- 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
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Your organization is adopting data-oriented programming with sealed interfaces and...": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/f628105c63/