Spring Data JPA: selecting specific columns
Codemia Knowledge Hub ·
To fetch only some columns in Spring Data JPA, write a JPQL @Query that selects those fields, build a Criteria API multiselect, or use a projection (an interface or a DTO). Fetching only the data you need saves performance and resources.
JPQL query returning Object[]
JPQL is JPA's object-oriented query language. Put a custom query in your repository:
findNamesAndEmails returns a list of Object[], each holding one user's name and email, in the order of the SELECT clause. It is straightforward, but the results are not strongly typed unless you process them further.
Criteria API for dynamic queries
The Criteria API builds queries in a type-safe way and is useful when the query depends on runtime conditions:
This selects name and email and returns a list of object arrays.
Projections: interfaces and DTOs
A projection defines, through an interface or a DTO (Data Transfer Object), how the data should be shaped when it comes back from the database.
Interface-based projections
Declare getters that match the entity's column names:
Spring Data JPA implements the interface for you, giving clean, type-safe code that maps directly to your entity.
DTO projections
Or construct a DTO in the query:
The DTOs are initialized directly in the query, which can be simpler than interface projections when the data is complex.
Choosing a technique
| Technique | Description | Type Safety | Flexibility |
| JPQL Queries | Simple string-based queries | Low | High (manual) |
| Criteria API | Type-safe API for dynamic queries | High | High |
| Interface Projections | Spring Data JPA automatically implements the interfaces | High | Medium (limited custom logic) |
| DTO Projections | Strong typing with advanced mapping logic | High | Medium (coding overhead) |
The right choice depends on whether you need dynamic queries, how complex the mapping is, and whether you need type-safe results.
Related reading
- Spring nested transactions
- Spring @Transactional - isolation, propagation
- springboot 2.3.0 while connecting to h2 database
- SpringBoot doesn't handle org.hibernate.exception.ConstraintViolationException
- Spring Kafka- When is exactly Consumer.poll() called behind the hood?
- Spring Kafka - Consume last N messages for partitions(s) for any topic
- SpringBoot error Registered driver with driverClassNameoracle.jdbc.driver.OracleDriver was not found, trying direct instantiation
- Springboot org.hibernate.MappingException Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.