2

While using a verbatim entered column name works fine in an SQL/JPA query, is it possible to use a variable?

@Query(value = "select distinct ?1 from Product", nativeQuery = true)
List<String> findOneColumn(String columnName);

This code appears to fail. It seems that it's not allowed to use parameters in SELECT clause (but works with WHERE clause). Is there any alternatives?

How can I set the column name dynamically?

Anton Petrovskyi
  • 442
  • 5
  • 18
  • What alternatives did you check? How far did you look for an existing answer? Is http://stackoverflow.com/questions/3144235/jpa-hibernate-native-queries-do-not-recognize-parameters related? – Abecee Jan 13 '15 at 00:27
  • "appears to fail"? and the evidence of that is? an exception? And does JDBC allow a parameter in the SELECT clause? – Neil Stockton Jan 13 '15 at 07:53
  • the link doesn't solve this problem. And it seems that it's not allowed to use parameter in SELECT clause. Is there any alternatives? The main goal is dynamic selection of the column... – Anton Petrovskyi Jan 13 '15 at 10:03

1 Answers1

2

Seems that Parameter in SELECT clause for @Query is not allowed pleasure :)

Anton Petrovskyi
  • 442
  • 5
  • 18
  • I also just had this issue, and could not find a solution - other than to build the query dynamically or hardcode the value in the SELECT – davidfrancis Apr 02 '19 at 11:26