Spring JPA
Querying Data
Select Statements
Database Columns
Java Programming

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:

java
1import java.util.List;
2
3import org.springframework.data.jpa.repository.JpaRepository;
4import org.springframework.data.jpa.repository.Query;
5import org.springframework.stereotype.Repository;
6
7@Repository
8public interface UserRepository extends JpaRepository<User, Long> {
9    @Query("SELECT u.name, u.email FROM User u")
10    List<Object[]> findNamesAndEmails();
11}

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:

java
1CriteriaBuilder cb = entityManager.getCriteriaBuilder();
2CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class);
3Root<User> user = cq.from(User.class);
4cq.multiselect(user.get("name"), user.get("email"));
5TypedQuery<Object[]> query = entityManager.createQuery(cq);
6List<Object[]> results = query.getResultList();

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:

java
1public interface UserNameAndEmail {
2    String getName();
3    String getEmail();
4}
5
6@Repository
7public interface UserRepository extends JpaRepository<User, Long> {
8    List<UserNameAndEmail> findProjectedBy();
9}

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:

java
1public class UserDTO {
2    private String name;
3    private String email;
4
5    public UserDTO(String name, String email) {
6        this.name = name;
7        this.email = email;
8    }
9
10    // Getters and setters
11}
12
13@Repository
14public interface UserRepository extends JpaRepository<User, Long> {
15    @Query("SELECT new com.example.UserDTO(u.name, u.email) FROM User u")
16    List<UserDTO> findUserDTOs();
17}

The DTOs are initialized directly in the query, which can be simpler than interface projections when the data is complex.

Choosing a technique

TechniqueDescriptionType SafetyFlexibility
JPQL QueriesSimple string-based queriesLowHigh (manual)
Criteria APIType-safe API for dynamic queriesHighHigh
Interface ProjectionsSpring Data JPA automatically implements the interfacesHighMedium (limited custom logic)
DTO ProjectionsStrong typing with advanced mapping logicHighMedium (coding overhead)

The right choice depends on whether you need dynamic queries, how complex the mapping is, and whether you need type-safe results.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design