Quick revision
The short answer
The first way is an interface projection: we declare interface CustomerSummary { Long getId(); String getName(); } and a repository method such as List<CustomerSummary> findByActiveTrue(), and Spring Data selects only those two columns. The second is a record or class DTO filled by a JPQL constructor expression, @Query("select new com.example.CustomerDto(c.id, c.name) from Customer c"). Both return unmanaged objects, so Hibernate does no dirty checking, but only closed projections narrow the SQL; open projections with @Value expressions load the full entity.
What to remember
In this answer, we loaded customer ids and names in two ways: a closed interface projection, where Spring Data selects only the columns behind the getters, and a record DTO filled by a JPQL constructor expression. Both return unmanaged objects with no dirty checking overhead. We saw that open projections with @Value expressions load the full entity and lose the benefit, and that dynamic projections let the caller choose the shape with a Class<T> parameter.
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.
The first way is an interface projection: we declare interface CustomerSummary { Long getId(); String getName(); } and a repository method such as List<CustomerSummary> findByActiveTrue(), and Spring Data selects only those two columns. The second is a record or class DTO filled by a JPQL constructor expression, @Query("select new com.example.CustomerDto(c.id, c.name) from Customer c"). Both return unmanaged objects, so Hibernate does no dirty checking, but only closed projections narrow the SQL; open projections with @Value expressions load the full entity.
Why this question is asked
Most list screens and API responses need a handful of fields, yet the easiest code loads whole entities with every column and every eager association. On a table with 40 columns and a few thousand rows, that difference shows up directly in latency and memory. Interviewers ask this to check that we know the projection features Spring Data JPA gives us, and the small rules that decide whether they really save anything. In this answer, we will recap what a projection is, write the interface version and the DTO version, look at what SQL each one produces, see the open projection trap, and finish with dynamic projections.
1A Quick Recap of Projections
A projection is a query result that contains only some attributes instead of a full entity. In SQL terms, it is the difference between select * and select id, name. In JPA terms, the result is not an entity: Hibernate does not put it in the persistence context, does not keep a snapshot of it and does not check it for changes at flush. That makes projections cheaper in two ways: less data from the database, and less bookkeeping in memory.
Let us use one entity for both examples.
Loading this entity for a list of names would bring notes and join address for every row, even though we only show two fields.
2Way 1: An Interface Projection
We declare an interface whose getters match entity attribute names, and use it as the return type of a repository method. Spring Data sees that the interface only needs id and name and builds a query that selects only those columns. At runtime it returns proxies, objects generated on the fly that implement our interface and read from the query's result row.
This is called a closed projection, because every getter maps directly to an attribute and Spring Data knows exactly what to select. Notice that no join to address and no notes column appear in the SQL.
3Way 2: A Record DTO With a Constructor Expression
The second way returns a plain class or, in modern Java, a record. In JPQL we write a constructor expression with select new and the fully qualified class name; Hibernate calls the constructor once per row with the selected values.
| Interface projection | Record DTO | |
|---|---|---|
| Declared as | interface with getters | record or class with constructor |
| Query | derived query or @Query | @Query with select new, or derived |
| Runtime object | proxy generated by Spring Data | our own record instance |
| Good for | quick read models in repositories | API responses, passing between layers |
| Managed by Hibernate? | no | no |
The constructor's parameter types must match the selected expressions in order, and the class name must be fully qualified, otherwise the query fails when the repository starts. A record is a good fit: it is immutable, has equals and toString, and serialises cleanly to JSON. Spring Data can also return a record from a derived query such as List<CustomerDto> findByActiveTrue(), deriving the constructor call from the record's components; check your version for the exact rules when combining DTO classes with @Query.
4The Open Projection Trap
Interface projections can also compute values with a SpEL expression, Spring's expression language, in @Value. That is called an open projection. It looks harmless, but because the expression can use any attribute of target, the entity, Spring Data cannot know which columns are needed. So it loads the full entity and evaluates the expression on it.
So if the point of the projection is to save work, keep it closed. If we need a computed value, select the columns we need into a record and compute it there, for example with a method on the record.
5No Dirty Checking, and Dynamic Projections
Because projections are not managed, changing a value on a DTO does nothing to the database. That is a feature for reads: Hibernate does not create snapshots for them and has nothing to compare at flush, which matters when a list returns thousands of rows. It also means projections are the wrong tool when we intend to update the data; then we load the entity.
Spring Data also supports dynamic projections. We add a Class<T> parameter and the caller chooses the shape at runtime, which saves us from writing one repository method per shape.
6Follow-up questions
Can an interface projection include a nested object?
Yes. A getter can return another projection interface, for example AddressSummary getAddress() with its own getCity(). Be aware that nested projections may load the associated entity rather than only its columns, depending on the association and the Spring Data version; check the generated SQL.
Can I use a projection with native queries?
Interface projections work with native queries: Spring Data maps the result columns to getters by alias, so select id, name as name works. Constructor expressions are JPQL only; for native SQL we use interface projections, a result set mapping or JdbcClient.
Is a projection always faster than an entity?
It is cheaper when we select fewer columns and avoid managed state, which is most read screens. For a single row by id that is already in the persistence context, an entity can cost nothing extra. Measure with the SQL log when in doubt.
7Conclusion
In this answer, we loaded customer ids and names in two ways: a closed interface projection, where Spring Data selects only the columns behind the getters, and a record DTO filled by a JPQL constructor expression. Both return unmanaged objects with no dirty checking overhead. We saw that open projections with @Value expressions load the full entity and lose the benefit, and that dynamic projections let the caller choose the shape with a Class<T> parameter.
Open with
There are two main ways. The first is an interface-based projection: I declare an interface CustomerSummary with getId and getName, and a repository method like List of CustomerSummary findByActiveTrue.
Then, if they want more
Because it is a closed projection, every getter maps to an attribute, Spring Data selects only id and name, and returns proxies that implement the interface. The second is a class or record DTO, CustomerDto with id and name, filled by a JPQL constructor expression in @Query: select new com.example.CustomerDto(c.id, c.name) from Customer c.
The class name must be fully qualified and the constructor must match the selected types. Recent Spring Data versions can also return records from derived queries. In both cases the results are not managed entities, so Hibernate keeps no snapshots and does no dirty checking, which matters for long lists.
One trap is open projections, where a getter has an @Value SpEL expression using target; Spring Data cannot know which columns are needed, so it loads the whole entity. And if different callers need different shapes, I can use a dynamic projection with a Class of T parameter.
About this answer
- Last updated
- 12 November 2026
- 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
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "Show two ways to load only id and name of customers into a DTO with Spring Data JPA...": https://scrivio-interview-library.netlify.app/databases/jpa-performance/c9aa566f9e/