SteelReed

Search

Search posts, questions…

Java CoreQuestion 20 / 43

Junior

What is a record, how does it differ from a normal class, and can it be a JPA entity?

Short answer

A record (since Java 16) is a class only for carrying data that doesn't change: declare the fields once and the compiler generates the rest.

use asnot asrecord Money(amount,currency)private final fieldscanonical constructoraccessor amount()equals, hashCode, toStringDTO, projection, valueobjectJPA entity

3 points to say:

  1. It generates the constructor, getters, equals, hashCode, toString.
  2. Great as a DTO, not as a JPA entity.
  3. Only shallowly immutable: a list inside can still change.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.