Skip to content
Preptima
mediumConceptScenarioMidSenior

A teammate turns a JPA entity into a Java record because records are immutable. Where is that claim false, and where is a record the wrong type?

Java records have shallow immutability: fields are final, but a List or array component can still change through its reference unless the compact constructor copies it. A record cannot be a JPA entity, because the class and fields are final, it lacks a no-arg constructor, and providers subclass and populate entities.

5 min readUpdated 2026-10-09Target archetype: Enterprise Captive, Product Startup

Answer

Short answer

Java records are immutable one level deep. Every component is a private final field with no setter, but if the component is a List, an array or a Date, anyone holding the reference can still change what it points to. That is shallow immutability, and you fix it with defensive copies in the compact constructor. Separately, a record cannot be a JPA entity: the specification requires a non-final class with non-final persistent fields and a no-arg constructor, and a record fails all three.

Was this answer helpful?

Likely follow-ups

  • Your record holds a List of mutable LineItem objects and you copy the list. What can a caller still change?
  • How would you expose an array component from a record without leaking the internal array?
  • Why can reflection change a final field in an ordinary class but not in a record?
  • Which parts of an entity would you model as embeddable records, and what changes when one of them needs to be updated?

Related questions

Further reading

Related Topics