254

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

ElGavilan
  • 6,610
  • 16
  • 27
  • 36
user1817436
  • 2,595
  • 2
  • 13
  • 7
  • 3
    see this http://stackoverflow.com/questions/12618489/jpa-criteria-api-select-only-specific-columns – Abhishek Nayak Feb 25 '14 at 07:30
  • 1
    The idea behind JPA not looking for specific fields is that is cost (efficiency wise) the same to bring one column or all columns from one row of the table. – Desorder Sep 15 '14 at 09:30
  • 13
    @Desorder -- the cost is not always the same. It's probably not a big deal for simpler, primitive sort of datatypes but the reason I ended up on this page is because I noticed a simple "list documents" query was running slow. That entity has a BLOB column (needs it for file upload/storage) & I suspect it is slow because it is loading the BLOBs into memory even though they're not required for listing the docs. – jm0 Mar 12 '15 at 20:59
  • @jm0 As far as you remember, how many tables had BLOB columns? – Desorder Mar 14 '15 at 08:06
  • 1
    @Desorder it was just one table but I was doing a "list" function (multirow -- list all docs created by a given id). The only reason I noticed this issue was because this simple list query was taking several seconds, whereas more complex queries on other tables were happening almost instantly. Once I realized, I knew it would suffer more and more as rows are added because Spring JPA is loading every BLOB into memory even tho they are not used. I found a decent solution for Spring data (posted below) but I think I have an even better one that is pure JPA annotation, I will post tmrw if it works – jm0 Mar 16 '15 at 01:26
  • Spring Data Projection is the solution. Ref: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections – Shivang Agarwal Dec 12 '21 at 20:38
  • To people saying that selecting specific values has no sense: indeed selecting specific entries has much sense, for example whenever the user is doing something like SELECT MAX(something) or SELECT AVG(something). So, indeed it's a problem if it's not so intuitive to do so from JPA, and so indeed the question is useful. Thanks for reporting this question. – Valerio Bozz May 26 '23 at 14:26

19 Answers19

