Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

A team wants to use records for request and response DTOs in Spring MVC with Jackson, and also as JPA entities. What works and what does not?

Records work very well as Spring MVC request and response DTOs: Jackson 2.12 and later binds them through the canonical constructor, Spring Boot's managed Jackson versions support this, and Bean Validation annotations on components are checked with @Valid. Records cannot be JPA entities, because entities need a no-argument constructor, changeable state and non-final classes for proxies. They can be embeddables in recent Hibernate versions and are excellent query projections, so the usual design maps between entity and record at the service boundary.

Why this question is asked

Once a team discovers records, the natural next thought is to use them everywhere, including for the database model. Half of that idea is excellent and the other half fails at startup. Interviewers ask this scenario to see whether we know the requirements each framework places on our classes and can design around them. In this answer, we will build a record DTO with validation, see why a record entity fails, look at the places records do fit in persistence, and finish with the mapping between the two at the service boundary.

1Records as Request and Response DTOs

A DTO, a data transfer object, is a type whose only job is to carry data across a boundary, here between JSON and our controller. That is precisely what records are for. Since Jackson 2.12, a record is deserialized by calling its canonical constructor with the JSON values matched by component name, and serialized by calling its accessors. Every Spring Boot 3.x release manages a Jackson version far past 2.12, and Spring Boot 4 uses Jackson 3, which supports records as well.

Java
public record CreateOrderRequest(
@NotBlank String customerId,
@NotEmpty List<@Valid LineRequest> lines) {}
public record LineRequest(@NotBlank String sku, @Positive int quantity) {}
public record OrderResponse(String id, BigDecimal total, String status) {}
@PostMapping("/orders")
ResponseEntity<OrderResponse> create(@Valid @RequestBody CreateOrderRequest req) {
return ResponseEntity.status(201).body(orders.create(req));
}
// POST {"customerId":"","lines":[]} -> 400, two field errors: customerId, lines

Bean Validation works because an annotation on a record component is copied by the compiler to the private field, and Hibernate Validator validates fields. @Valid on the parameter makes Spring validate the body, and a failure becomes a MethodArgumentNotValidException that Spring turns into a 400 response. Notice also that the request record is immutable, so nothing between the controller and the service can quietly change what the client sent.

2Why a Record Cannot Be a JPA Entity

Now the other half. Suppose we annotate a record with @Entity.

Java
@Entity
public record Order(@Id Long id, String customerId, BigDecimal total) {}
// Fails when the persistence unit starts: the provider rejects the class as an entity
// (exact message depends on the Hibernate version)
What each use needs, and records
UseRequirementRecord fits?
Jackson request/response DTOconstructor binding (Jackson 2.12+)Yes
Bean Validation targetannotations on fields or parametersYes
JPA entityno-arg constructor, non-final, mutable stateNo
JPA embeddableprovider support for instantiationYes, in Hibernate 6.2+
JPQL constructor expressionmatching constructorYes
Spring Data DTO projectionmatching constructorYes
Records fit everywhere data is carried; entities are the exception.

The Jakarta Persistence specification requires an entity class to have a public or protected no-argument constructor, to not be final, and to not have final persistent fields or methods. A record breaks all three. The reasons are not arbitrary. The provider creates an empty entity and then fills in the columns when it loads a row, which needs a no-argument constructor and assignable fields. It tracks changes by comparing or intercepting field values, which needs state that can change in place. And it returns lazy proxies, generated subclasses that load the data on first access, which needs a non-final class. A record is final, has only a canonical constructor and has final fields, so it cannot play this role.

3Where Records Do Fit in Persistence

Records still have two good places in the persistence layer. The first is embeddables, groups of columns stored inside the owning entity's table, such as an address or a money amount. Hibernate 6.2 added support for records as embeddables, instantiating them through the canonical constructor, and Jakarta Persistence 3.2 recognises them. Check your provider version if you are not on Hibernate.

Java
@Embeddable
public record Money(BigDecimal amount, String currency) {}
@Entity
public class Order {
@Id @GeneratedValue Long id;
String customerId;
@Embedded Money total; // columns amount and currency in the orders table
protected Order() {} // for JPA
}
// Projection with a JPQL constructor expression
public record OrderSummary(Long id, BigDecimal amount) {}
@Query("select new com.shop.OrderSummary(o.id, o.total.amount) from Order o where o.customerId = :c")
List<OrderSummary> summaries(@Param("c") String customerId); // [OrderSummary[id=7, amount=42.00]]

