I'm using JAVA JPA in spring boot. The scenario is my table is having around 8 columns (suppose id, A,B,C,D,E,F,G,H) from these I want 5 column as result E,F,G,H and only one from A,B,C,D which will be not fixed for every execution.
I have written like this.
@Query("Select :dynamicColName ,E,F,G,H FROM TABLEname where id= :id ") List findById(@Param("dynamicColName")String dynamicColName, @Param("id")String id );
It's not working. Here query isn't refering value of :dynamicColName as column name here.
Suggestion are highly appreciated.
Dynamic column name injection in query
Option 1 - Workaround: You can use EntityManager:
Option 2 - CASE block: add a condition inside the SELECT query part:
The solution will looks like this:
Reference Wikipedia article: New in JPA 2.0
Option 3 - JPA Criteria API
OpenJPA: Chapter 11. JPA Criteria
Bueldung: JPA Criteria Queries