309

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();
mpr
  • 3,730
  • 3
  • 17
  • 22
  • 25
    This is a clean solution. it may have boiler template but the interface can be the inner class of the entity. Making it quite clean. – iceman Oct 05 '17 at 22:10
  • 2
    awesome, just remember not to implement the interface on your Entity or it won't work – alizelzele Nov 29 '17 at 05:24
  • 1
    where does the projected interface go? in its own file or can it be included in the public interface that returns the full entity properties? – Micho Rizo Jan 19 '18 at 06:25
  • 18
    This solution doesn't work when extending JpaRepository, any one knows a workaround? – Germán Sep 06 '18 at 14:21
  • For me the values are always null :( – Nehal Damania Sep 22 '18 at 12:21
  • 4
    I had no issues getting this to work with JpaRepository, try this method signature List findAllProjectedBy() inside your repository class. I would also suggest keeping your projection interfaces close to your DTOs in terms of packaging. IMO projection interfaces behave like DTOs. – Jim Kennedy Jul 01 '19 at 15:04
  • 15
    You can not use findAll(); as it will clash with JPARepositorys method. You need to use something like List findAllBy(); – Vikas Jul 22 '19 at 11:21
  • 1
    It is good approach, but it does not work with native queries. -_- – Peter S. Oct 07 '19 at 07:59
  • 3
    You will also need to `as` any columns in your query that do not map to the name of the projection field e.g. if table col is `event_type` and the projection field is `eventType` then the query should be `select event_type as eventType ...` – 8bitme Feb 11 '20 at 10:10
  • Really like this solution, elegant and simple. Also, as it's an interface, it's easy to mock for testing. Also had to use an alias for column name where it has an underscore, as mentioned by @8bitme above – shakel May 07 '20 at 16:08
  • Maybe this will help : https://www.logicbig.com/tutorials/spring-framework/spring-data/projections-using-default-methods.html – samaniego Jun 22 '20 at 20:13
  • I need to use a @caxheable over this query, for such a case can I make the object serializable? I am getting an error when I try this. – neeraj Nov 23 '20 at 22:33
  • This Answer is SimplySuperb. But what if I want to query like select col1, col2, col3 where col1 = id; Any Help? May be @Code_Mode – GreenROBO May 26 '21 at 01:27
  • @JimKennedy Or can you give any Idea about my above comment? – GreenROBO May 26 '21 at 01:34
  • This is good solution, too bad it does not work with specifications. – zygimantus Sep 07 '21 at 09:24
  • For JPARepositories I recommend `List findDistinctAllBy()` – Datz Sep 29 '21 at 19:45
  • 1
    I've tried this, even though it's return specific column, but the query that's generated still using all the column – alcatraz Dec 10 '21 at 07:13
  • The underlying datasource is still returned by the repo, and you might want to reduce data processing by avoiding large objects – Huub Feb 23 '22 at 17:52
  • @Germán Actually it's working for me when extending JpaRepository. `Collection findAllByIsActiveTrue();` – malware_656 Sep 22 '22 at 03:54
198

I don't like the syntax particularly (it looks a little bit hacky...) but this is the most elegant solution I was able to find (it uses a custom JPQL query in the JPA repository class):

@Query("select new com.foo.bar.entity.Document(d.docId, d.filename) from Document d where d.filterCol = ?1")
List<Document> findDocumentsForListing(String filterValue);

Then of course, you just have to provide a constructor for Document that accepts docId & filename as constructor args.

ifnotak
  • 4,147
  • 3
  • 22
  • 36
jm0
  • 3,294
  • 2
  • 16
  • 18
  • 18
    (and btw I verified, you don't need to provide the fully qualified classname if "Document" is imported -- just had it that way because that's how it was done in the only sample I was able to find) – jm0 Apr 23 '15 at 14:40
  • this should be the accepted answer. It works perfectly and really selects only the necessary fields. – Yonatan Wilkof Jul 22 '16 at 12:13
  • 1
    The unnecessary fields are also included, but with the value 'null', would those fields occupy memory? – gabbler Nov 21 '16 at 09:09
  • yes but so minimal that in vast majority of cases it would be really ridiculous to try to engineer around this -- http://stackoverflow.com/questions/2430655/java-does-null-variable-require-space-in-memory you'd have to make specialized lightweight objects without these fields & have them point to same table? which IMO is undesired when using ORMs and leveraging them for their relationships... hyper-optimization is maybe more in the realm of just using some lightweight query DSL and mapping directly to DTOs, & even then i think redundancy is discouraged – jm0 Nov 21 '16 at 15:08
  • 3
    jm0 it did not work for me without fully qualified classname, though it was imported. It did compile successfully though. – Heisenberg Apr 25 '18 at 13:14
  • interesting... maybe i didn't test it correctly (but I thought I did) or things have changed. This answer is quite old. – jm0 May 17 '18 at 18:41
  • You don't need a custom query. If Document contains only 2 fields, then Spring JPA knows to only fetch those 2 columns from the database, as described in [this answer](https://stackoverflow.com/a/41015492/782011). – pacoverflow Aug 14 '19 at 21:49
  • This solution looks the most easy to apply but what if you have more than 8 fields that you want to fetch ? In that case sonar lint would fail if you have some max parameter length rule is set . – Vishal Nair Feb 09 '20 at 13:37
  • It's the best solution ! (Simple, no new Interface or class) It's works with Json for send to a client waiting for full entity (without any client-side adaptation). I confirm that it's not necessary to provide fully qualified classname (Spring boot 2.3.3) – Elloco Aug 28 '20 at 09:24
  • This is the good solution. Also if the Repository and the Entity class in same package you don't need to provide the fully qualified class name. I generally organize code in module/features, for some one organize code by the type of code such as Model, DAO etc this won't help. – Nish Dec 19 '22 at 17:25
  • @VishalNair can u elaborate on your point which says, `" In that case sonar lint would fail if you have some max parameter length rule is set."` – humbleCodes Mar 28 '23 at 20:37
104

You can set nativeQuery = true in the @Query annotation from a Repository class like this:

public static final String FIND_PROJECTS = "SELECT projectId, projectName FROM projects";

@Query(value = FIND_PROJECTS, nativeQuery = true)
public List<Object[]> findProjects();

Note that you will have to do the mapping yourself though. It's probably easier to just use the regular mapped lookup like this unless you really only need those two values:

public List<Project> findAll()

It's probably worth looking at the Spring data docs as well.

Durandal
  • 5,575
  • 5
  • 35
  • 49
  • 14
    no need for native queries. You should avoid using them, for they ruin the advantages of JPQL. see Atals answer. – phil294 Jan 28 '17 at 20:26
  • 1
    For me I had to qualify the first attribute (above `FIND_PROJECTS`) with the `value` attribute name (hence if this was my code I would have had to write it as `@Query(value = FIND_PROJECTS, nativeQuery = true)`, etc. – smeeb Apr 17 '17 at 20:57
35

In my situation, I only need the json result, and this works for me:

public interface SchoolRepository extends JpaRepository<School,Integer> {
    @Query("select s.id, s.name from School s")
    List<Object> getSchoolIdAndName();
}

in Controller:

@Autowired
private SchoolRepository schoolRepository;

@ResponseBody
@RequestMapping("getschoolidandname.do")
public List<Object> getSchool() {
    List<Object> schools = schoolRepository.getSchoolIdAndName();
    return schools;
}
buræquete
  • 14,226
  • 4
  • 44
  • 89
Atal
  • 351
  • 3
  • 2
  • 2
    you should substitute `Object` with a custom interface as described by mpr. works flawlessly – phil294 Feb 21 '17 at 09:13
  • mpr's solution selects all queries from the database and filters them server-side. See the difference by logging the native queries sent to the database. – IcyIcicle Oct 19 '22 at 22:59
18

With the newer Spring versions One can do as follows:

If not using native query this can done as below:

public interface ProjectMini {
    String getProjectId();
    String getProjectName();
}

public interface ProjectRepository extends JpaRepository<Project, String> { 
    @Query("SELECT p FROM Project p")
    List<ProjectMini> findAllProjectsMini();
}

Using native query the same can be done as below:

public interface ProjectRepository extends JpaRepository<Project, String> { 
    @Query(value = "SELECT projectId, projectName FROM project", nativeQuery = true)
    List<ProjectMini> findAllProjectsMini();
}

For detail check the docs

jombie
  • 231
  • 2
  • 8
15

In my case i created a separate entity class without the fields that are not required (only with the fields that are required).

Map the entity to the same table. Now when all the columns are required i use the old entity, when only some columns are required, i use the lite entity.

e.g.

@Entity
@Table(name = "user")
Class User{
         @Column(name = "id", unique=true, nullable=false)
         int id;
         @Column(name = "name", nullable=false)
         String name;
         @Column(name = "address", nullable=false)
         Address address;
}

You can create something like :

@Entity
@Table(name = "user")
Class UserLite{
         @Column(name = "id", unique=true, nullable=false)
         int id;
         @Column(name = "name", nullable=false)
         String name;
}

This works when you know the columns to fetch (and this is not going to change).

won't work if you need to dynamically decide the columns.

Sachin Sharma
  • 1,446
  • 4
  • 18
  • 37
12

In my opinion this is great solution:

interface PersonRepository extends Repository<Person, UUID> {

    <T> Collection<T> findByLastname(String lastname, Class<T> type);
}

and using it like so

void someMethod(PersonRepository people) {

  Collection<Person> aggregates =
    people.findByLastname("Matthews", Person.class);

  Collection<NamesOnly> aggregates =
    people.findByLastname("Matthews", NamesOnly.class);
}
Evgeni Atanasov
  • 482
  • 1
  • 5
  • 13
11

Using Spring Data JPA there is a provision to select specific columns from database

---- In DAOImpl ----

@Override
    @Transactional
    public List<Employee> getAllEmployee() throws Exception {
    LOGGER.info("Inside getAllEmployee");
    List<Employee> empList = empRepo.getNameAndCityOnly();
    return empList;
    }

---- In Repo ----

public interface EmployeeRepository extends CrudRepository<Employee,Integer> {
    @Query("select e.name, e.city from Employee e" )
    List<Employee> getNameAndCityOnly();
}

It worked 100% in my case. Thanks.

Yash
  • 11,486
  • 4
  • 19
  • 35
SR Ranjan
  • 113
  • 1
  • 5
10

I guess the easy way may be is using QueryDSL, that comes with the Spring-Data.

Using to your question the answer can be

JPAQuery query = new JPAQuery(entityManager);
List<Tuple> result = query.from(projects).list(project.projectId, project.projectName);
for (Tuple row : result) {
 System.out.println("project ID " + row.get(project.projectId));
 System.out.println("project Name " + row.get(project.projectName)); 
}}

The entity manager can be Autowired and you always will work with object and clases without use *QL language.

As you can see in the link the last choice seems, almost for me, more elegant, that is, using DTO for store the result. Apply to your example that will be:

JPAQuery query = new JPAQuery(entityManager);
QProject project = QProject.project;
List<ProjectDTO> dtos = query.from(project).list(new QProjectDTO(project.projectId, project.projectName));

Defining ProjectDTO as:

class ProjectDTO {

 private long id;
 private String name;
 @QueryProjection
 public ProjectDTO(long projectId, String projectName){
   this.id = projectId;
   this.name = projectName;
 }
 public String getProjectId(){ ... }
 public String getProjectName(){....}
}
kszosze
  • 341
  • 4
  • 9
4

You can use JPQL:

TypedQuery <Object[]> query = em.createQuery(
  "SELECT p.projectId, p.projectName FROM projects AS p", Object[].class);

List<Object[]> results = query.getResultList();

or you can use native sql query.

Query query = em.createNativeQuery("sql statement");
List<Object[]> results = query.getResultList();
Henrik
  • 1,797
  • 4
  • 22
  • 46
2

It is possible to specify null as field value in native sql.

@Query(value = "select p.id, p.uid, p.title, null as documentation, p.ptype " +
            " from projects p " +
            "where p.uid = (:uid)" +
            "  and p.ptype = 'P'", nativeQuery = true)
Project findInfoByUid(@Param("uid") String uid);
hahn
  • 3,588
  • 20
  • 31
2

You can apply the below code in your repository interface class.

entityname means your database table name like projects. And List means Project is Entity class in your Projects.

@Query(value="select p from #{#entityName} p where p.id=:projectId and p.projectName=:projectName")

List<Project> findAll(@Param("projectId") int projectId, @Param("projectName") String projectName);
Sneha
  • 2,200
  • 6
  • 22
  • 36
ajaz
  • 29
  • 3
1

You can use the answer suggested by @jombie, and:

  • place the interface in a separate file, outside the entity class;
  • use native query or not (the choice depended on your needs);
  • don't override findAll() method for this purpose but use name of your choice;
  • remember to return a List parametrized with your new interface (e.g. List<SmallProject>).
foxbit
  • 241
  • 3
  • 4
1
public static final String FIND_PROJECTS = "select ac_year_id,ac_year from tbl_au_academic_year where ac_year_id=?1";

    @Query(value = FIND_PROJECTS, nativeQuery = true)
    public  List<Object[]> findByAcYearId(Integer ac_year_id);

this works for me

Wang Liang
  • 4,244
  • 6
  • 22
  • 45
vijay
  • 31
  • 2
1

You can update your JPARepository as below.

@Query("select u.status from UserLogin u where u.userId = ?1 or u.email = ?1 or u.mobile = ?1")
public UserStatus findByUserIdOrEmailOrMobile(String loginId);

Where UserStatus is a Enum

public enum UserStatus
{
    New,
    Active,
    Deactived,
    Suspended,
    Locked
}
1

You can use a DTO like that

 @Data
    public class UserDtoLight implements Serializable {
       private final Long id;
       private final String name;
    }

and in your repository

 List<UserDtoLight> findAll();
Yehouda
  • 112
  • 1
  • 6
1

With current version I'm using (JDK 11, Spring Boot 3) It's very simple:

  1. You need to add a constructor in your entity class for limited fields which you want to select:
    class Projects {
        private String projectId;
        private String projectName;
        // other fields
    
        // No argument / all arguments  constructor as needed
    
        public Projects(String projectId, String projectName) {
            this.projectId = projectId;
            this.projectName = projectName;
        }
    }
  1. In your Repository, you can add Query & method as below (add WHERE condition as needed):
    @Query("SELECT new Projects (p.projectId, p.projectName) FROM Projects p")
    List<Projects > findAllProjects();

For me, this works & I get list of all objects with only 2 fields specified.

Saurabhcdt
  • 1,010
  • 1
  • 12
  • 24
0

Using Native Query:

Query query = entityManager.createNativeQuery("SELECT projectId, projectName FROM projects");
List result = query.getResultList();
ukchaudhary
  • 377
  • 3
  • 7
-3

Use:

@Query("SELECT e FROM #{#entityName} e where e.userId=:uid")
List<ClienteEnderecoEntity> findInfoByUid(@Param("uid") UUID uid);
Gil
  • 43
  • 3