Retrieving limited columns using jpa specifications



I am using spring boot JPA specifications for executing complex queries. However, the table I am querying contains more than 20 columns and I need to pull just 3. I tried cq.multiselect(...) but it didn’t work and returned me the entity with all the columns.

On investigation, I got to know that it’s a bug with specifications that’s not yet fixed. Another option was to use projections but specifications can’t be combined with projections. An attempt to do so returns the complete entity.

I do not want to switch to Querydsl or @Query approach since it’s an existing code and I am stuck with specifications. Any pointers on how to limit the number of columns will be much appreciated 🙂

Answer

Got to know Spring boot JPA Specifications doesn’t provide any way to limit the number of columns. There is a bug related to this feature and it’s pending for more than 3 years now. Not sure if it will be available soon



Source: stackoverflow