Spring JPA selecting specific columns

user1817436 picture user1817436 · Feb 25, 2014 · Viewed 295.2k times · Source

I am using Spring JPA to perform all database operations. However I don't know how to select specific columns from a table in Spring JPA?

For example:
SELECT projectId, projectName FROM projects

Answer

mpr picture mpr · Dec 2, 2016

You can use projections from Spring Data JPA (doc). In your case, create interface:

interface ProjectIdAndName{
    String getId();
    String getName();
}

and add following method to your repository

List<ProjectIdAndName> findAll();