The second place is query projections. A projection reads only the columns we need into a small type, instead of loading full managed entities. Records are ideal because they are read-only by nature: nobody will mistakenly change a projection and expect it to be saved. Spring Data also supports class-based DTO projections, where a repository method returns a record whose constructor parameters match selected properties, and interface-based projections, which are interfaces rather than records but solve the same problem.

4Mapping at the Service Boundary

So the working design has two models. The entity is a mutable class managed by JPA, with business methods that keep it valid. The records are the API model, used for requests, responses and read queries. The service layer is the boundary where one becomes the other.

Java
@Transactional
public OrderResponse create(CreateOrderRequest req) {
Order order = Order.place(req.customerId());
req.lines().forEach(l -> order.addLine(l.sku(), l.quantity()));
repository.save(order);
return new OrderResponse(order.id().toString(), order.total().amount(), order.status().name());
}

Mapping can be written by hand, as here, or generated with MapStruct, which supports records as targets. The separation pays for itself: the database schema and the JSON contract can now change independently, lazy-loading exceptions cannot appear during JSON serialization because no entity reaches the controller, and the API never exposes columns by accident.

5Follow-up questions

Can Spring Data JDBC or other mappers use records as aggregates?

Spring Data JDBC can map immutable types through their constructor and uses wither methods or the constructor to set generated ids, so records can work there. That is a different model from JPA, with no lazy loading or dirty checking. Check the Spring Data JDBC documentation for your version before choosing it.

Do Bean Validation annotations on components work for method validation too?

Yes. With @Validated on a Spring bean, constraints on method parameters and on the components of record parameters are checked through method validation. In Spring Framework 6.1 and later, controller method validation is also built in for parameters such as @RequestParam.

Is it worth using records for embeddables if the team is on an older Hibernate?

Not before upgrading. On Hibernate versions before 6.2, a record embeddable needs a custom instantiator, which adds complexity. A small final class with a protected constructor is the simpler choice until the upgrade.

6Conclusion

In this answer, we saw that records are a strong fit for Spring MVC DTOs, because Jackson 2.12 and later binds them through the canonical constructor and Bean Validation annotations on components are enforced with @Valid. Records cannot be JPA entities, because entities need a no-argument constructor, mutable state and non-final classes for proxies. They work as embeddables in Hibernate 6.2 and later and as query projections through constructor expressions or Spring Data DTO projections, and the clean design maps between entities and records at the service boundary.

What to say in the interviewAbout 90 seconds aloud

Open with

For the web layer, records are a very good choice. Jackson since 2.12 binds records through the canonical constructor and serializes through the accessors, and every Spring Boot 3 release manages a much newer Jackson, with Spring Boot 4 on Jackson 3.

Then, if they want more

Bean Validation annotations like @NotBlank on record components are copied to the fields, so @Valid @RequestBody works and gives a 400 with field errors. And the requests are immutable, which is what we want. For JPA entities, records do not work. The specification needs a public or protected no-argument constructor, a non-final class and non-final persistent fields.

That is because the provider creates empty objects and fills them when loading rows, tracks changes to mutable state, and creates lazy proxies as subclasses. A record is final with final fields and only a canonical constructor, so it fails all three.

Records do fit in two places in persistence: as embeddables, which Hibernate supports since 6.2, for things like Money or Address, and as projections, either with a JPQL constructor expression, select new OrderSummary, or Spring Data class-based DTO projections.

So the design I would propose is a mutable, encapsulated entity class for persistence, records for requests, responses and read models, and mapping between them in the service layer, by hand or with MapStruct. That also keeps lazy loading problems and accidental column exposure out of the API.

About this answer

Last updated
12 November 2026
Versions it names
Hibernate 6, Spring Boot 3, Spring Boot 4, Spring Framework 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 documentation below. 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 "A team wants to use records for request and response DTOs in Spring MVC with Jackson...": https://scrivio-interview-library.netlify.app/java/records/e2e0571fde/